码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 刷题专栏 【欢迎指点优化】

时间:2019-03-25 10:21:05      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:ora   count   com   tps   bsp   ==   ems   lse   +=   

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 刷题专栏 【欢迎指点优化】

标签:ora   count   com   tps   bsp   ==   ems   lse   +=   

原文地址:https://www.cnblogs.com/lijiagui/p/10590787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!