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.

翻转180度后对称的数有:8->8, 0->0, 1->1, 6->9, 9->6,从两边向中间检查对应位置的两个数是否满足对称数就行了。比如619,先判断6和9是有映射的,然后1和自己又是映射,所以是对称数。有点像判断回文Palindrome,回文判断是否相等,这里判断是否满足那几个数字的条件。判断是可以直接写条件判断,也可以用HashMap存放数字的映射,然后用双指针从两边向中间查看。

Java:

public class Solution {
public boolean isStrobogrammatic(String num) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('1','1');
map.put('0','0');
map.put('6','9');
map.put('9','6');
map.put('8','8');
int left = 0, right = num.length() - 1;
while(left <= right){
if(!map.containsKey(num.charAt(right)) || num.charAt(left) != map.get(num.charAt(right))){
return false;
}
left++;
right--;
}
return true;
}
}

Python:

class Solution:
lookup = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'} def isStrobogrammatic(self, num):
n = len(num)
for i in xrange((n+1) / 2):
if num[n-1-i] not in self.lookup or \
num[i] != self.lookup[num[n-1-i]]:
return False
return True

Python: wo

class Solution():
def strobogrammatic(self, s):
lookup = {'1': '1', '8': '8', '0': '0', '6': '9', '9': '6'}
i, j = 0, len(s) - 1
while i <= j:
if s[i] not in lookup or lookup[s[i]] != s[j]:
return False
i += 1
j -= 1 return True 

C++:

class Solution {
public:
bool isStrobogrammatic(string num) {
unordered_map<char, char> m {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
for (int i = 0; i <= num.size() / 2; ++i) {
if (m[num[i]] != num[num.size() - i - 1]) return false;
}
return true;
}
};

 

类似题目: 

[LeetCode] 247. Strobogrammatic Number II 对称数II

[LeetCode] 248. Strobogrammatic Number III 对称数III

All LeetCode Questions List 题目汇总

  

[LeetCode] 246. Strobogrammatic Number 对称数的更多相关文章

  1. [LeetCode] Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. LeetCode 246. Strobogrammatic Number

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...

  4. [LeetCode] 247. Strobogrammatic Number II 对称数II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  6. [LeetCode] 248. Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. 246. Strobogrammatic Number 上下对称的数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

  8. 【LeetCode】246. Strobogrammatic Number 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  9. 246. Strobogrammatic Number

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

随机推荐

  1. SQL模糊查询的四种匹配模式

    执行数据库查询时,有完整查询和模糊查询之分,一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 一.四种匹配模式 关于条件,SQL提供了四种匹配模式: 1.% 表 ...

  2. zabbix4.2.5默认告警模板

    产生告警: Problem: {EVENT.NAME} Problem started at {EVENT.TIME} on {EVENT.DATE} Problem name: {EVENT.NAM ...

  3. [Luogu 3794]签到题IV

    Description 题库链接 给定长度为 \(n\) 的序列 \(A\).求有多少子段 \([l,r]\) 满足 \[ \left(\gcd_{l\leq i\leq r}A_i\right) \ ...

  4. Problem F. Wiki with String

    Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...

  5. Bootstrap内辅助类,响应式工具,组件的个人总结

    辅助类(工具类): 文本颜色: <p class="text-muted">Fusce dapibus, tellus ac cursus commodo, torto ...

  6. 使用docker 实现MySQL主从同步/读写分离

    1. 利用 docker 实现 mysql 主从同步 / 读写分离 为了保证数据的完整和安全,mysql 设计了主从同步,一个挂掉还可以用另个.最近重构论坛,想来改成主从吧.担心失误,就先拿 dock ...

  7. springboot(三)

    SpringBoot 整合JdbcTemplate 1.创建一个springboot_jdbc项目  2.导入依赖 <dependency> <groupId>org.spri ...

  8. vue-cli3 ios10白屏问题解决思路

    在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*'  ...

  9. Gift to XBACK(小小礼物)

    什么白天 什么黑夜 我没有 准备着给你的 Surprise 你给我的爱 让我觉得已足够 是你让我相信爱会有 是你的爱陪我绕宇宙 打开日记本写下忧愁 你却让我看时间轴 我才知道现在我能看到的画面 拥有你 ...

  10. html转为图片插件:html2canvas保存图片模糊问题解决

    使用官网的CDN: <script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></ ...