November 28, 2019

246. Strobogrammatic Number

246. Strobogrammatic Number

  • 算出对应的位置
  • 简单的做一个映射
class Solution {
    public boolean isStrobogrammatic(String num) {
        Map<Character, Character> map = new HashMap<>();
        map.put('0', '0');
        map.put('1', '1');
        map.put('6', '9');
        map.put('8', '8');
        map.put('9', '6');
        int i = 0;
        int j = num.length() - 1;
        while (i <= j) { // 注意这里是<=
            if (!map.containsKey(num.charAt(i))) {
                return false;
            }
            if (map.get(num.charAt(i)) != num.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
comments powered by Disqus