简体   繁体   中英

what num[~ Wavy lines ] in python means?

I encountered a problem in leetcode named 246. Strobogrammatic Number

class Solution(object):
    def isStrobogrammatic(self, num):
        return all(num[i] + num[~i] in '696 00 11 88' for i in range(len(num)/2+1))

i am curious of what num[~i] means?

~ is the NOT Bitwise operator. Essentially it will invert all the bits.

So if you performed ~ on 4 bits like 0101 , it would invert to 1010 .

Here's a helpful answer that I found, as Bitwise operators can turn into a complex topic that has surely been covered on SO.

for i in range(10):
...  print(i, ~i)
...
0 -1
1 -2
2 -3
3 -4
4 -5
5 -6
6 -7
7 -8
8 -9
9 -10

It (probably) means reversing the binany representation of the number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM