Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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를 몰라서 이게 되는진 모르겠지만 다른 언어에서는 일반적으로 가능한 방법이라고 알고 있어 코멘트 남깁니다!

}
}