0ms C++,simply code


  • 1
    S
    class Solution {
    public:
        bool isStrobogrammatic(string num) {
            int size = num.size();
            int n = (size%2 == 0 ? size/2 : size/2+1);
            for(int i = 0;i < n;i++){
                if(num[i] == '2' || num[i] == '3' || num[i] == '4' || num[i] == '5' || num[i] == '7')
                return false;
                if(num[i] == '6' && num[size-i-1] != '9')
                return false;
                if(num[i] == '9' && num[size-i-1] != '6')
                return false;
                if((num[i] != '6' && num[i] !='9') && num[i] != num[size-1-i])
                return false;
            }
            return true;
        }
    };
    

    This is a special case of palindrome.We need only think about 6 and 9.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.