717. 1-bit and 2-bit Characters@python
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的更多相关文章
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode Longest Substring Without Repeating Characters python
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...
- python 编码报错问题 'ascii' codec can't encode characters 解决方法
python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters pyth ...
- Python CSV文件处理/读写及With as 用法
可以不使用CSV模块 逐行处理: for line in open("samples/sample.csv"): title, year, director = line.spli ...
- [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 ...
- 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- 解决Excel打开UTF-8编码CSV文件乱码的问题
打开 Excel,执行“数据”->“自文本”,选择 CSV 文件,出现文本导入向导,选择“分隔符号”,下一步,勾选“逗号”,去掉“ Tab 键”,下一步,完成,在“导入数据”对话框里,直接点确定 ...
- bzoj 5499: [2019省队联测]春节十二响【堆】
首先看两条链怎么合并,贪心可得是从大到小取max,多条链同理 所以dfs合并子树的大根堆即可,注意为了保证复杂度,合并的时候要合并到最长链上,证明见长链剖分 #include<iostream& ...
- Luogu P1373 小a和uim之大逃离【dp】By cellur925
题目传送门 $50pts$:容易设计出状态$f[i][j][l][r][st]$表示当前的这个人在($i$,$j$),小a和uim魔瓶中的含量分别为$l$,$r$,当$st=0$表明现在是小a在吃,当 ...
- noi,ac第五场部分题解 By cellur925
题目质量还是不错的,只是我太菜了== 传送门 T1:序列计数(count) 题目描述 长度为n+1的序列A,其中的每个数都是不大于n的正整数,且n以内每个正整数至少出现一次. 对于每一个正整数k=1, ...
- 编译安装Apache:出现错误configure: error: mod_deflate
在进行编译安装Apache时,出现如下错误 checking whether to enable mod_deflate... configure: error: mod_deflate has be ...
- C#中自定义类数组和结构数组的使用
如有雷同,不胜荣幸,若转载,请注明 C#中自定义类数组和结构数组的使用 最近在很多项目中发现很多时候给定的数组要实现某个逻辑或处理很是麻烦,一维数组,二维数组,,,等等需要经过n多转换,还不如自己写一 ...
- cmd - 批量重命名文件
相信大家或多或少都遇到过类似的情况:从网上下载了好多图片(或者其他的文件),这些图片的名字往往都是些乱七八糟的字母数字的组合,我们想要一次性修改几十张上百张的图片的名字应该怎么办呢? 这里有两种方法, ...
- Java | 基础归纳 | set
用于存储无序(存入和取出的顺序不一定相同)元素,值不能重复.
- python之os、sys和random模块
import os # print(os.getcwd())#获取当前目录,绝对路径# print(os.chdir('../'))#更改当前目录,../的意思是退回上一级目录# print(os.g ...
- 线段树(区间合并) HDOJ 3308 LCIS
题目传送门 题意:线段树操作:1. 单点更新 2. 求区间的LCIS(longest consecutive increasing subsequence) 分析:注意是连续的子序列,就是简单的区间合 ...