美文网首页
算法---回文判断

算法---回文判断

作者: reedthinking | 来源:发表于2017-06-21 18:42 被阅读0次

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

public class Parlindrome {
    public boolean parlindrome(char[] str) {
        if (str == null || str.length == 0) {
            return false;
        }
        //指向头,尾
        int i = 0;
        int j = str.length - 1;
        //当指针相遇则是回文
        while (i < j) {
            //如果头尾所指不同,则不是回文
            if (str[i] != str[j]) {
                return false;
            }
            //向中间靠拢
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        Parlindrome parlindrome = new Parlindrome();
        System.out.println(parlindrome.parlindrome("stttttss".toCharArray()));
    }
}

相关文章

网友评论

      本文标题:算法---回文判断

      本文链接:https://www.haomeiwen.com/subject/oumtcxtx.html