Skip to content

Commit bbcb43a

Browse files
committed
Added 2 solutions & refactored 4 solutions
1 parent 0b68c5e commit bbcb43a

6 files changed

+95
-67
lines changed

Easy/Implement strStr.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
class Solution {
22
public int strStr(String haystack, String needle) {
3-
return haystack.indexOf(needle);
3+
int start = 0;
4+
int end = haystack.length() - needle.length() + 1;
5+
6+
while (start < end) {
7+
if (haystack.substring(start, start + needle.length()).equals(needle)) {
8+
return start;
9+
}
10+
11+
start++;
12+
}
13+
14+
return -1;
415
}
516
}

Easy/Last Stone Weight.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int lastStoneWeight(int[] stones) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
4+
@Override
5+
public int compare(Integer o1, Integer o2) {
6+
return o2.compareTo(o1);
7+
}
8+
});
9+
10+
for (int stone : stones) {
11+
pq.add(stone);
12+
}
13+
14+
while (pq.size() > 1) {
15+
int stone1 = pq.poll();
16+
int stone2 = pq.poll();
17+
18+
int resSize = Math.max(stone1, stone2) - Math.min(stone1, stone2);
19+
20+
if (resSize > 0) {
21+
pq.add(resSize);
22+
}
23+
}
24+
25+
return pq.size() == 1 ? pq.poll() : 0;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String removeDuplicates(String S) {
3+
StringBuilder sb = new StringBuilder();
4+
char[] chars = S.toCharArray();
5+
6+
for (char c : chars) {
7+
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c) {
8+
sb.deleteCharAt(sb.length() - 1);
9+
}
10+
else {
11+
sb.append(c);
12+
}
13+
}
14+
15+
return sb.toString();
16+
}
17+
}

Medium/Integer To Roman.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
class Solution {
22
public String intToRoman(int num) {
3-
List<Integer> keys = Arrays.asList(1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000);
4-
List<String> values = Arrays.asList("I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M");
5-
Map<Integer, String> map = new HashMap<>();
6-
for (int i=0; i<keys.size(); i++) {
7-
map.put(keys.get(i), values.get(i));
8-
}
9-
3+
int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
4+
String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
5+
106
StringBuilder sb = new StringBuilder();
11-
12-
for (int i=keys.size()-1; i>=0; i--) {
13-
if (num / keys.get(i) > 0) {
14-
int n = num / keys.get(i);
15-
num %= keys.get(i);
16-
17-
sb.append(String.join("", Collections.nCopies(n, map.get(keys.get(i)))));
18-
}
19-
20-
if (num == 0) {
21-
break;
7+
8+
for (int i = 0; i < values.length; i++) {
9+
while (num >= values[i]) {
10+
num -= values[i];
11+
sb.append(strs[i]);
2212
}
2313
}
24-
14+
2515
return sb.toString();
2616
}
2717
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class Solution {
22
public int lengthOfLongestSubstring(String s) {
3-
int maxL = 0;
4-
Set<Character> arr = new HashSet<>();
5-
int j = 0;
6-
int i = 0;
7-
while (i<s.length()) {
8-
if (!arr.contains(s.charAt(i))) {
9-
arr.add(s.charAt(i++));
10-
maxL = Math.max(maxL, arr.size());
3+
int maxLen = 0;
4+
int n = s.length();
5+
int start = 0;
6+
int end = 0;
7+
Set<Character> set = new HashSet<>();
8+
9+
while (end < n) {
10+
if (!set.contains(s.charAt(end))) {
11+
set.add(s.charAt(end++));
12+
maxLen = Math.max(maxLen, set.size());
1113
}
1214
else {
13-
arr.remove(s.charAt(j++));
15+
set.remove(s.charAt(start++));
1416
}
1517
}
1618

17-
return maxL;
19+
return maxLen;
1820
}
1921
}

Medium/String to Integer(atoi).java

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,32 @@ public int myAtoi(String str) {
33
if (str.length() == 0) {
44
return 0;
55
}
6-
7-
StringBuilder sb = new StringBuilder();
6+
87
int idx = 0;
98
int n = str.length();
10-
11-
// Removing initial white spaces
9+
int sign = 1;
1210
while (idx < n && str.charAt(idx) == ' ') {
1311
idx++;
1412
}
15-
16-
int sign = 1;
17-
// Updating sign value if there is a '+' or '-' present
18-
if (idx < n) {
19-
if (str.charAt(idx) == '-') {
20-
sign = -1;
21-
idx++;
22-
}
23-
else if (str.charAt(idx) == '+') {
24-
idx++;
25-
}
13+
14+
if (idx < n && (str.charAt(idx) == '-' || str.charAt(idx) == '+')) {
15+
sign = str.charAt(idx) == '-' ? -1 : 1;
16+
idx++;
2617
}
27-
28-
// Integer limits
29-
int positiveLimit = Integer.MAX_VALUE;
30-
int negativeLimit = Integer.MIN_VALUE;
31-
32-
// Adding values to StringBuilder until a digit is found
33-
// Keeping a check for bounds
18+
19+
long num = 0;
3420
while (idx < n && Character.isDigit(str.charAt(idx))) {
35-
sb.append(str.charAt(idx));
36-
if (sign == 1 && Long.parseLong(sb.toString()) > positiveLimit) {
37-
return Integer.MAX_VALUE;
21+
num = num * 10 + Character.getNumericValue(str.charAt(idx++));
22+
23+
if ((sign == 1 && num > Integer.MAX_VALUE) || (-num < Integer.MIN_VALUE)) {
24+
return getOverflowValue(sign);
3825
}
39-
40-
if (sign == -1 && Long.parseLong(sb.toString()) * sign < negativeLimit) {
41-
return Integer.MIN_VALUE;
42-
}
43-
44-
idx++;
4526
}
46-
47-
if (sb.length() == 0) {
48-
return 0;
49-
}
50-
51-
return (int) (Long.parseLong(sb.toString()) * sign);
27+
28+
return sign * (int) num;
29+
}
30+
31+
private int getOverflowValue(int sign) {
32+
return sign == -1 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
5233
}
5334
}

0 commit comments

Comments
 (0)