File tree Expand file tree Collapse file tree 1 file changed +43
-1
lines changed Expand file tree Collapse file tree 1 file changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -1614,6 +1614,48 @@ def getBinaryList(num: int) -> list:
16141614
16151615 return ans
16161616
1617+ def isOneBitCharacter (self , bits : List [int ]) -> bool :
1618+ """
1619+ 返回是否必须是 1 bit字符结尾
1620+ 也就是不存在最后以 2 bit 结尾的选项
1621+ 使用dp
1622+ dp[i][j] 代表 第i个结尾字符是 第j种字符结尾的可能
1623+ 0 - 0
1624+ 1 - 10
1625+ 2 - 11
1626+ :param bits:
1627+ :return:
1628+ """
1629+ n = len (bits )
1630+ dp = [[0 ,0 ,0 ] for _ in range (n )]
1631+ if n == 1 :
1632+ return bits [0 ] == 0
1633+ if bits [0 ] == 0 :
1634+ dp [0 ][0 ] = 1
1635+ if bits [1 ] == 0 :
1636+ dp [1 ][0 ] = 1
1637+ else :
1638+ if bits [1 ] == 0 :
1639+ dp [1 ][1 ] = 1
1640+ else :
1641+ dp [1 ][2 ] = 1
1642+
1643+ for i in range (2 , n ):
1644+ if bits [i ] == 0 :
1645+ if sum (dp [i - 1 ]) > 0 :
1646+ dp [i ][0 ] = 1
1647+ if sum (dp [i - 2 ]) > 0 and bits [i - 1 ] == 1 :
1648+ dp [i ][1 ] = 1
1649+ else :
1650+ if bits [i - 1 ] == 1 and sum (dp [i - 2 ]) > 0 :
1651+ dp [i ][2 ] = 1
1652+
1653+ # for i in dp:
1654+ # print(i)
1655+ return dp [- 1 ][0 ] == 1 and sum (dp [- 1 ]) == 1
1656+
1657+ def numDecodings (self , s : str ) -> int :
1658+
16171659
16181660s = Solution ()
1619- print (s .reverseBits ( num = 2147483647 ))
1661+ print (s .isOneBitCharacter ( bits = [ 1 , 1 , 1 , 0 ] ))
You can’t perform that action at this time.
0 commit comments