Skip to content

Commit 3fb9019

Browse files
committed
Solve Problems 1, 9, 13 and 14
0 parents  commit 3fb9019

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
out/
3+
*.iml
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem_0001_two_sum;
2+
3+
// LINK: https://leetcode.com/problems/two-sum/
4+
5+
public class Solution {
6+
public int[] twoSum(int[] nums, int target) {
7+
for(int i=0; i<nums.length; i++)
8+
for(int j=0; j<nums.length; j++)
9+
if(nums[i] + nums[j] == target && i != j)
10+
return new int[]{i, j};
11+
return null;
12+
}
13+
14+
public static void main(String[] args) {
15+
int[] nums = {3, 2, 4};
16+
int target = 6;
17+
int[] output = new Solution().twoSum(nums, target);
18+
for (int n: output) System.out.println(n);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem_0009_palindrome_number;
2+
3+
// LINK: https://leetcode.com/problems/palindrome-number/
4+
5+
public class Solution {
6+
public String reverse(String s) {
7+
StringBuilder temp = new StringBuilder();
8+
for (int i = s.length() - 1; i >= 0; i--)
9+
temp.append(s.charAt(i));
10+
return String.valueOf(temp);
11+
}
12+
13+
public boolean isPalindrome(int x) {
14+
String s = String.valueOf(x);
15+
return s.equals(reverse(s));
16+
}
17+
18+
public static void main(String[] args) {
19+
System.out.println(new Solution().isPalindrome(1234567899));
20+
}
21+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package problem_0013_roman_to_integer;
2+
3+
import java.util.ArrayList;
4+
5+
// LINK: https://leetcode.com/problems/roman-to-integer/
6+
// I 1 | V 5 | X 10 | L 50 | C 100 | D 500 | M 1000
7+
// LVIII (50 5 1 1 1) = 58
8+
// MCMXCIV (1000 100 1000 10 100 1 5) = 1994
9+
// I can be placed before V (5) and X (10) to make 4 and 9.
10+
// X can be placed before L (50) and C (100) to make 40 and 90.
11+
// C can be placed before D (500) and M (1000) to make 400 and 900.
12+
13+
public class Solution {
14+
public int romanToInt(String s) {
15+
ArrayList<Integer> l = new ArrayList<>();
16+
for (char c : s.toCharArray())
17+
switch (c) {
18+
case 'I':
19+
l.add(1);
20+
break;
21+
case 'V':
22+
l.add(5);
23+
break;
24+
case 'X':
25+
l.add(10);
26+
break;
27+
case 'L':
28+
l.add(50);
29+
break;
30+
case 'C':
31+
l.add(100);
32+
break;
33+
case 'D':
34+
l.add(500);
35+
break;
36+
case 'M':
37+
l.add(1000);
38+
break;
39+
default:
40+
break;
41+
}
42+
43+
for (int i = 0; i < l.size(); i++)
44+
if (i + 1 < l.size() && l.get(i) < l.get(i + 1)) {
45+
l.set(i + 1, l.get(i + 1) - l.get(i));
46+
l.remove(i);
47+
}
48+
49+
int sum = 0;
50+
for (int n : l) sum += n;
51+
return sum;
52+
}
53+
54+
public static void main(String[] args) {
55+
System.out.println(new Solution().romanToInt("MCMXCIV"));
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem_0014_longest_common_prefix;
2+
3+
import java.util.Arrays;
4+
5+
// LINK: https://leetcode.com/problems/longest-common-prefix/
6+
7+
public class Solution {
8+
public String longestCommonPrefix(String[] strs) {
9+
// MORE ABOUT STREAMS: https://www.geeksforgeeks.org/stream-in-java/
10+
String shortestStr = Arrays.stream(strs)
11+
.reduce(strs[0], (res, s) -> s.length() < res.length() ? s : res);
12+
13+
StringBuilder prefix = new StringBuilder();
14+
15+
outerloop:
16+
for (int i = 0; i < shortestStr.length(); i++) {
17+
for (String s : strs)
18+
if (s.charAt(i) != shortestStr.charAt(i)) break outerloop;
19+
prefix.append(shortestStr.charAt(i));
20+
}
21+
22+
return String.valueOf(prefix);
23+
}
24+
25+
public static void main(String[] args) {
26+
String[] strs = {"flower", "flow", "flight"};
27+
System.out.println(new Solution().longestCommonPrefix(strs));
28+
}
29+
}

0 commit comments

Comments
 (0)