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
1 class Solution 2 { 3 public boolean isStrobogrammatic(String num) 4 { 5 HashMap<Character, Character> map = new HashMap<>(); 6 7 map.put('1','1'); 8 map.put('6','9'); 9 map.put('9','6'); 10 map.put('8','8'); 11 map.put('0','0'); 12 13 char[] arr = num.toCharArray(); 14 int left = 0; 15 int right = arr.length - 1; 16 17 while(left <= right) 18 { 19 if(!map.containsKey(arr[left]) || map.get(arr[left]) != arr[right]) 20 return false; 21 22 left++; 23 right--; 24 } 25 26 return true; 27 28 } 29 }
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List