LeetCode 246. Strobogrammatic Number (可颠倒数字) $
2024-08-23 22:15:48
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.
题目标签:Hash Table
题目给了我们一个string num,让我们判断是不是 可翻转180°的数字。
在数字里,可以颠倒的数字有:0,1,6,9,8 在这些数字中,69 是特殊的一对,剩下的数字颠倒后依然是原样。
只要把这些存入HashMap就可以了。
Java Solution:
Runtime beats 11.67%
完成日期:05/27/2017
关键词:HashMap
关键点:利用HashMap来存入0 - 0, 1 - 1, 6 - 9, 9 - 6, 8 - 8
class Solution
{
public boolean isStrobogrammatic(String num)
{
HashMap<Character, Character> map = new HashMap<>(); map.put('1','1');
map.put('6','9');
map.put('9','6');
map.put('8','8');
map.put('0','0'); char[] arr = num.toCharArray();
int left = 0;
int right = arr.length - 1; while(left <= right)
{
if(!map.containsKey(arr[left]) || map.get(arr[left]) != arr[right])
return false; left++;
right--;
} return true; }
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
最新文章
- MySQL高级查询语句
- Django-安装篇
- JavaOne 2016——观众得以一睹JShell的威力
- 2015年9月28日JQuery提前预习预热笔记
- 3行3列表格 table实现,div+css实现
- 导入Excel加行公式和验证
- 部署DNS服务
- Android常用布局、文件存储与权限、XML
- Python s12 Day3 笔记及作业
- Linux多线程实践(三)线程的基本属性设置API
- hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)
- [20190416]process allocation latch.txt
- 红黑树(red-black tree)实现记录
- 解决Tomcat启动时项目重复加载问题
- gf框架之grpool - 高性能的goroutine池
- Asp.Net MVC Razor视图引擎与My97DatePicker插件的结合
- MVC MVP MVVM 图解
- golang 垃圾回收 gc
- APP测试功能点总结
- Linux 基础教程 41-系统关机和重启