-
Notifications
You must be signed in to change notification settings - Fork 93
Added tasks 345, 349. #106
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
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f013243
Added task 345
TristianMa de5e813
update task 345, add task 349
TristianMa 9ac2472
fix comments
TristianMa 4d19148
update solution for task 345 and 349
TristianMa 0ab5856
fix comment
ThanhNIT 2232855
format code
ThanhNIT 0324a03
Update Solution.java
ThanhNIT fb87b97
format code
ThanhNIT eef5bc5
Update Solution.java
ThanhNIT d8af2d4
Update Solution.java
ThanhNIT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
src/main/java/g0301_0400/s0345_reverse_vowels_of_a_string/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package g0301_0400.s0345_reverse_vowels_of_a_string; | ||
|
||
// #Easy #String #Two_Pointers | ||
|
||
public class Solution { | ||
private boolean isVowel(char c) { | ||
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' | ||
|| c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'; | ||
} | ||
|
||
private StringBuilder swap(StringBuilder s, int l, int r) { | ||
char tmp = s.charAt(l); | ||
s.setCharAt(l, s.charAt(r)); | ||
s.setCharAt(r, tmp); | ||
return s; | ||
} | ||
|
||
public String reverseVowels(String s) { | ||
int l = 0; | ||
int r = s.length() - 1; | ||
StringBuilder res = new StringBuilder(s); | ||
while (l < r) { | ||
while (l < r && !isVowel(s.charAt(l))) { | ||
l++; | ||
} | ||
while (l < r && !isVowel(s.charAt(r))) { | ||
r--; | ||
} | ||
res = swap(res, l, r); | ||
l++; | ||
r--; | ||
} | ||
return res.toString(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
src/main/java/g0301_0400/s0345_reverse_vowels_of_a_string/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
345\. Reverse Vowels of a String | ||
|
||
Easy | ||
|
||
Given a string `s`, reverse only all the vowels in the string and return it. | ||
|
||
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both cases. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "hello" | ||
|
||
**Output:** "holle" | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "leetcode" | ||
|
||
**Output:** "leotcede" | ||
|
||
**Constraints:** | ||
|
||
* 1 <= s.length <= 3 * 105 | ||
* `s` consist of **printable ASCII** characters. |
23 changes: 23 additions & 0 deletions
src/main/java/g0301_0400/s0349_intersection_of_two_arrays/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package g0301_0400.s0349_intersection_of_two_arrays; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// #Easy #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move comment after package. |
||
|
||
class Solution { | ||
public int[] intersection(int[] nums1, int[] nums2) { | ||
boolean occ[]=new boolean[1001]; | ||
for(int i=0;i<nums1.length;i++) occ[nums1[i]]=true; | ||
List<Integer> res=new ArrayList<>(); | ||
for(int i=0;i<nums2.length;i++){ | ||
if(occ[nums2[i]]){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please format source codes. |
||
occ[nums2[i]]=false; | ||
res.add(nums2[i]); | ||
} | ||
} | ||
int result[]=new int[res.size()]; | ||
for(int i=0;i<res.size();i++) result[i]=res.get(i); | ||
return result; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
src/main/java/g0301_0400/s0349_intersection_of_two_arrays/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
349\. Intersection of Two Arrays | ||
|
||
Easy | ||
|
||
Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must be **unique** and you may return the result in **any order**. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\] | ||
|
||
**Output:** \[2\] | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums1 = \[4,9,5\], nums2 = \[9,4,9,8,4\] | ||
|
||
**Output:** \[9,4\] | ||
|
||
**Explanation:** \[4,9\] is also accepted. | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums1.length, nums2.length <= 1000` | ||
* `0 <= nums1[i], nums2[i] <= 1000` |
18 changes: 18 additions & 0 deletions
src/test/java/g0301_0400/s0345_reverse_vowels_of_a_string/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package g0301_0400.s0345_reverse_vowels_of_a_string; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void reverseVowels() { | ||
assertThat(new Solution().reverseVowels("hello"), equalTo("holle")); | ||
} | ||
|
||
@Test | ||
void reverseVowels2() { | ||
assertThat(new Solution().reverseVowels("leetcode"), equalTo("leotcede")); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
src/test/java/g0301_0400/s0349_intersection_of_two_arrays/SolutionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package g0301_0400.s0349_intersection_of_two_arrays; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
@Test | ||
void intersection() { | ||
assertThat( | ||
new Solution().intersection(new int[] {1, 2, 2, 1}, new int[] {2, 2}), | ||
equalTo(new int[] {2})); | ||
} | ||
|
||
@Test | ||
void intersection2() { | ||
assertThat( | ||
new Solution().intersection(new int[] {4, 9, 5}, new int[] {9, 4, 9, 8, 4}), | ||
equalTo(new int[] {9, 4})); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.