0

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?

Peter Tsung
  • 915
  • 2
  • 10
  • 20

2 Answers2

4

~ 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.

23k
  • 1,596
  • 3
  • 23
  • 52
1
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.

user3483203
  • 50,081
  • 9
  • 65
  • 94
mickeyandkaka
  • 1,452
  • 2
  • 11
  • 21