update hammingWeight
This commit is contained in:
parent
04e76f1879
commit
d7d9603bbc
2 changed files with 2 additions and 10 deletions
|
|
@ -9,15 +9,7 @@ import Foundation
|
||||||
|
|
||||||
class LeetCode {
|
class LeetCode {
|
||||||
func hammingWeight(_ n: Int) -> Int {
|
func hammingWeight(_ n: Int) -> Int {
|
||||||
var weight = 0
|
return n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1)
|
||||||
let quotient = n / 2
|
|
||||||
weight += n % 2
|
|
||||||
let roundedQuotient = floor(Double(quotient))
|
|
||||||
if roundedQuotient != 0 {
|
|
||||||
weight += hammingWeight(Int(roundedQuotient))
|
|
||||||
}
|
|
||||||
|
|
||||||
return weight
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func singleNumber(_ nums: [Int]) -> Int {
|
func singleNumber(_ nums: [Int]) -> Int {
|
||||||
|
|
|
||||||
|
|
@ -10,4 +10,4 @@ let code = LeetCode()
|
||||||
// print(code.hammingWeight(2_147_483_645))
|
// print(code.hammingWeight(2_147_483_645))
|
||||||
// print(code.singleNumber([4, 1, 2, 1, 2]))
|
// print(code.singleNumber([4, 1, 2, 1, 2]))
|
||||||
// print(code.containsDuplicate([1, 2, 3, 4]))
|
// print(code.containsDuplicate([1, 2, 3, 4]))
|
||||||
print(code.productExceptSelf([-1, 1, 0, -3, 3]))
|
// print(code.productExceptSelf([-1, 1, 0, -3, 3]))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue