File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // Longest_Consecutive_Sequence.swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+
8
+ ///https://leetcode.com/problems/longest-consecutive-sequence/
9
+
10
+
11
+ class Solution {
12
+ func longestConsecutive( _ nums: [ Int ] ) -> Int {
13
+ guard nums. count != 0 else { return 0 }
14
+
15
+ var sortednums = Array ( Set ( nums) ) . sorted ( ) //중복제거 + 오름차순 정렬
16
+ var maxcount = 1 //최대 카운드
17
+ var count = 1 // 연산용 카운트
18
+
19
+ for i in 0 ..< sortednums. count - 1 {
20
+ if sortednums [ i] == sortednums [ i+ 1 ] - 1 {
21
+ //i번째 정렬된 리스트 와 i+1번째 리스트의 값 - 1이 같을 경우
22
+ count += 1 //카운트에 1+
23
+ maxcount = max ( maxcount, count) //maxcount는 연산과, max중 큰 것
24
+ } else {
25
+ count = 1 //아니면 1로 초기화.
26
+ }
27
+ }
28
+
29
+ return maxcount
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments