zoukankan      html  css  js  c++  java
  • 246. Strobogrammatic Number 上下对称的数字

    [抄题]:

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

    Write a function to determine if a number is strobogrammatic. The number is represented as a string.

    For example, the numbers "69", "88", and "818" are all strobogrammatic.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    class Solution {
        public boolean isStrobogrammatic(String num) {
            //cc
            if (num == null || num.length() == 0) {
                return true;
            }
            
            //for loop
            for (int i = 0, j = num.length() - 1; i <= j ; i++, j --) {
                if (!"11 00 88 69 96".contains(num.charAt(i) + "" + num.charAt(j))) return false;
            }
            
            return true;
        }
    }
    View Code

    不知道对称性怎么处理:两个指针啊!

    [一句话思路]:

    抽出来之后看字符串中是否互相包含

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

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

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

    [总结]:

    判断字符串对称 用对撞型指针

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

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

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    247. Strobogrammatic Number II 找出所有两位的:递归,好吧

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

     [代码风格] :

  • 相关阅读:
    快速幂和矩阵快速幂-模板
    gcd-模板+最小公倍数
    manacher-模板-hd-3068
    kmp-模板-hd-1711
    链式前向星-邻接表--模板
    poj-3096-Suprising Strings
    hihocoder-1615-矩阵游戏II
    hihocoder-Week174-Dice Possibility
    论文-Swish: A self-gated Active Function
    hihocoder-1595-Numbers
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8961401.html
Copyright © 2011-2022 走看看