Skip to content

Commit 983c956

Browse files
authored
Merge pull request #2069 from ymir0804/main
[ymir0804] WEEK 02 solutions
2 parents 432ad65 + d960abe commit 983c956

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

house-robber/ymir0804.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
int money = 0;
4+
for (int i = 0; i < nums.length; i++) {
5+
if (i % 2 == 0) {
6+
money += nums[i];
7+
}
8+
}
9+
return money;
10+
}
11+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
HashSet<Integer> numsSet = new HashSet<>();
6+
int maxNum = 0;
7+
for (int num : nums) {
8+
numsSet.add(num);
9+
}
10+
11+
if (numsSet.isEmpty()) {
12+
return 1;
13+
} else if (numsSet.size() == 1) {
14+
return 0;
15+
}
16+
17+
for (int num : numsSet) {
18+
boolean isStartPoint = !numsSet.contains(num - 1);
19+
if (isStartPoint) {
20+
int current = num;
21+
int length = 1;
22+
while (numsSet.contains(++current)) {
23+
length++;
24+
}
25+
if (length > maxNum) {
26+
maxNum = length;
27+
}
28+
}
29+
}
30+
return maxNum;
31+
}
32+
}

valid-anagram/ymir0804.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Set;
2+
import java.util.stream.Collectors;
3+
4+
class Solution {
5+
public boolean isAnagram(String s, String t) {
6+
Set<Character> characterS = s.chars()
7+
.mapToObj(c -> (char) c)
8+
.collect(Collectors.toSet());
9+
Set<Character> characterT = t.chars()
10+
.mapToObj(c -> (char) c)
11+
.collect(Collectors.toSet());
12+
return characterS.equals(characterT);
13+
}
14+
}

0 commit comments

Comments
 (0)