File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public int longestConsecutive (int [] nums ) {
5
+ Set <Integer > numSet = new HashSet <>();
6
+ for (int num : nums ) {
7
+ numSet .add (num );
8
+ }
9
+
10
+ int longNum = 0 ;
11
+
12
+ // κ° μ«μμ λν΄ μνμ€ μμ μ¬λΆλ₯Ό νμΈ
13
+ for (int num : numSet ) {
14
+ // num-1μ΄ μλ κ²½μ°μλ§ μνμ€λ₯Ό μμ
15
+ if (!numSet .contains (num - 1 )) {
16
+ int currentNum = num ;
17
+ int currentLong = 1 ;
18
+
19
+ // μ°μλ μ«μλ₯Ό νμ
20
+ while (numSet .contains (currentNum + 1 )) {
21
+ currentNum ++;
22
+ currentLong ++;
23
+ }
24
+
25
+ // κ°μ₯ κΈ΄ μνμ€λ₯Ό κ°±μ
26
+ longNum = Math .max (longNum , currentLong );
27
+ }
28
+ }
29
+
30
+ return longNum ;
31
+ }
32
+ }
You canβt perform that action at this time.
0 commit comments