当前位置:网站首页>Leetcode daily question solution: 717 1-bit and 2-bit characters - reverse order
Leetcode daily question solution: 717 1-bit and 2-bit characters - reverse order
2022-06-24 18:13:00 【Xiaoxinxin's Xiaozhai】
There are two special characters :
The first character can use one bit 0 To express
The second character can use two bits (10 or 11) To express 、
Give one with 0 Binary array at the end bits , If the last character must be one character , Then return to true .
Example 1:
Input : bits = [1, 0, 0]
Output : true
explain : The only encoding is a two bit character and a one bit character .
So the last character is a bit character .
Example 2:
Input : bits = [1, 1, 1, 0]
Output : false
explain : The only way to encode is two bit characters and two bit characters .
So the last character is not a bit character .
Answer key : If you can't understand the question, please read my last blog ( The explanation is very clear )- This blog is a better way to use , The last one was about violence - This passage uses the method of flashback - The last one in each list must be 0, Then we can find the penultimate one first 0, Set his position as i , The next one is i+1,n For the length of the list ,i +1 To n - 2 The number of direct characters is n - i - 2, If n - i - 2 For the even ( because 1 and 1 1 and 0 As long as it is an even number, it must be able to form a complete 2 The currency ), So the last 0 There must be something left , To meet the requirements of the topic , by true, Otherwise false. If it's an odd number , Will eat the last 0, return false.
class Solution:
def isOneBitCharacter(self, bits: List[int]) -> bool:
n = len(bits) # Find the length of the list
i = n - 2 # initial i The location of the for n-2
while i >= 0 and bits[i]: # Loop traversal , When bit[i] by 0 When Out of the loop
i -= 1
return (n-i)%2 == 0 # Output results 边栏推荐
- Operation and maintenance guide | cos back source setting practice
- 你知道CMDB吗?
- Erc-721 Standard Specification
- Online sequence flow chart making tool
- Welcome to the network security threat information sharing program
- Selection (032) - what is the output of the following code?
- RestCloud ETL抽取动态库表数据实践
- Ten software development indicators for project managers
- From file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql The GPG key to retrieve the key source "MySQL 5.7 community server" is installed, but not applicable to
- Setting the Arduino environment for tinyml experiments
猜你喜欢

Error reported after NPM I

Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
Issue 39: MySQL time class partition write SQL considerations
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers

Nine practical guidelines for improving responsive design testing
Online sequence flow chart making tool

LC 300. Longest increasing subsequence

Three indicators to help you measure the effectiveness of digital transformation

Regression testing strategy for comprehensive quality assurance system

NVM download, installation and use
随机推荐
How to create simple shapes in illustrator 2022
Mariana Trench, Facebook's open source code analysis tool
这个巡检平台你还不知道,真是亏大了!
Several key points for enterprises to pay attention to digital transformation
Is it safe to open an account online? What should I do?
Constantly changing the emergency dialing of harmonyos ETS during the new year
Easygbs video platform TCP active mode streaming exception repair
03. Tencent cloud IOT device side learning -- overview of mqtt control package
Digital transformation informatization data planning and technology planning
Complete Guide to web application penetration testing
Error reported after NPM I
Erc-721 Standard Specification
Failure analysis | database failure MHA is not switched
股票网上开户安全吗?应该怎么办理?
Bigdecimalavoiddoubleconstructorrule: do not directly use the double variable as a parameter to construct BigDecimal
[JS Framework] Failed to execute the callback function:
How does the chief information security officer discuss network security with the enterprise board of directors
An analysis of the comments on the TV series Douban by procedural apes
Recommend 14 commonly used test development tools
你知道CMDB吗?