Skip to content

Commit 71260b2

Browse files
authored
Merge pull request #61 from tiationg-kho/update
update java solution
2 parents e8ea8b3 + f6f3bdc commit 71260b2

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Feel free to raise issues or post in discussions.
125125
| 103 | O | [383. Ransom Note](https://leetcode.com/problems/ransom-note/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/383-ransom-note.py) [Java]([C]hashmap/[C]hashmap/383-ransom-note.java) |
126126
| 104 | O | [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/242-valid-anagram.py) [Java]([C]hashmap/[C]hashmap/242-valid-anagram.java) |
127127
| 105 | O | [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/409-longest-palindrome.py) [Java]([C]hashmap/[C]hashmap/409-longest-palindrome.java) |
128-
| 106 | | [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/387-first-unique-character-in-a-string.py) |
128+
| 106 | | [387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/387-first-unique-character-in-a-string.py) [Java]([C]hashmap/[C]hashmap/387-first-unique-character-in-a-string.java) |
129129
| 107 | | [767. Reorganize String](https://leetcode.com/problems/reorganize-string/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/767-reorganize-string.py) |
130130
| 108 | | [266. Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/266-palindrome-permutation.py) |
131131
| 109 | | [2423. Remove Letter To Equalize Frequency](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [C]hashmap | store sth’s freq | [Python]([C]hashmap/[C]hashmap/2423-remove-letter-to-equalize-frequency.py) |
@@ -301,7 +301,7 @@ Feel free to raise issues or post in discussions.
301301
| 279 | | [948. Bag of Tokens](https://leetcode.com/problems/bag-of-tokens/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/948-bag-of-tokens.py) |
302302
| 280 | O | [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/125-valid-palindrome.py) [Java]([H]array/[H]array-two-pointers-opposite-direction/125-valid-palindrome.java) |
303303
| 281 | | [680. Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/680-valid-palindrome-ii.py) |
304-
| 282 | | [344. Reverse String](https://leetcode.com/problems/reverse-string/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/344-reverse-string.py) |
304+
| 282 | | [344. Reverse String](https://leetcode.com/problems/reverse-string/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/344-reverse-string.py) [Java]([H]array/[H]array-two-pointers-opposite-direction/344-reverse-string.java) |
305305
| 283 | | [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [H]array-two-pointers-opposite-direction | use shrink type | [Python]([H]array/[H]array-two-pointers-opposite-direction/151-reverse-words-in-a-string.py) |
306306
| 284 | O | [5. Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [H]array-two-pointers-opposite-direction | use expand type | [Python]([H]array/[H]array-two-pointers-opposite-direction/5-longest-palindromic-substring.py) [Java]([H]array/[H]array-two-pointers-opposite-direction/5-longest-palindromic-substring.java) |
307307
| 285 | O | [179. Largest Number](https://leetcode.com/problems/largest-number/) | [H]array-sort | use self defined sort | [Python]([H]array/[H]array-sort/179-largest-number.py) |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int firstUniqChar(String s) {
3+
HashMap<Character, Integer> charToFreq = new HashMap<>();
4+
for (int i = 0; i < s.length(); i++) {
5+
charToFreq.compute(s.charAt(i), (k, v) -> v == null ? 1 : v + 1);
6+
}
7+
for (int i = 0; i < s.length(); i++) {
8+
if (Objects.equals(charToFreq.get(s.charAt(i)), 1)) {
9+
return i;
10+
}
11+
}
12+
return - 1;
13+
}
14+
}
15+
16+
// time O(n), due to traverse twice
17+
// space O(1), due to hashmap (26 letters)
18+
// using hashmap and store sth’s freq
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public void reverseString(char[] s) {
3+
int left = 0;
4+
int right = s.length - 1;
5+
while (left < right) {
6+
char temp = s[left];
7+
s[left] = s[right];
8+
s[right] = temp;
9+
left += 1;
10+
right -= 1;
11+
}
12+
return;
13+
}
14+
}
15+
16+
// time O(n)
17+
// space O(1)
18+
// using array and two pointers opposite direction and shrink type

0 commit comments

Comments
 (0)