File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func hammingWeight( _ n: Int ) -> Int {
3
+ var number = n
4
+ var answer = 0
5
+ while number > 0 {
6
+ if number & 1 == 1 {
7
+ answer += 1
8
+ }
9
+ number = number >> 1
10
+ }
11
+ return answer
12
+ }
13
+ }
14
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func isPalindrome( _ s: String ) -> Bool {
3
+ let string = s. replacingOccurrences ( of: " " , with: " " ) . lowercased ( ) . map { $0 }
4
+ var leftIndex = string. startIndex
5
+ var rightIndex = string. endIndex - 1
6
+ let letterRange : ClosedRange < UInt8 > = 97 ... 122
7
+ let numberRange : ClosedRange < UInt8 > = 48 ... 57
8
+ while leftIndex <= rightIndex {
9
+ guard let leftCharAscii = string [ leftIndex] . asciiValue else { break }
10
+ guard let rightCharAscii = string [ rightIndex] . asciiValue else { break }
11
+ if !letterRange. contains ( leftCharAscii) && !numberRange. contains ( leftCharAscii) {
12
+ leftIndex += 1
13
+ continue
14
+ }
15
+ if !letterRange. contains ( rightCharAscii) && !numberRange. contains ( rightCharAscii) {
16
+ rightIndex -= 1
17
+ continue
18
+ }
19
+ if leftCharAscii == rightCharAscii {
20
+ leftIndex += 1
21
+ rightIndex -= 1
22
+ } else {
23
+ return false
24
+ }
25
+ }
26
+ return true
27
+ }
28
+ }
29
+
You can’t perform that action at this time.
0 commit comments