Skip to content

Commit 2ab3667

Browse files
authored
Merge pull request DaleStudy#26 from WhiteHyun/main
[Hyun] Week 1 Solutions
2 parents 7b5ad49 + dcd2851 commit 2ab3667

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// 121. Best Time to Buy and Sell Stock.swift
3+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode121 {
12+
func maxProfit(_ prices: [Int]) -> Int {
13+
if prices.count == 1 { return 0 }
14+
var diff = 0
15+
var left = 0
16+
var right = 0
17+
18+
while right < prices.endIndex {
19+
if prices[left] > prices[right] {
20+
left = right
21+
} else {
22+
diff = max(diff, prices[right] - prices[left])
23+
}
24+
right += 1
25+
}
26+
return diff
27+
}
28+
}

contains-duplicate/WhiteHyun.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// 217. Contains Duplicate.swift
3+
// https://leetcode.com/problems/contains-duplicate/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode217 {
12+
func containsDuplicate(_ nums: [Int]) -> Bool {
13+
Set(nums).count != nums.count
14+
}
15+
}

two-sum/WhiteHyun.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// 1. Two Sum.swift
3+
// https://leetcode.com/problems/two-sum/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode1 {
12+
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] {
13+
let sortedNumbersWithIndex = numbers.enumerated().sorted { lhs, rhs in
14+
lhs.element < rhs.element
15+
}
16+
var left = 0
17+
var right = sortedNumbersWithIndex.endIndex - 1
18+
19+
while left < right {
20+
let sum = sortedNumbersWithIndex[left].element + sortedNumbersWithIndex[right].element
21+
if sum == target { break }
22+
if sum < target {
23+
left += 1
24+
} else {
25+
right -= 1
26+
}
27+
}
28+
29+
return [sortedNumbersWithIndex[left].offset, sortedNumbersWithIndex[right].offset]
30+
}
31+
}

valid-anagram/WhiteHyun.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// 242. Valid Anagram.swift
3+
// https://leetcode.com/problems/valid-anagram/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode242 {
12+
func isAnagram(_ s: String, _ t: String) -> Bool {
13+
Dictionary(s.map { ($0, 1) }, uniquingKeysWith: +) == Dictionary(t.map { ($0, 1) }, uniquingKeysWith: +)
14+
}
15+
}

valid-palindrome/WhiteHyun.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// 125. Valid Palindrome.swift
3+
// https://leetcode.com/problems/valid-palindrome/description/
4+
// Algorithm
5+
//
6+
// Created by 홍승현 on 2024/04/26.
7+
//
8+
9+
import Foundation
10+
11+
final class LeetCode125 {
12+
func isPalindrome(_ s: String) -> Bool {
13+
if let regex = try? NSRegularExpression(pattern: "[a-zA-Z0-9]+") {
14+
let string = regex
15+
.matches(in: s, range: .init(location: 0, length: s.count))
16+
.map { s[Range($0.range, in: s)!].lowercased() }
17+
.joined()
18+
19+
return string == String(string.reversed())
20+
}
21+
22+
return false
23+
}
24+
25+
func isPalindromeUsingRegexString(_ s: String) -> Bool {
26+
var alphabets = ""
27+
for match in s.matches(of: /(?<alphabet>[a-zA-Z0-9]+)/) {
28+
alphabets += match.alphabet.lowercased()
29+
}
30+
return String(alphabets.reversed()) == alphabets
31+
}
32+
}

0 commit comments

Comments
 (0)