LeetCode Strobogrammatic Number
2016-03-05 02:33
267 查看
原题链接在这里: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.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
题解:
把几种对应关系加到HashMap中,两个指针向中间夹逼.
Time Complexity: O(n). Space: O(1).
AC Java:
题目:
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.
题解:
把几种对应关系加到HashMap中,两个指针向中间夹逼.
Time Complexity: O(n). Space: O(1).
AC Java:
public class Solution { public boolean isStrobogrammatic(String num) { HashMap<Character, Character> hm = new HashMap<Character, Character>(); hm.put('6','9'); hm.put('9','6'); hm.put('1','1'); hm.put('0','0'); hm.put('8','8'); int l = 0; int r = num.length() - 1; while(l <= r){ if(!hm.containsKey(num.charAt(l))){ return false; }else if(hm.get(num.charAt(l)) != num.charAt(r)){ return false; }else{ l++; r--; } } return true; } }
相关文章推荐
- 大数据架构和模式(二)如何知道一个大数据解决方案是否适合您的组织
- LinuxMint17.3配置全局变量
- 第一期:搭建什么样的多用户平台?(未完)
- 第二期: 如何搭建 多用户的平台?(未完)
- 大数据架构和模式(三)理解大数据解决方案的架构层
- 如何避免编程中的BUG
- swift:CoreData简单入门(增加、查询、修改、删除)(详细讲解)
- ThreadLocal源码分析解密
- ubuntu 14.04 开机添加磁盘挂载
- bootstrap相关
- JavaScript-正则表达式总结
- 25该何去何从
- weblogic
- 微信js sdk 分享 失败 有时候好 有时候坏
- ssm框架整合笔记-1-基础框架搭建
- javascript中的BOM知识点总结
- Node.js是什么
- Graph Concepts
- angularjs animation
- Rest ful WebService