diff --git a/3sum/JustHm.swift b/3sum/JustHm.swift new file mode 100644 index 000000000..91a758f45 --- /dev/null +++ b/3sum/JustHm.swift @@ -0,0 +1,28 @@ +class Solution { + // time: O(n2) space: O(n)..? + func threeSum(_ nums: [Int]) -> [[Int]] { + let nums = nums.sorted() // hashmap 방식으로는 안될거 같아 정렬후 순차탐색 하기 + + var answer = Set<[Int]>() // 중복 제거를 위해 Set으로 선언 + for i in nums.indices { + var left = i + 1 + var right = nums.count - 1 + while left < right { + let result = nums[left] + nums[right] + nums[i] + if result == 0 { + answer.insert([nums[i], nums[left], nums[right]]) + // 포인터 옮겨주고 더 검사하기 + right -= 1 + left += 1 + } + else if result > 0 { + right -= 1 + } + else { + left += 1 + } + } + } + return Array(answer) + } +} diff --git a/climbing-stairs/JustHm.swift b/climbing-stairs/JustHm.swift new file mode 100644 index 000000000..ae8d5fcf1 --- /dev/null +++ b/climbing-stairs/JustHm.swift @@ -0,0 +1,47 @@ +class Solution { + func climbStairs(_ n: Int) -> Int { + var arr = [1, 2, 3, 5, 8] + if arr.count > n { return arr[n-1] } + + for i in 4.. [Int] { + var resultFromFirst = [1] + var resultFromLast = [1] + // 기준 원소의 왼쪽 곱은 resultFromFirst에 저장 + // 기준 원소의 오른쪽 곱은 resultFromLast에 저장 + for i in 1.. [Int] { + var result = Array(repeating: 1, count: nums.count) + for i in 1.. Bool { // O(n) + // s 문자열의 문자를 키로 빈도수를 값으로 저장 + var baseDict = Dictionary(s.map{($0,1)}, uniquingKeysWith: +) + + for char in t { // t를 반복하면서 존재하는지 확인, 만약 없다면 return false + guard baseDict[char] != nil, baseDict[char] != 0 + else { return false } + + baseDict[char]? -= 1 + if baseDict[char] == 0 { + baseDict.removeValue(forKey: char) + } + } + return baseDict.isEmpty + } +} + +// MARK: 가장 간단한 방법 time: O(nlogn) +class Solution2 { + func isAnagram(_ s: String, _ t: String) -> Bool { + s.sorted() == t.sorted() + } +} + +// MARK: 실행속도가 가장 빠름 time: O(n) +class Solution3 { + func isAnagram(_ s: String, _ t: String) -> Bool { + guard s.count == t.count else { return false } + + let sCharCount = getCharacterCount(s) + let tCharCount = getCharacterCount(t) + // 반환된 배열이 동일하면 애너그램 + return sCharCount == tCharCount + } + // 배열로 문자 빈도수를 처리 및 저장 + private func getCharacterCount(_ str: String) -> [Int] { + var charCount = [Int](repeating: 0, count: 26) + let aAsciiValue = Character("a").asciiValue ?? 0 + + for char in str.utf8 { + charCount[Int(char - aAsciiValue)] += 1 + } + + return charCount + } +} diff --git a/validate-binary-search-tree/JustHm.swift b/validate-binary-search-tree/JustHm.swift new file mode 100644 index 000000000..6c68f0ce0 --- /dev/null +++ b/validate-binary-search-tree/JustHm.swift @@ -0,0 +1,17 @@ +// time: O(n) +class Solution { + func isValidBST(_ root: TreeNode?) -> Bool { + return checkNodes(root, Int.min, Int.max) + } + + func checkNodes(_ root: TreeNode?, _ min: Int, _ max: Int) -> Bool { + guard let node = root else { return true } + if node.val <= min || node.val >= max { return false } + + return checkNodes(node.left, min, node.val) && checkNodes(node.right, node.val, max) + } +} +/* + 중위순회를 통해서도 해결이 가능하다. + 중위순회를 하며 이전에 들렀던 값보다 현재 방문한 노드의 값이 작으면 false! +*/