LeetCode 246. Strobogrammatic Number
原题链接在这里: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 II, Strobogrammatic Number III.
LeetCode 246. Strobogrammatic Number的更多相关文章
- [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] 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. Strobogrammatic Number 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
- 246. Strobogrammatic Number
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
随机推荐
- IAR_EW_MSP430下载
附带完整安装过程,来自本人下载截图. 附带四种花色的花样灯源码和仿真图(ps:不用担心是错的,有疑问欢迎博客留言) 链接:https://pan.baidu.com/s/1ShDRlEQLwkYNOu ...
- 记录MindSphere On Cloud Foundry的一次尝试过程
试验背景: 开始时间:2019年12月11日 结束时间:2019年12月13日 自己编写一个后台程序,尝试推送到Cloud Foundry上,并开放从MindSphere以外访问的权限. 程序实现以下 ...
- 并查集问题hdu 1232
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...
- PMBOK项目管理的五大过程组和十大知识领域
PMBOK五大过程组是:启动过程.规划过程.执行过程.监控过程.收尾过程. 各用一句话概括项目管理知识体系五大过程组: 1.启动过程组:作用是设定项目目标,让项目团队有事可做: 2.规划过程组:作用是 ...
- 效率提升工具Listary
效率提升工具Listary https://baijiahao.baidu.com/s?id=1590032175308204846&wfr=spider&for=pc
- POJ1475(Pushing Boxes)--bbffss
题目在这里 题目一看完就忙着回忆童年了.推箱子的游戏. 假设只有一个箱子.游戏在一个R行C列的由单位格子组成的区域中进行,每一步, 你可以移动到相邻的四个格子中的一个,前提是那个格子是空的:或者,如果 ...
- 【转】使用Scanner输入字符串时next()和nextLine()区别
在实现字符窗口的输入时,很多人更喜欢选择使用扫描器Scanner,它操作起来比较简单.在编程的过程中,我发现用Scanner实现字符串的输入有两种方法,一种是next(),一种nextLine(),但 ...
- js控制台不同的打印方式
在控制台单个输出: console.log(...):值 console.info(...):信息 console.debug(...):调试信息 console.warn(...):警告信息 con ...
- csv注入复现代码
以下代码生成的csv文件,使用Microsoft Execl能成功弹出计算器,虽然打开时有安全提示,但是大多数src还是会接收该类漏洞 -------------------------------- ...
- ios9 字符串与UTF-8 互相转换
在数据网路请求或其他情况下,需要将字符串转换成UTF-8编码 ios9后对其方法进行了修改 NSString *str = @"北京"; 把这个转成UTF8以前我们使用的是 NSS ...