博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 717: 1-bit and 2-bit Characters
阅读量:5016 次
发布时间:2019-06-12

本文共 1424 字,大约阅读时间需要 4 分钟。

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: TrueExplanation: 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: FalseExplanation: 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.

 

1 public class Solution { 2     public bool IsOneBitCharacter(int[] bits) {         3         // Optimization: if the last bit is not 0, return false immediately 4         if (bits[bits.Length - 1] != 0) return false; 5          6         int i = 0; 7         while (i < bits.Length) 8         { 9             if (bits[i] == 1)10             {11                 i += 2;12             }13             else if (i == bits.Length - 1)14             {15                 return true;16             }17             else18             {19                 i++;20             }                21         }22         23         return false;24     }25     26 }

 

转载于:https://www.cnblogs.com/liangmou/p/7782893.html

你可能感兴趣的文章
读后感-浮生六纪
查看>>
执行指定路径的程序文件
查看>>
Leetcode-950 Reveal Cards In Increasing Order(按递增顺序显示卡牌)
查看>>
[Linux] 在 Linux CLI 使用 ssh-keygen 生成 RSA 密钥
查看>>
14款下载有用脚本的超酷网站
查看>>
LXC-Linux Containers介绍
查看>>
c#中使用servicestackredis操作redis
查看>>
ios app 真机crash报告分析
查看>>
CRC标准以及简记式
查看>>
SEO搜索引擎
查看>>
关于本地使用tomcat部署web应用,浏览器自动跳转为https的问题
查看>>
一、Text To Speech
查看>>
Java读取并下载网络文件
查看>>
github上构建自己的个人网站
查看>>
在word中粘贴的图片为什么显示不完整
查看>>
SQL Server 数据库的鼠标操作
查看>>
net软件工程师求职简历
查看>>
nullnullHandling the Results 处理结果
查看>>
SQL SERVER BOOK
查看>>
JS基础回顾,小练习(判断数组,以及函数)
查看>>