File tree Expand file tree Collapse file tree 4 files changed +117
-0
lines changed
longest-consecutive-sequence
product-of-array-except-self Expand file tree Collapse file tree 4 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ //
2+ // 15. 3Sum
3+ // https://leetcode.com/problems/3sum/description/
4+ // Dale-Study
5+ //
6+ // Created by WhiteHyun on 2024/06/04.
7+ //
8+
9+ class Solution {
10+ func threeSum( _ nums: [ Int ] ) -> [ [ Int ] ] {
11+ var result : [ [ Int ] ] = [ ]
12+ let sorted = nums. sorted ( )
13+
14+ for (index, element) in sorted. enumerated ( ) where index <= 0 || element != sorted [ index - 1 ] {
15+ var left = index + 1
16+ var right = sorted. count - 1
17+
18+ while left < right {
19+ let threeSum = element + sorted[ left] + sorted[ right]
20+ if threeSum > 0 {
21+ right -= 1
22+ continue
23+ }
24+ if threeSum < 0 {
25+ left += 1
26+ continue
27+ }
28+
29+ result. append ( [ element, sorted [ left] , sorted [ right] ] )
30+ left += 1
31+
32+ while sorted [ left] == sorted [ left - 1 ] && left < right {
33+ left += 1
34+ }
35+ }
36+ }
37+
38+
39+ return result
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ //
2+ // 128. Longest Consecutive Sequence
3+ // https://leetcode.com/problems/longest-consecutive-sequence/description/
4+ // Dale-Study
5+ //
6+ // Created by WhiteHyun on 2024/06/01.
7+ //
8+
9+ final class Solution {
10+ func longestConsecutive( _ nums: [ Int ] ) -> Int {
11+ let set = Set ( nums)
12+ var best = 0
13+ for number in set where !set. contains ( number - 1 ) {
14+ var next = number + 1
15+ while set. contains ( next) {
16+ next += 1
17+ }
18+ if best < next - number {
19+ best = next - number
20+ }
21+ }
22+
23+ return best
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ //
2+ // 238. Product of Array Except Self
3+ // https://leetcode.com/problems/product-of-array-except-self/description/
4+ // Dale-Study
5+ //
6+ // Created by WhiteHyun on 2024/06/01.
7+ //
8+
9+ final class Solution {
10+ func productExceptSelf( _ nums: [ Int ] ) -> [ Int ] {
11+ var answer : [ Int ] = . init( repeating: 1 , count: nums. count)
12+
13+ var left_product = 1
14+ for i in nums. indices {
15+ answer [ i] = left_product
16+ left_product *= nums [ i]
17+ }
18+
19+ var right_product = 1
20+ for i in nums. indices. reversed ( ) {
21+ answer [ i] *= right_product
22+ right_product *= nums [ i]
23+ }
24+
25+ return answer
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ //
2+ // 347. Top K Frequent Elements
3+ // https://leetcode.com/problems/top-k-frequent-elements/description/
4+ // Dale-Study
5+ //
6+ // Created by WhiteHyun on 2024/06/01.
7+ //
8+
9+ final class Solution {
10+ func topKFrequent( _ nums: [ Int ] , _ k: Int ) -> [ Int ] {
11+ // Python의 Counter 모듈처럼 만드는 것과 동일
12+ var counter : [ Int : Int ] = [ : ]
13+ for number in nums {
14+ counter [ number, default: 0 ] += 1
15+ }
16+
17+ return counter
18+ . sorted { lhs, rhs in
19+ lhs. value > rhs. value // 빈도 수를 기준으로 내림차순 정렬
20+ }
21+ . prefix ( k) // 앞에서부터 k만큼 Subarray로 가져옴
22+ . map ( \. key) // 제일 빈도가 많았던 것의 숫자를 가져옴 (Dictionary의 Key값)
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments