java if-else solution


  • 0
    2
    public class Solution {
        public boolean isStrobogrammatic(String num) {
            int lo = 0;
            int hi = num.length() - 1;
            while (lo <= hi) {
                if (num.charAt(lo) == '1') {
                    if (num.charAt(hi) != '1') {
                        return false;
                    }
                }
                else if (num.charAt(lo) == '6') {
                    if (num.charAt(hi) != '9') {
                        return false;
                    }
                }
                else if (num.charAt(lo) == '8') {
                    if (num.charAt(hi) != '8') {
                        return false;
                    }
                }
                else if (num.charAt(lo) == '9') {
                    if (num.charAt(hi) != '6') {
                        return false;
                    }
                }
                else if (num.charAt(lo) == '0') {
                    if (num.charAt(hi) != '0') {
                        return false;
                    }
                }
                else {
                    return false;
                }
                lo++;
                hi--;
            }
            return true;
        }
    }
    

Log in to reply
 

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