yokostan / Leetcode-Solutions

Doing exercise on Leetcode. Carry on!
0 stars 3 forks source link

Leetcode #246. Strobogrammatic Number #225

Open yokostan opened 5 years ago

yokostan commented 5 years ago

Runtime beats 100%, or hashmap is another implementation

class Solution {
    public boolean isStrobogrammatic(String num) {
        if (num == null || num.length() == 0) return false;
        char[] c = num.toCharArray();
        int i = 0, j = c.length - 1;
        while (i <= j) {
                if (c[i] == '0' && c[j] == '0') {i++; j--;}
                else if (c[i] == '1' && c[j] == '1') {i++; j--;}
                else if (c[i] == '8' && c[j] == '8') {i++; j--;}
                else if (c[i] == '6' && c[j] == '9') {i++; j--;}
                else if (c[i] == '9' && c[j] == '6') {i++; j--;} 
                else return false;
        }
        return true;
    }
}