LeetCode – 246. 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 class Solution {
    public boolean isStrobogrammatic(String num) {
        int i=0;
        int l=num.length() -1;
        while(i<=l){
            if(num.charAt(i) == '0'){
                if(num.charAt(l) != '0'){
                   return false; 
                }
            }
            else if(num.charAt(i) == '1'){
                if(num.charAt(l) != '1'){
                   return false; 
                }                
            }
            else if(num.charAt(i) == '8'){
                if(num.charAt(l) != '8'){
                   return false; 
                }                    
            }
            else if(num.charAt(i) == '6'){
                if(num.charAt(l) != '9'){
                   return false; 
                }                  
            }
            else if(num.charAt(i) == '9'){
                if(num.charAt(l) != '6'){
                   return false; 
                }                  
            }
            else{
                return false;
            }
            i++;
            l--;
        }
        return true;
    }
}

喜欢的话订阅一个呗~第一时间收到文章更新哟~

发表评论

电子邮件地址不会被公开。