From 3272e7f84d35194114202767a00df65d9acf4648 Mon Sep 17 00:00:00 2001 From: HISEHOONAN Date: Tue, 8 Apr 2025 09:22:47 +0900 Subject: [PATCH 1/4] Solutuion Anagram --- valid-anagram/HISEHOONAN.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-anagram/HISEHOONAN.swift diff --git a/valid-anagram/HISEHOONAN.swift b/valid-anagram/HISEHOONAN.swift new file mode 100644 index 000000000..d17dcfa31 --- /dev/null +++ b/valid-anagram/HISEHOONAN.swift @@ -0,0 +1,16 @@ +// +// 218.swift +// Algorithm +// +// Created by 안세훈 on 4/8/25. +// + +//Valid Anagram +class Solution { + func isAnagram(_ s: String, _ t: String) -> Bool { + var sortS = s.sorted() //s를 sort하여 sortS에 저장 + var sortT = t.sorted() //t를 sort하여 sortT에 저장 + + return sortS == sortT ? true : false //sortS와 sortT가 같다면 true 아니면 false + } +} From 8609721773f584ab0ca53df11497d809fe621a62 Mon Sep 17 00:00:00 2001 From: HISEHOONAN Date: Tue, 8 Apr 2025 10:16:31 +0900 Subject: [PATCH 2/4] Solution Climbing Stars --- climbing-stairs/HISEHOONAN.swift | 134 +++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 climbing-stairs/HISEHOONAN.swift diff --git a/climbing-stairs/HISEHOONAN.swift b/climbing-stairs/HISEHOONAN.swift new file mode 100644 index 000000000..ea7ca9fdb --- /dev/null +++ b/climbing-stairs/HISEHOONAN.swift @@ -0,0 +1,134 @@ +// +// 230.swift +// Algorithm +// +// Created by 안세훈 on 4/8/25. +// + +//Climbing Stairs + +class Solution { //다이나믹 프로그래밍. + func climbStairs(_ n: Int) -> Int { + if n < 4 {return n} // 0부터 4보다 작을때 까지는 걍 그 숫자 리턴 + + var dp = [0,1,2,3] //dp에 쓸 기본 배열. + + for i in 4...n{ + dp.append(dp[i-2] + dp[i-1]) // 숫자의 전 + 숫자의 전전 = n의 숫자. + } + return dp[dp.count-1] //마지막 배열 리턴 + } +} + +// 패턴 + +/* + +** 1 or 2계단** +--------------------------- +n = 1 + +1스탭 +--------------------------- +n = 2 +1 + 1 + +2 + +2스탭 +--------------------------- +n = 3 +1 + 1 + 1 + +2 + 1 +1 + 2 + +3스탭 +--------------------------- +n = 4 +1 + 1 + 1 + 1 + +2 + 1 + 1 +1 + 2 + 1 +1 + 1 + 2 + +2 + 2 + +5스탭 +n + 1 +--------------------------- +n = 5 +1 + 1 + 1 + 1 + 1 + +2 + 1 + 1 + 1 +1 + 2 + 1 + 1 +1 + 1 + 2 + 1 +1 + 1 + 1 + 2 + +2 + 2 + 1 +2 + 1 + 2 +1 + 2 + 2 + +8스탭 +n + 3 +--------------------------- +n = 6 +1 + 1 + 1 + 1 + 1 + 1 + +2 + 1 + 1 + 1 + 1 +1 + 2 + 1 + 1 + 1 +1 + 1 + 2 + 1 + 1 +1 + 1 + 1 + 2 + 1 +1 + 1 + 1 + 1 + 2 + +2 + 2 + 1 + 1 +2 + 1 + 2 + 1 +2 + 1 + 1 + 2 + +1 + 2 + 2 + 1 +1 + 2 + 1 + 2 + +1 + 1 + 2 + 2 + +2 + 2 + 2 + +13 스탭 +--------------------------- +n = 7 +1 + 1 + 1 + 1 + 1 + 1 + 1 + +2 + 1 + 1 + 1 + 1 + 1 +1 + 2 + 1 + 1 + 1 + 1 +1 + 1 + 2 + 1 + 1 + 1 +1 + 1 + 1 + 2 + 1 + 1 +1 + 1 + 1 + 1 + 2 + 1 +1 + 1 + 1 + 1 + 1 + 2 + +2 + 2 + 1 + 1 + 1 +2 + 1 + 2 + 1 + 1 +2 + 1 + 1 + 2 + 1 +2 + 1 + 1 + 1 + 2 + +1 + 2 + 2 + 1 + 1 +1 + 2 + 1 + 2 + 1 +1 + 2 + 1 + 1 + 2 + +1 + 1 + 2 + 2 + 1 +1 + 1 + 2 + 1 + 2 + +1 + 1 + 1 + 2 + 2 + +2 + 2 + 2 + 1 +2 + 2 + 1 + 2 +2 + 1 + 2 + 2 + +1 + 2 + 2 + 2 + +21개 + +n = 1 2 3 | 4 5 6 7 +cnt = 1, 2, 3,| 5, 8, 13, 21 + + +? 피보나치자나? +*/ From 5d5255c1f5a3b6a2ffc054bbbf41b5ad665e9925 Mon Sep 17 00:00:00 2001 From: HISEHOONAN Date: Wed, 9 Apr 2025 09:36:27 +0900 Subject: [PATCH 3/4] Solution productExceptSelf --- product-of-array-except-self/HISEHOONAN.swift | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 product-of-array-except-self/HISEHOONAN.swift diff --git a/product-of-array-except-self/HISEHOONAN.swift b/product-of-array-except-self/HISEHOONAN.swift new file mode 100644 index 000000000..51a1ff412 --- /dev/null +++ b/product-of-array-except-self/HISEHOONAN.swift @@ -0,0 +1,46 @@ +// +// 239.swift +// Algorithm +// +// Created by 안세훈 on 4/8/25. +// + +//Product of Array Except Self +class Solution { + func productExceptSelf(_ nums: [Int]) -> [Int] { + + var array1 : [Int] = nums // 원래 배열 + var array2 : [Int] = nums.reversed() // 뒤집은 배열 + + var array1Forloop : [Int] = [] // 원래 배열을 계산 후 저장할 배열 + var array2Forloop : [Int] = [] // 뒤집은 배열을 계산 후 저장할 배열 + + var multiply = 1 // 연산용 + + var result : [Int] = [] // 최종 결과를 담을 배열 + + // 원래 누적 곱 계산 (자기 자신 제외) + for num in array1 { + array1Forloop.append(multiply) // 현재까지의 누적 곱을 저장 (시작은1) + multiply = num * multiply // 누적 곱 업데이트 + } + + multiply = 1 //뒤집은 배열 계산을 위해 초기화 + + // 뒤집은 배열 누적 곱 계산 (자기 자신 제외) + for num in array2 { + array2Forloop.append(multiply) // 현재까지의 누적 곱을 저장 + multiply = num * multiply // 누적 곱 업데이트 + } + + array2Forloop = array2Forloop.reversed()// 뒤집은 배열 곱을 원래 순서로 되돌림 + + // 원래 배열 곱과 뒤집은 배열 곱의 인덱스가 같은놈들끼리 + // 곱해서 최종 결과 생성 + for i in 0.. Date: Sat, 12 Apr 2025 15:53:36 +0900 Subject: [PATCH 4/4] solution 3sum,valid-binary-search-tree --- 3sum/HISEHOONAN.swift | 48 ++++++++++++++++++++ validate-binary-search-tree/HISEHOONAN.swift | 40 ++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 3sum/HISEHOONAN.swift create mode 100644 validate-binary-search-tree/HISEHOONAN.swift diff --git a/3sum/HISEHOONAN.swift b/3sum/HISEHOONAN.swift new file mode 100644 index 000000000..07d2d55e0 --- /dev/null +++ b/3sum/HISEHOONAN.swift @@ -0,0 +1,48 @@ +// +// 241.swift +// Algorithm +// +// Created by 안세훈 on 4/8/25. +// + +//3Sum +class Solution { //정렬 + two pointer + func threeSum(_ nums: [Int]) -> [[Int]] { + let nums = nums.sorted() //배열을 오름차순으로 정렬 + var result: [[Int]] = [] // 결과를 저장할 배열. + + for i in 0.. 0 && nums[i] == nums[i - 1] { + continue + } + + var left = i + 1 //left는 i+1번째 인덱스 + var right = nums.count - 1 //right는 배열의 끝번째 인덱스 + + while left < right { + let sum = nums[i] + nums[left] + nums[right] + + if sum == 0 { + result.append([nums[i], nums[left], nums[right]]) + + // 중복 제거 + while left < right && nums[left] == nums[left + 1] { + left += 1 + } + while left < right && nums[right] == nums[right - 1] { + right -= 1 + } + + left += 1 + right -= 1 + } else if sum < 0 { + left += 1 + } else { + right -= 1 + } + } + } + + return result + } +} diff --git a/validate-binary-search-tree/HISEHOONAN.swift b/validate-binary-search-tree/HISEHOONAN.swift new file mode 100644 index 000000000..7406b5160 --- /dev/null +++ b/validate-binary-search-tree/HISEHOONAN.swift @@ -0,0 +1,40 @@ +// +// 251.swift +// Algorithm +// +// Created by 안세훈 on 4/8/25. +// + +//Validate Binary Search Tree + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ + +class Solution { + func isValidBST(_ root: TreeNode?) -> Bool { + return validate(root, min: nil, max: nil) + } + + private func validate(_ node: TreeNode?, min: Int?, max: Int?) -> Bool { + guard let node = node else { return true } + + if let min = min, node.val <= min { return false } + if let max = max, node.val >= max { return false } + + return validate(node.left, min: min, max: node.val) && + validate(node.right, min: node.val, max: max) + } +}