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.

Solution

public class Solution {
    public boolean isStrobogrammatic(String num) {
        int i = 0;
        int j = num.length() - 1;
        while(i <= j) {
            if(!isStrob(num.charAt(i), num.charAt(j))) return false;
            i++;
            j--;
        }
        return true;
    }

    public boolean isStrob(char a, char b) {
        if(a == '1') return b == '1';
        else if(a == '0') return b == '0';
        else if(a == '6') return b == '9';
        else if(a == '8') return b == '8';
        else if(a == '9') return b == '6';
        return false;
    }
}

results matching ""

    No results matching ""