Skip to content

Commit 2005cf7

Browse files
realDuYuanChaogithub-actions
andauthored
update leetcode (examplehub#145)
* valid-anagram * missing number * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent a220359 commit 2005cf7

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import com.examplehub.maths.SumToNFormula;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/** https://leetcode.com/problems/missing-number/ */
9+
public class MissingNumber {
10+
public static int solution1(int... nums) {
11+
Arrays.sort(nums);
12+
for (int i = 0; i < nums.length; ++i) {
13+
if (nums[i] != i) {
14+
return i;
15+
}
16+
}
17+
return nums.length;
18+
}
19+
20+
public static int solution2(int... nums) {
21+
Set<Integer> set = new HashSet<>();
22+
for (int num : nums) {
23+
set.add(num);
24+
}
25+
for (int i = 0; i < nums.length; ++i) {
26+
if (!set.contains(i)) {
27+
return i;
28+
}
29+
}
30+
return nums.length;
31+
}
32+
33+
public static int solution3(int... nums) {
34+
int missingNumber = nums.length;
35+
for (int i = 0; i < nums.length; ++i) {
36+
missingNumber ^= nums[i] ^ i;
37+
}
38+
return missingNumber;
39+
}
40+
41+
public static int solution4(int... nums) {
42+
int bigSum = SumToNFormula.sum(nums.length);
43+
int smallSum = 0;
44+
for (int num : nums) {
45+
smallSum += num;
46+
}
47+
return bigSum - smallSum;
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import java.util.Arrays;
4+
5+
/** https://leetcode.com/problems/valid-anagram/ */
6+
public class ValidAnagram {
7+
public static boolean solution1(String s, String t) {
8+
if (s.length() != t.length()) {
9+
return false;
10+
}
11+
char[] firstChars = s.toCharArray();
12+
char[] secondChars = t.toCharArray();
13+
Arrays.sort(firstChars);
14+
Arrays.sort(secondChars);
15+
return Arrays.equals(firstChars, secondChars);
16+
}
17+
18+
public static boolean solution2(String s, String t) {
19+
if (s.length() != t.length()) {
20+
return false;
21+
}
22+
StringBuilder builder = new StringBuilder(s);
23+
for (char ch : t.toCharArray()) {
24+
int index = builder.indexOf(ch + "");
25+
if (index != -1) {
26+
builder.deleteCharAt(index);
27+
}
28+
}
29+
return builder.length() == 0;
30+
}
31+
32+
public static boolean solution3(String s, String t) {
33+
if (s.length() != t.length()) {
34+
return false;
35+
}
36+
int[] hashTable = new int[26];
37+
for (char ch : s.toCharArray()) {
38+
hashTable[ch - 'a']++;
39+
}
40+
for (char ch : t.toCharArray()) {
41+
int index = ch - 'a';
42+
hashTable[index]--;
43+
if (hashTable[index] < 0) {
44+
return false;
45+
}
46+
}
47+
return true;
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class MissingNumberTest {
8+
@Test
9+
void testSolution1() {
10+
assertEquals(2, MissingNumber.solution1(3, 0, 1));
11+
assertEquals(2, MissingNumber.solution1(0, 1));
12+
assertEquals(8, MissingNumber.solution1(9, 6, 4, 2, 3, 5, 7, 0, 1));
13+
}
14+
15+
@Test
16+
void testSolution2() {
17+
assertEquals(2, MissingNumber.solution2(3, 0, 1));
18+
assertEquals(2, MissingNumber.solution2(0, 1));
19+
assertEquals(8, MissingNumber.solution2(9, 6, 4, 2, 3, 5, 7, 0, 1));
20+
}
21+
22+
@Test
23+
void testSolution3() {
24+
assertEquals(2, MissingNumber.solution3(3, 0, 1));
25+
assertEquals(2, MissingNumber.solution3(0, 1));
26+
assertEquals(8, MissingNumber.solution3(9, 6, 4, 2, 3, 5, 7, 0, 1));
27+
}
28+
29+
@Test
30+
void testSolution4() {
31+
assertEquals(2, MissingNumber.solution4(3, 0, 1));
32+
assertEquals(2, MissingNumber.solution4(0, 1));
33+
assertEquals(8, MissingNumber.solution4(9, 6, 4, 2, 3, 5, 7, 0, 1));
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ValidAnagramTest {
8+
@Test
9+
void testSolution1() {
10+
assertTrue(ValidAnagram.solution1("anagram", "nagaram"));
11+
assertFalse(ValidAnagram.solution1("rat", "car"));
12+
}
13+
14+
@Test
15+
void testSolution2() {
16+
assertTrue(ValidAnagram.solution2("anagram", "nagaram"));
17+
assertFalse(ValidAnagram.solution2("rat", "car"));
18+
assertFalse(ValidAnagram.solution2("a", "ab"));
19+
}
20+
21+
@Test
22+
void testSolution3() {
23+
assertTrue(ValidAnagram.solution3("anagram", "nagaram"));
24+
assertFalse(ValidAnagram.solution3("rat", "car"));
25+
assertFalse(ValidAnagram.solution3("a", "ab"));
26+
}
27+
}

0 commit comments

Comments
 (0)