LeetCode 刷题专栏 欢迎指点优化
Posted lijiagui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 刷题专栏 欢迎指点优化相关的知识,希望对你有一定的参考价值。
717. 1-bit and 2-bit Characters
链接:https://leetcode.com/problems/1-bit-and-2-bit-characters/
1 class Solution: 2 def isOneBitCharacter(self, bits): 3 length = len(bits) 4 count = 0 5 bits.reverse() 6 if (length == 1): 7 return True 8 if (bits[1] == 0): 9 return True 10 while (length > 0): 11 if (count == length - 1): 12 if (count % 2 != 0): 13 return False 14 else: 15 return True 16 if (bits.pop(bits[1]) == 1): 17 count += 1 18 else: 19 break 20 if (count % 2 != 0): 21 return False 22 else: 23 return True
对代码进行优化:
1 class Solution: 2 def isOneBitCharacter(self, bits): 3 length = len(bits) 4 count = 0 5 bits.reverse() 6 if (length==1 or bits[1] == 0): 7 return True 8 while (bits.pop(bits[1]) == 1): 9 count+=1 10 if (count == length - 1): 11 break 12 if (count % 2 != 0): 13 return False 14 else: 15 return True
以上是关于LeetCode 刷题专栏 欢迎指点优化的主要内容,如果未能解决你的问题,请参考以下文章