java 246. Strobogrammatic Number(1st).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 246. Strobogrammatic Number(1st).java相关的知识,希望对你有一定的参考价值。

public class Solution {
    public boolean isStrobogrammatic(String num) {
        if(num == null || num.length() == 0) return true;
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        map.put('0', '0');
        map.put('1', '1');
        map.put('8', '8');
        map.put('6', '9');
        map.put('9', '6');
        
        int start = 0;
        int end = num.length() - 1;
        while(start <= end) {
            char temp1 = num.charAt(start);
            char temp2 = num.charAt(end);
            if(map.containsKey(temp1) && map.get(temp1) == temp2) {
                start++;
                end--;
                continue;
            }
            return false;
        }
        return true;
    }
}
public class Solution {
    public boolean isStrobogrammatic(String num) {
        for (int i=0, j = num.length()-1; i <= j; i++, j--) {
            if(!"00 11 88 696".contains(num.charAt(i) + "" + num.charAt(j))) {
                return false;
            }
        }
        return true;
    }
}

以上是关于java 246. Strobogrammatic Number(1st).java的主要内容,如果未能解决你的问题,请参考以下文章

java 246. Strobogrammatic Number(1st).java

java 246. Strobogrammatic Number(1st).java

java 246. Strobogrammatic Number(1st).java

java 246. Strobogrammatic Number(1st).java

246.Strobogrammatic Number

Leetcode 246: Strobogrammatic Number