Skip to content

[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 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
33 changes: 33 additions & 0 deletions combination-sum/HISEHOONAN.swift
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 // 리턴은 결과 배열
}
}
22 changes: 22 additions & 0 deletions number-of-1-bits/HISEHOONAN.swift
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만 추출한 후 그 개수 리턴
}
}
19 changes: 19 additions & 0 deletions valid-palindrome/HISEHOONAN.swift
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
}
Comment on lines +13 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

reutrn validS == String(validS.reversed())

위처럼 써서 조건문을 최소화할 수 있을 것 같아요. 제가 swift를 몰라서 이게 되는진 모르겠지만 다른 언어에서는 일반적으로 가능한 방법이라고 알고 있어 코멘트 남깁니다!

}
}