Skip to content

Commit dcbae76

Browse files
committed
Modified 2 solutions
1 parent f0fa969 commit dcbae76

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
class Solution {
2-
public int peakIndexInMountainArray(int[] A) {
3-
return peakIndexInMountainArrayImpl(A, 0, A.length-1);
2+
public int peakIndexInMountainArray(int[] A) {
3+
int low = 0;
4+
int high = A.length - 1;
5+
while (low < high) {
6+
int mid = low + (high - low) / 2;
7+
if (A[mid] < A[mid + 1]) {
8+
low = mid + 1;
9+
}
10+
else {
11+
high = mid;
12+
}
413
}
5-
6-
private int peakIndexInMountainArrayImpl(int[] arr, int start, int end) {
7-
if (start > end) return -1;
8-
9-
int mid = (start + end)/2;
10-
11-
if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]) {
12-
return mid;
13-
}
14-
else if (arr[mid] < arr[mid-1]) {
15-
return peakIndexInMountainArrayImpl(arr, start, mid-1);
16-
}
17-
else {
18-
return peakIndexInMountainArrayImpl(arr, mid+1, end);
19-
}
20-
}
14+
return low;
15+
}
2116
}

Easy/Unique Morse Code Words.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
class Solution {
2-
public static int uniqueMorseRepresentations(String[] words) {
3-
String[] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
4-
Set<String> set = new HashSet<>();
5-
6-
for (String word : words) {
7-
StringBuilder sb = new StringBuilder();
8-
9-
for (char c : word.toCharArray()) {
10-
sb.append(codes[c-'a']);
11-
}
12-
13-
set.add(sb.toString());
14-
}
15-
16-
return set.size();
2+
public int uniqueMorseRepresentations(String[] words) {
3+
String[] morseCodes = {
4+
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",
5+
".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
6+
};
7+
Set<String> set = new HashSet<>();
8+
for (String word : words) {
9+
StringBuilder sb = new StringBuilder();
10+
for (char c : word.toCharArray()) {
11+
sb.append(morseCodes[c - 'a']);
12+
}
13+
set.add(sb.toString());
1714
}
15+
return set.size();
16+
}
1817
}

0 commit comments

Comments
 (0)