Skip to content

Commit 7a636df

Browse files
committed
Solution longest-consecutive-sequence
1 parent 545894d commit 7a636df

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)