246. 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.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
链接: http://leetcode.com/problems/strobogrammatic-number/
题解:
验证一个数是否是strobogrammatic number。我们可以用验证Palindrome的方法,从头部和尾部向中间遍历。这里因为这种数的条件比较少,所以我用了一个HashMap来保存所有合理的可能性。空间复杂度应该也可以算是O(1)的
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public boolean isStrobogrammatic(String num) {
if(num == null || num.length() == 0)
return false;
int lo = 0, hi = num.length() - 1;
Map<Character, Character> map = new HashMap<>();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6'); while(lo <= hi) {
char cLo = num.charAt(lo);
if(!map.containsKey(cLo))
return false;
else if(map.get(cLo) != num.charAt(hi))
return false;
else {
lo++;
hi--;
}
} return true;
}
}
二刷:
先建立一个查找表,然后遍历字符串的时候进行查找。表很小所以可以看做O(1)。 Stefan Pochmann还有很fancy的解法,放在reference里,很漂亮。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public boolean isStrobogrammatic(String num) {
if (num == null || num.length() == 0) {
return false;
}
Map<Character, Character> map = new HashMap();
map.put('6', '9');
map.put('9', '6');
map.put('1', '1');
map.put('8', '8');
map.put('0', '0');
int lo = 0, hi = num.length() - 1;
while (lo <= hi) {
if (map.containsKey(num.charAt(hi)) && num.charAt(lo) == map.get(num.charAt(hi))) {
lo++;
hi--;
} else {
return false;
}
}
return true;
}
}
三刷:
Java:
public class Solution {
public boolean isStrobogrammatic(String num) {
if (num == null) return false;
Map<Character, Character> map = new HashMap<>();
map.put('6', '9');
map.put('9', '6');
map.put('8', '8');
map.put('1', '1');
map.put('0', '0');
int lo = 0, hi = num.length() - 1;
while (lo <= hi) {
char loChar = num.charAt(lo);
char hiChar = num.charAt(hi);
if (!map.containsKey(loChar) || map.get(loChar) != hiChar) return false;
lo++;
hi--;
}
return true;
}
}
Reference:
https://leetcode.com/discuss/50594/4-lines-in-java
246. Strobogrammatic Number的更多相关文章
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- 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 246. Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- [LC] 246. Strobogrammatic Number
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 ...
- [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] Missing Ranges Strobogrammatic Number
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
随机推荐
- oracle创建用户,修改用户,删除用户等关于用户的
--直接修改底层表 USER$ 更换用户名 1.windows 平台下运行 cmd 2.sqlplus /nolog 3.SQL> conn SYSTEM/123@ORCL as sysdba ...
- js 前加分号和感叹号是什么意思?
;!function(){}(); ;!有什么用? 从语法上来开,Javascript中分号表示语句结束,在开头加上,可能是为了压缩的时候和别的方法分割一下,表示一个新的语句开始.所以,如果在一个单 ...
- mdelay,udelay,msleep区别
delay函数是忙则等待,占用CPU时间:而sleep函数使调用的进程进行休眠. udelay引用头文件/include/asm-***/delay.h,mdelay和ndelay则引用/includ ...
- mysql 查询
查询数据:select s_name from student limit 1;//限制数量 select * from student where s_id in (select s_id from ...
- .Net webservice动态调用
直接贴代码吧 public class PmsService { /// <summary> /// pms接口 /// </summary> /// <param na ...
- Luence简单实现1
初步认识Luence,简单按照官方文档做了个例子,大牛绕开,仅供小白路过参考.如有错误,欢迎指正批评. 建一个简单工程,并且加入这几个小奶瓶,如下图: 注:版本不同,可能对jdk的需求是不同的,这个需 ...
- 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- NYOJ-456 邮票分你一半 AC 分类: NYOJ 2014-01-02 14:33 152人阅读 评论(0) 收藏
#include<stdio.h> #define max(x,y) x>y?x:y int main(){ int n,x,y; scanf("%d",& ...
- Java Servlet Filter(转)
做web开发的人对于Filter应该不会陌生,一直在很简单的使用,但是一直没有系统的总结一下,随着年纪的慢慢长大,喜欢总结一些事情,下面说说我对Filter的理解,官方给出的Filter的定义是在请求 ...
- 判断不同IOS设备
var iOSGen = iPhone.generation; if (Debug.isDebugBuild) { Debug.Log("iPhone.generation : " ...