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 ...
随机推荐
- JS实现可用滑块滑动的缓动图
尝试模仿京东的"发现好货"模块的可用滑块滑动的缓动图 JS代码 function $(id) { return document.getElementById(id); } //缓 ...
- JAVA堆,栈的区别,用AarrayList、LinkedList自定义栈
大家都知道java模拟机在运行时要开辟空间所以它有特定的五个内存划分: 1.寄存器: 2.本地方法区: 3.方法区: 4.栈内存: 5.堆内存: 但是我们今天来注重讲一下栈和堆 ...
- idea的项目结构
idea项目结构: 一般是创建一个empty project,然后再创建一个个的Module.
- mtd-utils 的 使用
关于编译可以查看文章:<Arm-Linux 移植 mtd-utils 1.x> 查看信息 使用命令前用cat /proc/mtd 查看一下mtdchar字符设备:或者用ls -l /dev ...
- windows主机上ORACLE生成awr报告的步骤
oracle数据库是一个大型的关系型数据库,那么如果有一天装载数据库的主机由于大量的IO操作导致主机cpu荷载超过100%会使得主机卡顿或者对数据库连接或者进行数据库进行正常的IO操作都会产生影响,所 ...
- Mac下Sublime Text3激活码
方法1: 终端中打开文件 /etc/hosts,插入如下语句 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 方法2: 在s ...
- Linux入职基础-1.1_国内开源的主要镜像站
Linux入职基础-1.1_国内开源的主要镜像站 东北地区: 东北大学 http://mirror.neu.edu.cn 大连理工大学 http://mirror.dlut.edu.cn 大连东软 ...
- day01-02
- SMARTY的简单实例写法
访问页面main.php(后台页面) <?php include("../init.inc.php"); //引入入口文件 include("../DBDA.php ...
- iOS 中 UIView 和 CALayer 的关系
UIView 有一个名叫 layer ,类型为 CALayer 的对象属性,它们的行为很相似,主要区别在于:CALayer 继承自 NSObject ,不能够响应事件. 这是因为 UIView 除了负 ...