Skip to content

[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 3 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions combination-sum/JustHm.swift
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
}
}
21 changes: 21 additions & 0 deletions number-of-1-bits/JustHm.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알고리즘을 푸는 여러 방식을 고민하신 것 같아서, 저도 배워갑니다. 그리고 사실 Swift를 잘 모르는데, 파이썬만큼 간결한 코드로 문제를 풀 수 있는 것 같아서, 신기하다고 생각했습니다! 이번 주도 고생하셨습니다 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다! 이번에는 남은 문제를 해결을 못해서..

일단 마지막까지 노력해보고 머지해보겠습니다 ㅠㅠ

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
}
}
11 changes: 11 additions & 0 deletions valid-palindrome/JustHm.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 여유가 되신다면, 나중에 투 포인터를 활용해서 풀어보시는 것도 추천드립니다!

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
}
}