教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode717. 1比特与2比特字符 | 1-bit and 2-bit Characters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10509872.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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).

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.

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:

  • 1 <= len(bits) <= 1000.
  • bits[i] is always 0 or 1.

有两种特殊字符。第一种字符可以用一比特0来表示。第二种字符可以用两比特(10 或 11)来表示。

现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0结束。

示例 1:

输入: 
bits = [1, 0, 0]
输出: True
解释: 
唯一的编码方式是一个两比特字符和一个一比特字符。所以最后一个字符是一比特字符。

示例 2:

输入: 
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 }
复制代码

Runtime: 24 ms
Memory Usage: 18.7 MB
复制代码
 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 }
复制代码

 

 

posted @   为敢技术  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°