We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f7bf00a commit 27d89c9Copy full SHA for 27d89c9
longest-consecutive-sequence/donghyeon95.java
@@ -0,0 +1,34 @@
1
+import java.util.HashSet;
2
+
3
+public class Solution {
4
+ public int longestConsecutive(int[] nums) {
5
+ HashSet<Integer> set = new HashSet<>();
6
+ for (int num : nums) {
7
+ set.add(num);
8
+ }
9
10
+ int maxStreak = 0;
11
12
13
+ // 내가 시작 값이라면
14
+ if (!set.contains(num - 1)) {
15
+ int currentNum = num;
16
+ int currentStreak = 1;
17
18
+ // 나로부터 연결되는 값을 찾는다.
19
+ while (set.contains(currentNum + 1)) {
20
+ currentNum++;
21
+ currentStreak++;
22
23
24
+ maxStreak = Math.max(maxStreak, currentStreak);
25
26
27
28
+ return maxStreak;
29
30
+}
31
32
33
34
0 commit comments