File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var longestConsecutive = function ( nums ) {
6+ // set์ผ๋ก ์ค๋ณต๋ element ์ ๊ฑฐ
7+ const numSet = new Set ( nums ) ;
8+ // ์ต๋ ์ฐ์ ๊ธธ์ด ๋ณ์
9+ let maxLength = 0 ;
10+
11+ // ์ต๋ ์ฐ์ ๊ธธ์ด ์ฐพ๊ธฐ
12+ for ( let num of numSet ) {
13+ // ๋ง์ฝ ํ์ฌ ์ -1์ด ์๋ค๋ฉด?
14+ if ( ! numSet . has ( num - 1 ) ) {
15+ let currentNum = num ; //ํ์ฌ๊ฐ์ด ์์
16+ let currentLength = 1 ; //์ต๋ ๊ธธ์ด 1๋ก ์ด๊ธฐํ
17+
18+ // ํ์ฌ๊ฐ +1์ด ์์ ๋ ๊น์ง ๋ฐ๋ณต
19+ while ( numSet . has ( currentNum + 1 ) ) {
20+ currentNum ++ ;
21+ currentLength ++ ;
22+ }
23+
24+ // ์ต๋๊ธธ์ด ๊ฐ๊ณผ ํ์ฌ ๊ธธ์ด๊ฐ์ค ๋ ๋์๊ฒ์ด ์ต๋๊ธธ์ด๊ฐ
25+ maxLength = Math . max ( maxLength , currentLength ) ;
26+ }
27+ }
28+ return maxLength ;
29+ } ;
You canโt perform that action at this time.
0 commit comments