Skip to content

Commit 402655d

Browse files
authored
Merge pull request #1782 from sonjh1217/main
[sonjh1217] WEEK 03 solutions
2 parents 5e7ea55 + 05092cc commit 402655d

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

number-of-1-bits/sonjh1217.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Solution {
2+
func hammingWeightDivide(_ n: Int) -> Int {
3+
var count = 0
4+
var dividedNumber = n
5+
while dividedNumber > 0 {
6+
let remainder = dividedNumber % 2
7+
if remainder == 1 {
8+
count += 1
9+
}
10+
dividedNumber /= 2
11+
}
12+
13+
return count
14+
15+
//시간 복잡도(O(log n))
16+
//공간 복잡도(O(1))
17+
}
18+
19+
func hammingWeightShift(_ n: Int) -> Int {
20+
var count = 0
21+
22+
var shiftedNumber = n
23+
while shiftedNumber > 0 {
24+
if (shiftedNumber & 1) == 1 {
25+
count += 1
26+
}
27+
shiftedNumber >>= 1
28+
}
29+
30+
return count
31+
32+
//시간 복잡도(O(log n))
33+
//공간 복잡도(O(1))
34+
}
35+
36+
func hammingWeightMask(_ n: Int) -> Int {
37+
var count = 0
38+
39+
var mask = 1 << 31
40+
41+
while mask > 0 {
42+
if mask & n == mask {
43+
count += 1
44+
}
45+
mask >>= 1
46+
}
47+
48+
return count
49+
50+
//시간 복잡도(O(1))
51+
//공간 복잡도(O(1))
52+
}
53+
}
54+

valid-palindrome/sonjh1217.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func isPalindrome(_ s: String) -> Bool {
3+
let filtered = s.filter {$0.isLetter || $0.isWholeNumber}.lowercased()
4+
let reversed = String(filtered.reversed())
5+
6+
return filtered == reversed
7+
8+
//시간 복잡도 O(n)
9+
//공간 복잡도 O(n)
10+
}
11+
}
12+

0 commit comments

Comments
 (0)