-
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
Changes from 3 commits
f013243
de5e813
9ac2472
4d19148
0ab5856
2232855
0324a03
fb87b97
eef5bc5
d8af2d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
static boolean isVowel(char c) { | ||
return (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' | ||
|| c == 'O' || c == 'u' || c == 'U'); | ||
} | ||
|
||
public String reverseVowels(String str) { | ||
int i = 0; | ||
int j = str.length() - 1; | ||
char[] str1 = str.toCharArray(); | ||
while (i < j) { | ||
if (!isVowel(str1[i])) { | ||
i++; | ||
continue; | ||
} | ||
if (!isVowel(str1[j])) { | ||
j--; | ||
continue; | ||
} | ||
|
||
// swapping | ||
char t = str1[i]; | ||
str1[i] = str1[j]; | ||
str1[j] = t; | ||
|
||
i++; | ||
j--; | ||
} | ||
return String.copyValueOf(str1); | ||
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 use this solution. 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(); } } |
||
} | ||
} |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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. |
||
|
||
public class Solution { | ||
public int[] intersection(int[] nums1, int[] nums2) { | ||
List<Integer> intersectionList = new ArrayList<>(); | ||
|
||
for (int j : nums1) { | ||
for (int k : nums2) { | ||
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 use faster solution, this is too slow. |
||
if (j == k && !intersectionList.contains(j)) { | ||
intersectionList.add(j); | ||
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. You may use solution filter. 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. Ok, I will update |
||
} | ||
} | ||
} | ||
int[] result = new int[intersectionList.size()]; | ||
|
||
for (int i = 0; i < intersectionList.size(); i++) { | ||
result[i] = intersectionList.get(i); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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` |
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")); | ||
} | ||
} |
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[] {4, 9})); | ||
} | ||
} |