diff --git a/house-robber/ymir0804.java b/house-robber/ymir0804.java new file mode 100644 index 0000000000..1faa7077e1 --- /dev/null +++ b/house-robber/ymir0804.java @@ -0,0 +1,11 @@ +class Solution { + public int rob(int[] nums) { + int money = 0; + for (int i = 0; i < nums.length; i++) { + if (i % 2 == 0) { + money += nums[i]; + } + } + return money; + } +} diff --git a/longest-consecutive-sequence/ymir0804.java b/longest-consecutive-sequence/ymir0804.java new file mode 100644 index 0000000000..e042024764 --- /dev/null +++ b/longest-consecutive-sequence/ymir0804.java @@ -0,0 +1,32 @@ +import java.util.HashSet; + +class Solution { + public int longestConsecutive(int[] nums) { + HashSet numsSet = new HashSet<>(); + int maxNum = 0; + for (int num : nums) { + numsSet.add(num); + } + + if (numsSet.isEmpty()) { + return 1; + } else if (numsSet.size() == 1) { + return 0; + } + + for (int num : numsSet) { + boolean isStartPoint = !numsSet.contains(num - 1); + if (isStartPoint) { + int current = num; + int length = 1; + while (numsSet.contains(++current)) { + length++; + } + if (length > maxNum) { + maxNum = length; + } + } + } + return maxNum; + } +} diff --git a/valid-anagram/ymir0804.java b/valid-anagram/ymir0804.java new file mode 100644 index 0000000000..cdf6dc6ae9 --- /dev/null +++ b/valid-anagram/ymir0804.java @@ -0,0 +1,14 @@ +import java.util.Set; +import java.util.stream.Collectors; + +class Solution { + public boolean isAnagram(String s, String t) { + Set characterS = s.chars() + .mapToObj(c -> (char) c) + .collect(Collectors.toSet()); + Set characterT = t.chars() + .mapToObj(c -> (char) c) + .collect(Collectors.toSet()); + return characterS.equals(characterT); + } +}