判断是否为回文数

/**递归实现
     * 判断是否是回文数
     * String s = "a,b,c,d,e,e,d,c,b,a";
     * @param str
     * @return
     */
    public static boolean loop(String str) {
        if (str.length() < 2) {
            return true;
        }
        char[] array = str.toCharArray();
        if (str.length() == 3) {
            if (array[0] == array[2])
                return true;
        }
        char first = array[0];
        char last = array[array.length - 1];
        if (first == last)
            return loop(str.substring(2, array.length - 2));
        return false;
    }

 循环判断实现

   @Test
    public void test2() {
      String string="abc";
        System.out.println(isDiff("aba"));
    }

    public boolean isDiff(String string){
        if(string.length()<=1){
            return false;
        }
        if(string.length() == 2){
            return true;
        }
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length/2; i++) {
            if(chars[i] != chars[chars.length-i-1]){
                return false;
            }
        }
        return true;
    }

 

posted @ 2018-12-09 15:10  冬马党  阅读(174)  评论(0编辑  收藏  举报