246. 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.
链接: http://leetcode.com/problems/strobogrammatic-number/
3/2/2017
performance不好,至少可以不用hashset而用hashmap,这样6,9就可以跟其他字母一样对待了。
1 public class Solution { 2 public boolean isStrobogrammatic(String num) { 3 HashSet<Character> s = new HashSet<Character>(Arrays.asList('1', '8', '0')); 4 for (int i=0, j=num.length() - 1; i<=j; i++, j--) { 5 char a = num.charAt(i); 6 char b = num.charAt(j); 7 if (a == b && s.contains(a) || a == '6' && b == '9' || a == '9' && b == '6') continue; 8 return false; 9 } 10 return true; 11 } 12 }