File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments