Problem

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.

Example 1:

Input: "69"
Output: true
Example 2:

Input: "88"
Output: true
Example 3:

Input: "962"
Output: false

Solution

class Solution {
    public boolean isStrobogrammatic(String s) {
        //69, 88, 00, 11, 6969, 698869, 69869, 6908069, 886988
        Map<Character, Character> map = new HashMap<>();
        map.put('6', '9');
        map.put('9', '6');
        map.put('0', '0');
        map.put('1', '1');
        map.put('8', '8');
        int i = 0, j = s.length()-1;
        while (i <= j) {
            if (!map.containsKey(s.charAt(i))) return false;
            if (map.get(s.charAt(i++)) != s.charAt(j--)) return false;
        }
        return true;
    }
}

linspiration
161 声望53 粉丝