Check string for palindrome

前端 未结 30 3532
悲哀的现实
悲哀的现实 2020-11-22 02:47

A palindrome is a word, phrase, number or other sequence of units that can be read the same way in either direction.

To check whether a word is a palindrome I get th

30条回答
  •  天涯浪人
    2020-11-22 03:13

    A concise version, that doesn't involve (inefficiently) initializing a bunch of objects:

    boolean isPalindrome(String str) {    
        int n = str.length();
        for( int i = 0; i < n/2; i++ )
            if (str.charAt(i) != str.charAt(n-i-1)) return false;
        return true;    
    }
    

提交回复
热议问题