-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HISEHOONAN] WEEK 02 solutions #1236
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
4 commits
Select commit
Hold shift + click to select a range
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,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..<nums.count { // i를 0번째 배열부터 순회. | ||
if i > 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 | ||
} | ||
} |
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,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 | ||
|
||
|
||
? 피보나치자나? | ||
*/ |
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,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..<nums.count { | ||
result.append(array1Forloop[i] * array2Forloop[i]) | ||
} | ||
|
||
return result | ||
} | ||
} |
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,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 | ||
} | ||
} |
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,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) | ||
} | ||
} |
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.
전과 전전만 있으면 될것같아서 n < 3 조건으로 해도 가능할것같아요!
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.
앗 그렇네요 ! 감사합니다 !