diff --git a/Sources/LeetCode/LeetCode.swift b/Sources/LeetCode/LeetCode.swift index 4fff6b2..14e7a36 100644 --- a/Sources/LeetCode/LeetCode.swift +++ b/Sources/LeetCode/LeetCode.swift @@ -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 { diff --git a/Sources/LeetCode/main.swift b/Sources/LeetCode/main.swift index 9238e25..eae89ad 100644 --- a/Sources/LeetCode/main.swift +++ b/Sources/LeetCode/main.swift @@ -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]))