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.

原题地址:  1-bit and 2-bit Characters

难度:  Easy

题意: 存在两类数,一类是10或者11,另一类为0.将一个数组拆分为这两类数,判断最后一组数为0,比如上面例子,第一组数为[1,0],第二组数为[0]

思路:

对于数字1,后面的一个数字是1或者0都行,所以遇到以可以跳过1后面的值,遇到0,则移动一位

代码:

class Solution(object):
def isOneBitCharacter(self, bits):
"""
:type bits: List[int]
:rtype: bool
"""
i = 0
while i < len(bits):
if bits[i] == 1:
i += 2
if i == len(bits):
return False
else:
i += 1
return True

时间复杂度: O(n)

空间复杂度: O(1)

717. 1-bit and 2-bit Characters@python的更多相关文章

  1. Leetcode3:Longest Substring Without Repeating Characters@Python

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  2. leetcode Longest Substring Without Repeating Characters python

    class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...

  3. python 编码报错问题 'ascii' codec can't encode characters 解决方法

    python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters pyth ...

  4. Python CSV文件处理/读写及With as 用法

    可以不使用CSV模块 逐行处理: for line in open("samples/sample.csv"): title, year, director = line.spli ...

  5. [LeetCode&Python] Problem 717. 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  6. 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  7. Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12

    今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>&g ...

  8. python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

    使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...

  9. 【Python】【BugList12】python自带IDLE执行print(req.text)报错:UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 93204-93204

    [代码] # -*- coding:UTF-8 -*- import requests if __name__ == '__main__': target = 'https://unsplash.co ...

随机推荐

  1. 斯坦福CS231n—深度学习与计算机视觉----学习笔记 课时1

    课时1 计算机视觉历史回顾与介绍上 CS231n:这一一门关于计算机视觉的课程,基于一种专用的模型架构,叫做神经网络(更细一点说,是卷积神经网络CNN).计算机视觉是人工智能领域中发展最为迅猛的一个分 ...

  2. 天空盒的制作方法 Max来生成天空盒的六张图片

    在虚拟现实技术中,需要产品展示,场景漫游等,只要想在内部有一个虚拟的3D天空,那么都要用到天空球:天空球目前基本做法主要有两种:分别是正方形的和球形的. 目前360度全景图主要用的是球形的,针对目前已 ...

  3. hdu1085 Holding Bin-Laden Captive!【生成函数】

    列出生成函数的多项式之后暴力乘即可 #include<iostream> #include<cstdio> #include<cstring> using name ...

  4. 学习Mahout(一)

    Mahout 官方下载地址:http://apache.fayea.com/apache-mirror/mahout/ 环境ubuntu 12.04, hadoop1.2.1 ,mahout 0.9 ...

  5. noi.ac 邀请赛1 By cellur925

    A. array 考场:上来就想暴力,首先第一个子任务肯定没问题,怎么搞都行.然后第二个子任务用个数组记下新修的值就行了.第三个子任务用一下等差数列求和公式帮助求解,每次都重新算(因为每次改变全部元素 ...

  6. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Highway

    Highway Accepted : 122   Submit : 393 Time Limit : 4000 MS   Memory Limit : 65536 KB Highway In ICPC ...

  7. LCA UESTC 92 Journey

    题目传送门 题意:先给一棵树,然后有一条额外的边,问u走到v从现在最短的路走和原来不加边走的路节省了多少距离 分析:首先跑不加边的树的LCA,这样能求出任意两点的距离,那么现在x和y多连了一条边,如果 ...

  8. 1-16使用try-catch捕捉异常

    处理异常 可以使用try-catch-处理异常,例如之前的程序可以使用try-catch-处理 package com.monkey1024.exception; import java.io.Fil ...

  9. MySQL读写分离实现

    数据库写入效率要低于读取效率,一般系统中数据读取频率高于写入频率,单个数据库实例在写入的时候会影响读取性能,这是做读写分离的原因.实现方式主要基于mysql的主从复制,通过路由的方式使应用对数据库的写 ...

  10. SQL异常为"当IDENTITY_INSERT设置为OFF时" 的解决

    误删数据库时,可以利用insert插入删除的数据,但是有时表可能有自增字段如id.这是插入数据如果包含自增字段就会出现错误,提示"IDENTITY_INSERT设置为OFF,插入失败&quo ...