LeetCode 246. Strobogrammatic Number
2016-04-07 06:08
393 查看
原题网址: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.
思路:旋转之后能够对称的数字只有0、1、6、8、9。
方法二:预先保存旋转的对应数字。
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.
思路:旋转之后能够对称的数字只有0、1、6、8、9。
public class Solution { public boolean isStrobogrammatic(String num) { char[] na = num.toCharArray(); int i=0, j=na.length-1; while (i<=j) { if ((na[i] == '0' && na[j] == '0') || (na[i] == '1' && na[j] == '1') || (na[i] == '8' && na[j] == '8') || (na[i] == '6' && na[j] == '9') || (na[i] == '9' && na[j] == '6')) { i ++; j --; } else { return false; } } return true; } }
方法二:预先保存旋转的对应数字。
public class Solution { private int[] rotated = {'0', '1', (char)0x00, (char)0x00, (char)0x00, (char)0x00, '9', (char)0x00, '8', '6'}; private boolean[] strobo = {true, true, false, false, false, false, true, false, true, true}; public boolean isStrobogrammatic(String num) { char[] na = num.toCharArray(); for(int i=0, j=na.length-1; i<(na.length+1)>>1; i++, j--) { if (!strobo[na[i]-'0'] || na[j] != rotated[na[i]-'0']) return false; } return true; } }
相关文章推荐
- LeetCode 245. Shortest Word Distance III(最短单词距离)
- The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)
- ios中的定时
- LeetCode 244. Shortest Word Distance II(最短单词距离)
- lintcode-easy-Ugly Number
- #8判断和循环#
- Xsolla 电子竞技学院合作项目正式启动:你加入了么?
- vs2015中升级Nuget后,找不到Umbraco安装包问题
- Windows的自带控件(比如TButton)大多数消息都由它自己处理,Delphi覆盖了那么多WM_函数优先级较低,一般用不上
- liunx 下scp出现 Permission denied, please try again 的解决办法
- Windows下Keras报错TypeError: <lambda>() got an unexpected keyword argument 'name'
- HttpClient
- Android属性动画
- swift 二维数组
- Nexus启动失败处理:The nexus service was launched, but failed to start.
- VB洗牌算法产生随机数组
- 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时
- LeetCode 243. Shortest Word Distance(单词最短距离)
- Java发送邮件
- lintcode-medium-Subsets II