★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-gctukhtj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We have two special characters. The first character can be represented by one bit 0
. The second character can be represented by two bits (10
or 11
).git
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.github
Example 1:微信
Input: bits = [1, 0, 0] Output: True Explanation: The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
Example 2:編碼
Input: bits = [1, 1, 1, 0] Output: False Explanation: The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
Note:spa
1 <= len(bits) <= 1000
.bits[i]
is always 0
or 1
.有兩種特殊字符。第一種字符能夠用一比特0
來表示。第二種字符能夠用兩比特(10
或 11
)來表示。code
現給一個由若干比特組成的字符串。問最後一個字符是否一定爲一個一比特字符。給定的字符串老是由0結束。htm
示例 1:blog
輸入: bits = [1, 0, 0] 輸出: True 解釋: 惟一的編碼方式是一個兩比特字符和一個一比特字符。因此最後一個字符是一比特字符。
示例 2:ip
輸入: bits = [1, 1, 1, 0] 輸出: False 解釋: 惟一的編碼方式是兩比特字符和兩比特字符。因此最後一個字符不是一比特字符。
注意:
1 <= len(bits) <= 1000
.bits[i]
老是0
或 1
.20ms
1 class Solution 2 { 3 func isOneBitCharacter( _ bits: [ Int ] ) -> Bool 4 { 5 func findR1( _ startIndex: Int ) -> Bool 6 { 7 if startIndex == ( bits.count ) 8 { 9 return false 10 } 11 if startIndex == ( bits.count - 1 ) 12 { 13 return bits[ ( bits.count - 1 ) ] == 0 14 } 15 var result: Bool 16 if bits[ startIndex ] == 0 17 { 18 result = findR1( startIndex + 1 ) 19 } 20 else 21 { 22 result = findR1( startIndex + 2 ) 23 } 24 return result 25 } 26 return findR1( 0 ) 27 } 28 }
1 class Solution { 2 func isOneBitCharacter(_ bits: [Int]) -> Bool { 3 var n:Int = bits.count 4 var i:Int = 0 5 while (i < n - 1) 6 { 7 i += bits[i] + 1 8 } 9 return i == n - 1 10 } 11 }
24ms
1 class Solution { 2 func isOneBitCharacter(_ bits: [Int]) -> Bool { 3 var skipping = false, is1bit = false 4 5 for b in bits { 6 if skipping { 7 skipping = false 8 continue 9 } 10 if b == 1 { 11 skipping = true 12 is1bit = false 13 } else { 14 is1bit = true 15 } 16 } 17 return is1bit 18 } 19 }
28ms
1 class Solution { 2 func isOneBitCharacter(_ bits: [Int]) -> Bool { 3 var state = false 4 var i = 0 5 while i < bits.count { 6 if bits[i] == 1{ 7 state = false 8 i += 2 9 }else{ 10 state = true 11 i += 1 12 } 13 } 14 return state 15 } 16 }
52ms
1 class Solution { 2 func isOneBitCharacter(_ bits: [Int]) -> Bool { 3 let count = bits.count 4 guard count > 1 else { return true } 5 6 var cursor = count - 2 7 var oneCount = 0 8 while cursor >= 0 { 9 defer { cursor -= 1 } 10 if bits[cursor] == 1 { 11 oneCount += 1 12 } 13 else { 14 break 15 } 16 } 17 18 return oneCount & 1 == 0 19 } 20 }
60ms
1 class Solution { 2 func isOneBitCharacter(_ bits: [Int]) -> Bool { 3 var isTwoBit = false 4 5 bits.enumerated().forEach { index, bit in 6 if isTwoBit { 7 isTwoBit = (bits.count - 1 == index) 8 return 9 } 10 11 if bit == 1 { 12 isTwoBit = true 13 return 14 } 15 } 16 17 return !isTwoBit 18 } 19 }