当前位置:网站首页>717.1-bit and 2-bit characters [sliding window]

717.1-bit and 2-bit characters [sliding window]

2022-06-24 18:25:00 @[toc] (directory)

https://leetcode-cn.com/problems/1-bit-and-2-bit-characters/

 Insert picture description here

This logic is so good
 Insert picture description here
simplify
 Insert picture description here

class Solution {
    
   //0,10,11  It can transform 
   //1,01,00, These are known by reading questions , impossible 1,01 ending 
  
   //bits[i] = 1->i+=2
   // 0->i++

    public boolean isOneBitCharacter(int[] bits) {
    
        int n = bits.length;
        int i=0;
        while(i<n){
    
            if(i == n-1){
      // If i It's the last one , Then go straight back 
                return true;
            }
            
            if(bits[i]==1){
    
                i+=2;
            }else{
    
                i+=1;
            }

        }

        return false;// If you traverse to the last second bit , Return to leave 
    }
}

Simplified version

class Solution {
    
    public boolean isOneBitCharacter(int[] bits) {
    
        
        int i=0;
        int n = bits.length;
        while(i<n-1){
    
        
            if(bits[i]==1){
    
                i+=2;
            }
            else{
    
                i+=1;
            }
        }
        return i == n-1;
        
    }
}

Inspection point : The sliding window ,i++,i+=2 The logic of , And finally simplify the logic of judgment

Reference link :
https://www.bilibili.com/video/BV1Nr4y1r7A9

原网站

版权声明
本文为[@[toc] (directory)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211343382968.html