-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HISEHOONAN] Week 03 solutions #1284
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
3 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,33 @@ | ||
// | ||
// Untitled.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/14/25. | ||
// | ||
|
||
class Solution { | ||
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { | ||
var middleArray : [Int] = [] // 연산중인 배열 | ||
var resultArray : [[Int]] = [] // 결과 배열 | ||
|
||
func recursive(startIndex : Int, Sum : Int){ //재귀함수. 시작index, 합계 | ||
if Sum == target{ //합계가 목표와 같다면 | ||
resultArray.append(middleArray) //결과 배열로 연산배열 append | ||
return //종료 | ||
} | ||
if Sum > target{ // 합계가 목표보다 크다면, | ||
return //그대로 리턴 | ||
} | ||
//핵심 | ||
for i in startIndex..<candidates.count{ // 인덱스 별로 for loop | ||
middleArray.append(candidates[i]) //연산중인 배열에 startIndex의 원소 추가 | ||
recursive(startIndex: i, Sum: Sum+candidates[i]) //재귀실행. i번째 인덱스와, i번째 인덱스의 원소 + 지금까지의 합을 더해서 재귀 함수로 리턴. | ||
middleArray.removeLast() // 백트래킹을 위해 맨 뒤 원소 삭제. | ||
} | ||
} | ||
|
||
recursive(startIndex: 0, Sum: 0) // 최초 재귀함수 호출. 초기화를 위해 0번째 인덱스와 합계 0부터 시작 | ||
print(resultArray) // 디버깅을 위한 출력문 | ||
return resultArray // 리턴은 결과 배열 | ||
} | ||
} |
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,22 @@ | ||
// | ||
// Untitled.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/14/25. | ||
// | ||
|
||
class Solution { | ||
func hammingWeight(_ n: Int) -> Int { | ||
var num = n //n을 저장할 변수 | ||
var remain = 0 //나머지를 저장할 변수 | ||
var array : [Int] = [] //이진법으로 변환한 수를 저장할 배열 | ||
|
||
while num > 0{ //num이 0보다 클때만 반복 | ||
remain = num % 2 //num을 2로 나눈 나머지를 저장 | ||
num = num / 2 //num을 2로 나눈 몫을 저장 | ||
array.append(remain) //array에 나머지를 저장 | ||
} | ||
|
||
return array.filter{$0 == 1}.count //array에 1만 추출한 후 그 개수 리턴 | ||
} | ||
} |
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,19 @@ | ||
// | ||
// Untitled.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/14/25. | ||
// | ||
|
||
class Solution { | ||
func isPalindrome(_ s: String) -> Bool { | ||
var validS = s.lowercased().filter{$0.isNumber == true || $0.isLetter == true} | ||
//s를 모두 소문자로 변환 후 숫자 or 문자가 true인 문자만 validS에 배열로 추출. | ||
|
||
if validS == String(validS.reversed()){ //validS와 뒤집은 것과 같다면 true 아니면 false | ||
return true | ||
}else{ | ||
return false | ||
} | ||
} | ||
} |
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.
위처럼 써서 조건문을 최소화할 수 있을 것 같아요. 제가 swift를 몰라서 이게 되는진 모르겠지만 다른 언어에서는 일반적으로 가능한 방법이라고 알고 있어 코멘트 남깁니다!