您的位置 首页 JAVA(2017)

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;
    }
}
看完了?留个评分呗?
[0人评了分,平均: 0/5]

本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注:

原文来源:《LeetCode – 246. Strobogrammatic Number》

发表评论

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

返回顶部