代码参考Leetcode 面试题01.01 字符是否唯一的解题思路
def sss():
s = input('请输入字符')
mark = 0
for i in s:
move_bit = ord(i) - ord('a')
if(mark & (1 << move_bit)) != 0:
print('F')
return False
else:
mark |= 1 << move_bit
print('T')
return True
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
sss()
请输入字符LEETCODE
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-e57e25f349dd> in <module>
17 # Press the green button in the gutter to run the script.
18 if __name__ == '__main__':
---> 19 sss()
<ipython-input-1-e57e25f349dd> in sss()
4 for i in s:
5 move_bit = ord(i) - ord('a')
----> 6 if(mark & (1 << move_bit)) != 0:
7 print('F')
8 return False
ValueError: negative shift count
提示出了negative shift count错误,是因为输入了大写英文字母
ord('L') - ord('a')
-21
1 << -21
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-d7e69202dc2f> in <module>
----> 1 1 << -21
ValueError: negative shift count
是因为出现了位移运算符的错误,才会提示negative shift count
需要注意的是,ord(‘c’)-ord(‘a’)=2,所以将其左移两位,具体思路可参考leetcode官网解题思路