题目:

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.

链接: http://leetcode.com/problems/strobogrammatic-number/

题解:

验证一个数是否是strobogrammatic number。我们可以用验证Palindrome的方法,从头部和尾部向中间遍历。这里因为这种数的条件比较少,所以我用了一个HashMap来保存所有合理的可能性。空间复杂度应该也可以算是O(1)的

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public boolean isStrobogrammatic(String num) {
if(num == null || num.length() == 0)
return false;
int lo = 0, hi = num.length() - 1;
Map<Character, Character> map = new HashMap<>();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6'); while(lo <= hi) {
char cLo = num.charAt(lo);
if(!map.containsKey(cLo))
return false;
else if(map.get(cLo) != num.charAt(hi))
return false;
else {
lo++;
hi--;
}
} return true;
}
}

二刷:

先建立一个查找表,然后遍历字符串的时候进行查找。表很小所以可以看做O(1)。 Stefan Pochmann还有很fancy的解法,放在reference里,很漂亮。

Java:

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public boolean isStrobogrammatic(String num) {
if (num == null || num.length() == 0) {
return false;
}
Map<Character, Character> map = new HashMap();
map.put('6', '9');
map.put('9', '6');
map.put('1', '1');
map.put('8', '8');
map.put('0', '0');
int lo = 0, hi = num.length() - 1;
while (lo <= hi) {
if (map.containsKey(num.charAt(hi)) && num.charAt(lo) == map.get(num.charAt(hi))) {
lo++;
hi--;
} else {
return false;
}
}
return true;
}
}

三刷:

Java:

public class Solution {
public boolean isStrobogrammatic(String num) {
if (num == null) return false;
Map<Character, Character> map = new HashMap<>();
map.put('6', '9');
map.put('9', '6');
map.put('8', '8');
map.put('1', '1');
map.put('0', '0');
int lo = 0, hi = num.length() - 1;
while (lo <= hi) {
char loChar = num.charAt(lo);
char hiChar = num.charAt(hi);
if (!map.containsKey(loChar) || map.get(loChar) != hiChar) return false;
lo++;
hi--;
}
return true;
}
}

Reference:

https://leetcode.com/discuss/50594/4-lines-in-java

246. Strobogrammatic Number的更多相关文章

  1. 246. Strobogrammatic Number 上下对称的数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

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

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

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

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

  4. LeetCode 246. Strobogrammatic Number

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

  5. [LC] 246. Strobogrammatic Number

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

  6. 【LeetCode】246. Strobogrammatic Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

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

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

  8. [LeetCode] 248. Strobogrammatic Number III 对称数III

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

  9. [LeetCode#246] Missing Ranges Strobogrammatic Number

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

随机推荐

  1. sysfs->sys简单介绍

    Sys节点 1:sysfs 是 Linux 内核中设计较新的一种虚拟的基于内存的文件系统, sysfs 的挂载点 /sys 目录结构. 2:/sys 文件系统下的目录结构 /sys 下的目录结构是经过 ...

  2. 应用层HTTP,FTP,TFTP,TELNET,DNS,EMAIL

    ip路由选择 crc校验 数据包转发子模块 ttl值 ip输出队列/输入队列icmp报文 路由表 ip是网络层 tcp是传输层 应用层表示层会话层传输层网络层链路层物理层 网卡工作在链路层 网卡是工作 ...

  3. 【ajax跨域】原因原理解决

    1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...

  4. 从零开始学ios开发(十七):Storyboards(上)

    在开始这章之前,先做个说明,从这篇开始,我所使用的xcode更新成了最新的版本,版本是4.6.1(4H512),如下: 大家可以打开自己电脑上的App Store,然后搜索xcode,第一个出现的就是 ...

  5. java 简单搜索算法

    --无序查找 public static int Search(int[] a,int key){ for(int i=0;i<a.length;i++){ if(key==a[i]){ ret ...

  6. 简单好用的 AJAX 上传插件,还可以抛弃难看的 file 按钮哦~

    在做网页设计的时候,设计师常常会把上传按钮设计得非常漂亮,还用了什么放大镜之类的图标来表达 browse 的效果.可是她们不知道,type="file" 的按钮在不同浏览器上的效果 ...

  7. vim插件介绍

    代码补全 http://blog.sina.com.cn/s/blog_a6559d920101acv3.html这个牛逼.************************************** ...

  8. JavaScript高级---桥模式设计

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. Sandcastle:生成.NET API文档的工具 (帮忙文档)

    (1)准备软件 首先需要我们准备如下软件: SandCastle, 下载地址: http://sandcastle.codeplex.com/releases/view/47665 (2)准备项目文件 ...

  10. 如何解决VS启动越来越慢

    VS2013 用久后,现在启动和打开项目变得很慢 解决方案: A.清理缓存 VS2010清理缓存:启用vs2010命令行工具:在vs2010命令提示符下,执行devenv.exe /resetuser ...