Skip to content

Commit 47b8b32

Browse files
committed
solve problems
1 parent 0e513b7 commit 47b8b32

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

number-of-1-bits/delight010.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+

valid-palindrome/delight010.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+

0 commit comments

Comments
 (0)