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 ...
随机推荐
- 解决VS2012新建MVC3等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误
vs2012来做一个mvc3的项目,哪知在创建ado数据模型时跳出这么一个东东 错 误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0 ...
- ASP.NET MVC +EasyUI 权限设计(二)环境搭建
请注明转载地址:http://www.cnblogs.com/arhat 今天突然发现博客园出问题了,老魏使用了PC,手机,平板都访问博客园了,都是不能正常的访问,原因是不能加载CSS,也就是不能访问 ...
- vagrant在windows下的使用
vagrant在windows下的使用 下载安装 VirtualBox :https://www.virtualbox.org/ 下载安装 Vagrant :http://www.vagrantup. ...
- NGUI基础之button(按钮)
1,button的创建:2,button组件的基本属性:3,button的事件监听 原位地址:http://blog.csdn.net/dingkun520wy/article/details/504 ...
- NOSQL的应用,Redis/Mongo
NOSQL的应用,Redis/Mongo 1.心路历程 上年11月份来公司了,和另外一个同事一起,做了公司一个移动项目的微信公众号,然后为了推广微信公众号,策划那边需要我们做一些活动,包括抽奖,投票. ...
- Quartz 2D画虚线-b
这里使用的函数为 CGContextSetLineDash,有四个参数 CGContextSetLineDash(<#CGContextRef _Nullable c#>, < ...
- unable to start within 45 seconds. If the server requires more time, try increasing the timeout in the server editor
eclipse启动项目时,提示超时: 解决方案: 修改 workspace\.metadata\.plugins\org.eclipse.wst.server.core\servers.xml文件. ...
- github 中删除/更名版本库(repository)
问题描述: github 中版本库创建/删除/更该名称 问题解决: (1)创建版本库(Repository) 注: 在上图中的+按钮图标指示的是创建版本库的按钮 注 ...
- Matlab实现二进制矩阵转换为十进制
一.问题描述 [1 1 1 0 1 0 1 1 0 1 0 0 1 1 0] 每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列. [7 1 6 1 1 2] 二.问题分析 1. ...
- DIV+CSS 基础
盒子模型:margin(边界),可被占位:border(边框):padding(填充):content(内容) 块元素: 默认占据一行,前后换行. 作为容器,装载块元素和行内元素,被装载元素的位置,会 ...