update hammingWeight

This commit is contained in:
cediackermann 2026-06-08 10:37:44 +02:00
parent 04e76f1879
commit d7d9603bbc
2 changed files with 2 additions and 10 deletions

View file

@ -9,15 +9,7 @@ import Foundation
class LeetCode {
func hammingWeight(_ n: Int) -> Int {
var weight = 0
let quotient = n / 2
weight += n % 2
let roundedQuotient = floor(Double(quotient))
if roundedQuotient != 0 {
weight += hammingWeight(Int(roundedQuotient))
}
return weight
return n == 0 ? 0 : (n & 1) + hammingWeight(n >> 1)
}
func singleNumber(_ nums: [Int]) -> Int {

View file

@ -10,4 +10,4 @@ let code = LeetCode()
// print(code.hammingWeight(2_147_483_645))
// print(code.singleNumber([4, 1, 2, 1, 2]))
// print(code.containsDuplicate([1, 2, 3, 4]))
print(code.productExceptSelf([-1, 1, 0, -3, 3]))
// print(code.productExceptSelf([-1, 1, 0, -3, 3]))