力扣系列(三):判断回文数

2020-04-27  本文已影响0人  codeMover

给定一个整形数字,判断是否是回文串

    public static boolean judgeNumber(int num){
        if(num<0){
            return false;
        }
        int result = 0;
        int temp = num;
        while (temp != 0){
            result =result*10+temp%10;
            temp /=10;
        }
        return result == num;
    }

给定一个字符串,判断是否是回文串

public static boolean judgeString(String  str){
        char[] chars = str.toCharArray();
        for(int i=0;i<chars.length/2;i++){
            if(chars[i]!=chars[chars.length-1-i]){
                return false;
            }
        }
        return true;
    }
上一篇 下一篇

猜你喜欢

热点阅读