-
-
Notifications
You must be signed in to change notification settings - Fork 195
[JustHm] Week 03 #1277
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
[JustHm] Week 03 #1277
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,26 @@ | ||
// 해설 참조.. | ||
class Solution { | ||
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { | ||
// 우선 답변용, 조합 찾기용 변수 생성 | ||
var answer = [[Int]]() | ||
var nums = [Int]() | ||
// 백트래킹 기법으로 로직 작성, | ||
// 현재 원소 위치와 합했을때의 값을 인자로 받음 | ||
func backtracking(start: Int, total: Int) { | ||
// 전처리 | ||
if total > target { return } // total이 target 보다 크면 조합X | ||
else if total == target { return answer.append(nums) } // 같으면 답변용 변수에 추가 | ||
|
||
// 시작 부분부터 값을 하나씩 더해서 재귀로 돌려봄 | ||
for index in start..<candidates.count { | ||
let temp = candidates[index] | ||
nums.append(temp) //먼저 선택된 원소를 조합 배열에 추가 | ||
backtracking(start: index, total: total + temp) // 현재 선택된 원소의 인덱스와 총 합을 인자로 함수 호출 | ||
nums.removeLast() // 조합찾기가 끝나면 종료 | ||
} | ||
} | ||
// 초기부터 시작함 | ||
backtracking(start: 0, total: 0) | ||
return answer | ||
} | ||
} |
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,21 @@ | ||
// time: O(n) | ||
class Solution { | ||
func hammingWeight(_ n: Int) -> Int { | ||
return String(n, radix: 2).filter{$0 == "1"}.count | ||
} | ||
} | ||
// time: O(1), space: O(1) | ||
class AnotherSolution { | ||
func hammingWeight(_ n: Int) -> Int { | ||
var count = 0 | ||
var num = n | ||
// 최대 32비트이기 때문에 반복을 32번 돌며 비트를 순회함 | ||
for _ in 0..<32 { | ||
if num & 1 == 1 { // 가장 오른쪽 비트만 가져와 1인지 확인함 | ||
count += 1 // 비트가 1이면 count 증가 | ||
} | ||
num >>= 1 // 오른쪽으로 1비트 이동 | ||
} | ||
return count | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 여유가 되신다면, 나중에 투 포인터를 활용해서 풀어보시는 것도 추천드립니다! |
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,11 @@ | ||
class Solution { | ||
func isPalindrome(_ s: String) -> Bool { | ||
let answer = s.lowercased().filter { | ||
if $0.isLetter || $0.isNumber { | ||
return true | ||
} | ||
return false | ||
} | ||
return answer == String(answer.reversed()) ? true : 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를 잘 모르는데, 파이썬만큼 간결한 코드로 문제를 풀 수 있는 것 같아서, 신기하다고 생각했습니다! 이번 주도 고생하셨습니다 😊
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.
리뷰 감사합니다! 이번에는 남은 문제를 해결을 못해서..
일단 마지막까지 노력해보고 머지해보겠습니다 ㅠㅠ