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.
链接: 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的更多相关文章
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode 246. Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- [LC] 246. Strobogrammatic Number
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- 【LeetCode】246. Strobogrammatic Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- [LeetCode] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode#246] Missing Ranges Strobogrammatic Number
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
随机推荐
- 在Centos7上安装漏洞扫描软件Nessus
本文摘要:简单叙述了在Centos7上安装Nessus扫描器的过程 Nessus 是目前全世界最多人使用的系统漏洞扫描与分析软件,Nessus的用户界面是基于Web界面来访问Nessus漏洞扫描器 ...
- 1094. The Largest Generation (25)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 【转载】mysql 四种隔离级别分析
sql标准中,有四种隔离级别,各个离级别都有各自的规则,隔离级别越低,允许并发越大,消耗的资源越少,但是越不安全,下面就mysql数据库来分别介绍一下(每个存储引擎实施的隔离级别会有稍微的不同)mys ...
- 【js】 流式布局 页面
<!DOCTYPE html><html><head> <meta content="text/html; charset=utf-8" ...
- CP="CAO PSA OUR" 用P3P header解决iframe跨域访问cookie
1.IE浏览器iframe跨域丢失Session问题 在开发中,我们经常会遇到使用Frame来工作,而且有时是为了跟其他网站集成,应用到多域的情况下,而Iframe是不能保存Session的因此,网上 ...
- SAP如何使用关于序列号的表
- 使用IE10登录,URL出现SessionId的解决办法
问题:用户登入之后,URL会出现一长串字符,类似SessionId,把这一长串字符删除之后重新进入页面,页面又会自动地跳转到登录页面,所以,应该是Session没记住用户已经登录的信息. 网站环境: ...
- windows 2012上配置好iis后出现不能访问的现象
新买的阿里云服务器,今天把环境弄了一下,安装iis之后不能访问网页 原因:没有安装asp.net 找到服务器管理器->(右上角)添加角色和功能->下一步下一步...找到应用程序 -> ...
- as3.0服务端FMS软件常用的方法与属性参考示例
转自:http://www.cuplayer.com/player/PlayerCode/RTMP/2012/0918429.html Application类的方法汇总方法 描述Applicatio ...
- 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 - BZOJ
Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...