-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Hyun] Week 1 Solutions #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5aa5ba0
feat: Add solution for LeetCode problem 217 Contains Duplicate
WhiteHyun e042fa3
feat: Add LeetCode242 solution for Valid Anagram
WhiteHyun 74353b8
feat: add solution for Two Sum problem on LeetCode
WhiteHyun 31faf13
feat: Add solution for LeetCode problem 125
WhiteHyun 4de8394
feat: Add solution for LeetCode problem 121
WhiteHyun dcd2851
move: Move file to correct directory
WhiteHyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// 121. Best Time to Buy and Sell Stock.swift | ||
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ | ||
// Algorithm | ||
// | ||
// Created by 홍승현 on 2024/04/26. | ||
// | ||
|
||
import Foundation | ||
|
||
final class LeetCode121 { | ||
func maxProfit(_ prices: [Int]) -> Int { | ||
if prices.count == 1 { return 0 } | ||
var diff = 0 | ||
var left = 0 | ||
var right = 0 | ||
|
||
while right < prices.endIndex { | ||
if prices[left] > prices[right] { | ||
left = right | ||
} else { | ||
diff = max(diff, prices[right] - prices[left]) | ||
} | ||
right += 1 | ||
} | ||
return diff | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// 217. Contains Duplicate.swift | ||
// https://leetcode.com/problems/contains-duplicate/description/ | ||
// Algorithm | ||
// | ||
// Created by 홍승현 on 2024/04/26. | ||
// | ||
|
||
import Foundation | ||
|
||
final class LeetCode217 { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
Set(nums).count != nums.count | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// 1. Two Sum.swift | ||
// https://leetcode.com/problems/two-sum/description/ | ||
// Algorithm | ||
// | ||
// Created by 홍승현 on 2024/04/26. | ||
// | ||
|
||
import Foundation | ||
|
||
final class LeetCode1 { | ||
func twoSum(_ numbers: [Int], _ target: Int) -> [Int] { | ||
let sortedNumbersWithIndex = numbers.enumerated().sorted { lhs, rhs in | ||
lhs.element < rhs.element | ||
} | ||
var left = 0 | ||
var right = sortedNumbersWithIndex.endIndex - 1 | ||
|
||
while left < right { | ||
let sum = sortedNumbersWithIndex[left].element + sortedNumbersWithIndex[right].element | ||
if sum == target { break } | ||
if sum < target { | ||
left += 1 | ||
} else { | ||
right -= 1 | ||
} | ||
} | ||
|
||
return [sortedNumbersWithIndex[left].offset, sortedNumbersWithIndex[right].offset] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// 242. Valid Anagram.swift | ||
// https://leetcode.com/problems/valid-anagram/description/ | ||
// Algorithm | ||
// | ||
// Created by 홍승현 on 2024/04/26. | ||
// | ||
|
||
import Foundation | ||
|
||
final class LeetCode242 { | ||
func isAnagram(_ s: String, _ t: String) -> Bool { | ||
Dictionary(s.map { ($0, 1) }, uniquingKeysWith: +) == Dictionary(t.map { ($0, 1) }, uniquingKeysWith: +) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// 125. Valid Palindrome.swift | ||
// https://leetcode.com/problems/valid-palindrome/description/ | ||
// Algorithm | ||
// | ||
// Created by 홍승현 on 2024/04/26. | ||
// | ||
|
||
import Foundation | ||
|
||
final class LeetCode125 { | ||
func isPalindrome(_ s: String) -> Bool { | ||
if let regex = try? NSRegularExpression(pattern: "[a-zA-Z0-9]+") { | ||
let string = regex | ||
.matches(in: s, range: .init(location: 0, length: s.count)) | ||
.map { s[Range($0.range, in: s)!].lowercased() } | ||
.joined() | ||
|
||
return string == String(string.reversed()) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func isPalindromeUsingRegexString(_ s: String) -> Bool { | ||
var alphabets = "" | ||
for match in s.matches(of: /(?<alphabet>[a-zA-Z0-9]+)/) { | ||
alphabets += match.alphabet.lowercased() | ||
} | ||
return String(alphabets.reversed()) == alphabets | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR에 친절하게 설명해주셔서 이 코드를 가까스로 이해했네요 ㅋㅋ
면접관이 Swfit를 쓰는지 여부에 따라서 이런 코드는 호불호가 갈릴 수도 있겠다는 생각이 들었습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다음부터는 숏코딩 보다는 읽기 쉬운 코드로 변경해보겠습니다.
조언 감사드립니다. 🙇