• 717. 1-bit and 2-bit Characters最后一位数是否为0


    [抄题]:

    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.
    

    Example 2:

    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.
    

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    没见过,不会

    [一句话思路]:

    正常操作后判断位数能不能对上

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 用while循环,因为最后要差也就之差了一位

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    正常操作后判断位数能不能对上

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [关键模板化代码]:

    直接用能不能对上来返回:

    //return, check
            return i == n - 1;

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public boolean isOneBitCharacter(int[] bits) {
            //ini
            int n = bits.length;
            int i = 0;
            
            //cc
            if (bits == null || n == 0) {
                return false;
            }
            
            //while loop
            while (i < n - 1) {
                if (bits[i] == 0) {
                    i++;
                }else {
                    i += 2;
                }
            }
            
            //return, check
            return i == n - 1;
        }
    }
    View Code
  • 相关阅读:
    「我和分布式数据库的故事」有奖征文来啦
    从落地效果看,如何基于SequoiaDB构建「集约运营」城商行内容管理平台
    MQ架构设计原理
    jpa条件表达式Expressions.booleanTemplate和JPAExpressions
    Git常用操作基础补充
    MongoDB有哪些数据脱敏技术?
    MongoDB常见问题解答:时间与时区
    【网络研讨会】MongoDB Vs 效仿者:选择MongoDB的理由
    MongoDB案例分享:如何使用oplog恢复数据
    记某核心MongoDB集群索引优化实践
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8875094.html
Copyright © 2020-2023  润新知