Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
javadev merged 10 commits into javadev:main from ThanhNIT:tasks-345-349
Dec 11, 2021
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
View file Open in desktop
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--;
}
swap(res, l, r);
l++;
r--;
}
return res.toString();
}
}
View file Open in desktop
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.
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g0301_0400.s0349_intersection_of_two_arrays;

// #Easy #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers

import java.util.ArrayList;
import java.util.List;

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]]) {
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;
}
}
View file Open in desktop
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`
View file Open in desktop
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"));
}
}
View file Open in desktop
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}));
}
}

AltStyle によって変換されたページ (->オリジナル) /