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 ...
随机推荐
- JDK1.8 的 HashMap 源码之注意事项
文章目录 链表变树 树形结构与Comparable,性能极致与降低 链表与树之间转换的阈值 英语渣靠着翻译插件,大概翻译的,难免有错误之处,注意甄别: 链表变树 This map usually ac ...
- linux 列出本文件下的目录
`ls /etc` 和$(ls /etc) 是两种获取命令执行结果的方式. for file in List 的语法里面的List 就是使用使用ls /etc 的命令执行结果作为List(https: ...
- Linux 基础 目录介绍
/bin 存放二进制可执行文件(ls cat clear)等等 ,常用基础命令在这个目录下 /etc 存放系统管理和配置文件 如 passwd 用 ...
- Python yield 使用浅析【转】
Python yield 使用浅析 IBM developerWorks 中国 : Open source IBM 开源 - IBM Developer 中国 (原 developerWorks 中国 ...
- Mybatis之关联关系(一对多、多对多)
目的: Mybatis关系映射之一对多 Mybatis关系映射之多对多 Mybatis关系映射之一对多 一对多 (订单对应多个订单项) 多对一 (订单项对应一个订单) 其是映射关系的基层思维是一样的 ...
- Netty服务端创建流程及组件职责
public class NettyServer { public static void main(String[] args) throws InterruptedException { NioE ...
- sublime text3上设置 python 环境
1. 打开Sublime text 3 安装package control 2. 安装 SublimeREPL Preferences -> package control 或者Ctrl+shi ...
- VBA switch语句
当用户想要根据Expression的值执行一组语句时,使用Switch Case语句. 每个值被称为一个”情况”,并根据每种情况变量接通测试.如果测试表达式与用户指定的任何Case不匹配,则执行Cas ...
- python day3 int,str,list类型补充
目录 python day 3 1. int类小知识点 2. str类小知识点 3. list类小知识点 python day 3 (学习资料来自老男孩教育) 2019/10/06 1. int类小知 ...
- JavaScript获取页面元素的常用方法
1.通过标签获取元素,返回一个数组 var li = document.getElementsByTagName('li');//标签获取元素 li[0].innerHTML;// 查看获取元素的内容 ...