原题链接在这里:https://leetcode.com/problems/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.

Example 1:

Input:  "69"
Output: true

Example 2:

Input:  "88"
Output: true

Example 3:

Input:  "962"
Output: false

题解:

把几种对应关系加到HashMap中,两个指针向中间夹逼.

Time Complexity: O(n). Space: O(1).

AC Java:

 class Solution {
public boolean isStrobogrammatic(String num) {
if(num == null || num.length() == 0){
return true;
} HashMap<Character, Character> hm = new HashMap<>();
String cans = "0011886996";
for(int i = 0; i<cans.length(); i+=2){
hm.put(cans.charAt(i), cans.charAt(i+1));
} int l = 0;
int r = num.length() - 1;
while(l <= r){
char left = num.charAt(l);
char right = num.charAt(r);
if(!hm.containsKey(left) || !hm.containsKey(right) || hm.get(left) != right || hm.get(right) != left){
return false;
} l++;
r--;
} return true;
}
}

跟上Strobogrammatic Number IIStrobogrammatic Number III.

LeetCode Strobogrammatic Number的更多相关文章

  1. [LeetCode] Strobogrammatic Number III 对称数之三

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

  2. [LeetCode] Strobogrammatic Number II 对称数之二

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

  3. [LeetCode] Strobogrammatic Number 对称数

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

  4. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

  5. Leetcode: Strobogrammatic Number III

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

  6. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

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

  7. [LeetCode] 248. Strobogrammatic Number III 对称数之三

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

  8. [LeetCode] 246. Strobogrammatic Number 对称数

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

  9. [LeetCode] 247. Strobogrammatic Number II 对称数II

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

随机推荐

  1. mysql 连接超时解决

    修改my.cnf文件即可. ************************************ 在/etc/my.cnf下添加如下两行代码: wait_timeout=31536000inter ...

  2. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  3. python 面向对象 初级

    面向对象 与 面向过程 面向对象对象,是根据某个对象,进行编写对象属性,不考虑对象以外的因素,只对对象本事的一些属于自己的属性进行创造,不用考虑 业务之间的逻辑. 面向过程, 是按照业务的一步步发展进 ...

  4. nodejs怎么同步从一个数据库查询函数中返回一个值

    var sql=require('msnodesql'); var conn_str="Driver={SQL Server Native Client 11.0};Server={127. ...

  5. 简单工程使用sbt公共库(sbt-assembly)

    只是为了简单实现一个算法,想用到breeze算法库.想把breeze当做external libraryies直接导入工程.可是官网没有,网上搜索更多的是在sbt工程或maven工程下. 后来实现目标 ...

  6. unity3d插件Daikon Forge GUI 中文教程6-高级控件richtextlabel的使用

    3.5.richtextlabel文本 可以像Word文档一样编辑出多样的内容,图片,字体颜色大小下划线.超链接背景等等. Defaults: 默认字体 默认图集 Blank Texture :空白的 ...

  7. windows自带的压缩,解压缩命令

    压缩一个文件: makecab c:\ls.exe ls.zip 解压一个文件: expand c:\ls.zip c:\ls.exe

  8. 模拟状态为inactive的日志损坏的恢复实验(完全恢复)

    1查看当前日志状态 从这里可以看到我们现在有三组日志,每组日志中只有1个成员.为了演示这个实验,我们为每个组增加1个成员. 2为每组增加组成员 添加后我们验证一下目前各日志成员的状态: 从上面的视图中 ...

  9. (转)MySQL优化实例

    在Apache, PHP,MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接影响到论坛的速度和承载量!同时 ...

  10. Maven详解之聚合与继承

    说到聚合与继承我们都很熟悉,maven同样也具备这样的设计原则,下面我们来看一下Maven的pom如何进行聚合与继承的配置实现. 一.为什么要聚合? 随着技术的飞速发展和各类用户对软件的要求越来越高, ...