junhong7 / Algorithm

0 stars 0 forks source link

进制转换 #2

Open junhong7 opened 4 years ago

junhong7 commented 4 years ago
  1. LeetCode-191 & 华为机试-HJ15 Approach #2 (Bit Manipulation Trick) 从二进制的角度思考,利用“与”运算,结合二进制“减法”。
    import sys
    n = int(sys.stdin.readline().strip())
    res = 0
    while n:
    res += 1
    n &= (n-1)
    print(res)