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 ...
随机推荐
- win10改装win7
参考链接: https://jingyan.baidu.com/article/3ea51489d0f3c852e61bba01.html 1. 制作win7 U盘启动盘 2. 设置BIOS 1:OS ...
- java多线程中篇(三) —— 线程的控制(创建,运行,阻塞,中断,结束)
简介 线程的控制就是程序对线程的主要管理,最重要的就是状态的切换维护. 每种转态都有不同的引发事件(对应线程的方法),每种状态又有各自不同的处理步骤和过程,整个线程控制主要就是涉及这些内容. 正文 线 ...
- 关于Python编码这一篇文章就够了
概述 在使用Python或者其他的编程语言,都会多多少少遇到编码错误,处理起来非常痛苦.在Stack Overflow和其他的编程问答网站上,UnicodeDecodeError和UnicodeEnc ...
- Linux shell 中 & && [] [[]] () [] 含义
| 语法:command 1 | command 2 功能:把第一个命令 command 1 执行的结果作为 command 2 的输入传给 command 2 & & 放在启动参数后 ...
- Linux基础-05-正文处理、tar、解压缩
1. 使用cat命令进行文件的纵向合并 1) 使用cat命令实现文件的纵向合并: a) 例如:使用cat命令将baby.age.baby.kg和baby.sex这三个文件纵向合并为baby文件的方法: ...
- Scratch(四)舞台区详解
在Scratch里面,所有的表现结果都在“舞台区”呈现,前面我们学习的“石头剪刀布”游戏,也是在“舞台区”完成的. 舞台区是非常重要的区域,所以我们今天单独用一个章节来详细说说这个舞台. 既然是一个舞 ...
- [v]Windows下Git安装指南
参考<Git权威指南>安装整理,图书配套网址参见[1] 1. Cygwin下安装配置Git 1. 在Windows下安装配置Git有2种不同的方案 (1)msysGit, (2)Cygwi ...
- javascript 之 扩展对象 jQuery.extend
在JQuery的API手册中,extend方法挂载在JQuery 和 JQuery.fn两个不同的对象上,但在JQuery内部代码实现的是相同的,只是功能各不相同. 官方解释: jQuery.exte ...
- Task 开始 停止
注意点:需要将每个线程的 MemoryCacheManager 保存,这里我保存在缓存中,需要取消时根据缓存key值取出 MemoryCacheManager //开始Task1 private vo ...
- 使用Harbor搭建Docker私有镜像仓库
Harbor介绍:https://goharbor.io/ 前置条件 需要安装了docker和docker-compose 下载Harbor 在harbor下载页(https://github.com ...