Skip to content

update java solution #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Feel free to raise issues or post in discussions.
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down Expand Up @@ -301,7 +301,7 @@ Feel free to raise issues or post in discussions.
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
18 changes: 18 additions & 0 deletions [C]hashmap/[C]hashmap/387-first-unique-character-in-a-string.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int firstUniqChar(String s) {
HashMap<Character, Integer> charToFreq = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
charToFreq.compute(s.charAt(i), (k, v) -> v == null ? 1 : v + 1);
}
for (int i = 0; i < s.length(); i++) {
if (Objects.equals(charToFreq.get(s.charAt(i)), 1)) {
return i;
}
}
return - 1;
}
}

// time O(n), due to traverse twice
// space O(1), due to hashmap (26 letters)
// using hashmap and store sth’s freq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left += 1;
right -= 1;
}
return;
}
}

// time O(n)
// space O(1)
// using array and two pointers opposite direction and shrink type