LeetCode 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 Strobogrammatic Number的更多相关文章
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode Strobogrammatic Number II
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...
- Leetcode: Strobogrammatic Number III
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] 248. Strobogrammatic Number III 对称数之三
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 ...
随机推荐
- BZOJ4569 : [Scoi2016]萌萌哒
建立ST表,每层维护一个并查集. 每个信息可以拆成两条长度为$2$的幂次的区间相等的信息,等价于ST表里两对点的合并. 然后递归合并,一旦发现已经合并过了就退出. 因为一共只会发生$O(n\log n ...
- node.js 实现一个简单的登录拦截器
拦截器在web开发中随处可见,比如站点的管理后台,不说所有人都能进入,所以就需要做一个拦截器并友好的跳转到提示页. 下面我们简单实现一种,判断用户是否登录成功,登录不成功的用户自动重定向到登录页面. ...
- 原生js添加和删除类
原生js添加和删除类: this.className +=" "; this.className = this.className.replace(" 原来的类" ...
- jQuery AJAX实例
<html><head><title>jQuery Ajax 实例演示</title></head><script language= ...
- tornado 学习笔记3 安装
3 安装 安装分为两种方式:自动安装和手动安装,推荐采用自动安装,提前是机器联网的情况下. 3.1 自动安装: 命令:pip install tornado 写一段代码测试一下安装是否成功 # -*- ...
- gui学习
GUI 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面 有名的实现 : ucgui . 实现围绕几个问题: ...
- GO语言练习:channel 工程实例
1.工程代码 2.编译及运行 1.工程目录结构 $ tree cgss cgss ├── cgss.go └── src ├── cg │ ├── centerclient.go │ ├── ...
- 记录JVM内存模型,参数含义和优化
一.JVM内存模型 (图片来自网络) 根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老代) Perm (永久代) 其中New和Tenured属于堆内存,堆内存会从J ...
- hdu-acm steps 免费馅饼
/*dp入门级的题目,和数塔是一样的,这道题不用做什么优化,感觉时间复杂度不会超.主要还是细节上的问题, 这道题的状态和状态方程都容易找到,采用自底向上的方式会好很多*/ #include" ...
- 关于viewpoint的疑惑
问题: 为什么在手机上打开一个PC web页面,用手机打开一个宽度为980的固定布局页面,页面会默认缩放到刚好满屏显示,并不会出现横向滚动条? 一:设备像素和CSS像素区别 现代浏览器中实现缩放的方式 ...