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.


    public boolean isStrobogrammatic(String num) {
        if(num.length() == 0) return false;
        int [] map = new int[10];
        for(int n : map)
            n = -1;
        map[0] = 0;
        map[1] = 1;
        map[6] = 9;
        map[8] = 8;
        map[9] = 6;

        for(int i = 0 ; i < num.length(); i++){
            char c = num.charAt(num.length() - i - 1);
            if(map[c - '0'] == -1 || map[c - '0'] != num.charAt(i) - '0')
                return false;
        }
        return true;
    }

results matching ""

    No results matching ""