问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

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

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

输入: bits = [1, 0, 0]

输出: True

解释: 唯一的编码方式是一个两比特字符和一个一比特字符。所以最后一个字符是一比特字符。

输入: bits = [1, 1, 1, 0]

输出: False

解释: 唯一的编码方式是两比特字符和两比特字符。所以最后一个字符不是一比特字符。

注意:

1 <= len(bits) <= 1000.
bits[i] 总是0 或 1.


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.

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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

public class Program {public static void Main(string[] args) {int[] nums = null;nums = new int[] { 1, 1, 1, 0 };var res = IsOneBitCharacter(nums);Console.WriteLine(res);Console.ReadKey();}private static bool IsOneBitCharacter(int[] bits) {//1总是要和后面的1个数字编码,即+2,0不用编码,+1往后继续即可//这道题做不出来主动面壁思过吧int i = 0;while(i < bits.Length - 1) {i = bits[i] == 0 ? ++i : i + 2;}return i == bits.Length - 1;}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3740 访问。

False

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#717-1比特与2比特字符( 1-bit and 2-bit Characters)相关推荐

  1. LeetCode刷题记录1——717. 1-bit and 2-bit Characters(easy)

    LeetCode刷题记录1--717. 1-bit and 2-bit Characters(easy) LeetCode刷题记录1--717. 1-bit and 2-bit Characters( ...

  2. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  3. C#LeetCode刷题,走进Google,走近人生

    该文章的最新版本已迁移至个人博客[比特飞],单击链接 C#LeetCode刷题,走进Google,走近人生 | .Net中文网 访问. 概述 本系列博文将会向大家介绍本人在钻研<算法导论 第3版 ...

  4. C#LeetCode刷题-程序员面试金典

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-程序员面试金典 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode程序员面试金典 ...

  5. C#LeetCode刷题-Shell

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-Shell | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCodeShell类算法题 ...

  6. C#LeetCode刷题-多线程

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-多线程 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode多线程类算法题汇总. ...

  7. C#LeetCode刷题-剑指Offer

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-剑指Offer | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode剑指Offer ...

  8. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...

  9. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% 困难 53 最 ...

  10. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

最新文章

  1. python---random模块使用详解
  2. Linux下zkui的安装
  3. java学习笔记(七)----异常
  4. 第六章:双指针,BFS,和图论 【完结】
  5. 分享8款简单大气的jQuery/CSS3图片特效
  6. 【ADO.NET基础知识】SqlConnection、command、DataSet 、DataTable、dataAdapter
  7. python matplotlib画图是设置线宽
  8. linux协议栈劫持,Linux系统优化之TCP协议栈优化-基本篇1
  9. android 自动化测试之monkeyrunner学习(三),自动化测试之Monkeyrunner
  10. IIC总线的操作时序
  11. centos安装multipath正确识别、使用存储(最基本的步骤)
  12. volley 框架简易封装使用
  13. js中数组常用逻辑算法(从大到小,从小到大排序,去重等问题)
  14. mysql 通讯录的实现_mysql中文排序并实现仿手机通讯录
  15. 穿山甲(巨量引擎)广告接入
  16. Amazon教程:刚买就降价!避免损失,申请PRICE MATCH(价格保护)的方法
  17. 读《周一清晨的领导课》有感
  18. Jenkins之分布式部署及构建(master-slaver)
  19. Python——计算程序运行帧率(FPS)
  20. 阿里和CVTE秋招面试题

热门文章

  1. SpringBoot—CORS跨域问题详解和解决方案
  2. temp变量this变量base变量 c# 1613715552
  3. 阿里云 快照恢复的操作过程
  4. django-orm-查询基本操作
  5. linux-vim-环境永久-多窗口操作
  6. python使用print不换行
  7. 【转】c#处理3种json数据的实例
  8. Java设计模式--解释器模式
  9. 使用 Scrum开发太阳能汽车
  10. 干货:结合Scikit-learn介绍几种常用的特征选择方法