[英]what num[~ Wavy lines ] in python means?
I encountered a problem in leetcode named 246. Strobogrammatic Number
我在leetcode中遇到了一个名为
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? 我很好奇
num[~i]
是什么意思?
~
is the NOT
Bitwise operator. ~
是NOT
按位运算符。 Essentially it will invert all the bits. 本质上,它将反转所有位。
So if you performed ~
on 4 bits like 0101
, it would invert to 1010
. 因此,如果您对4位(如
0101
执行~
,它将反转为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. 我发现这是一个有用的答案,因为按位运算符可以变成一个复杂的主题,而该主题肯定已涵盖在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. (可能)表示反转数字的二进制表示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.