Check string for palindrome

前端 未结 30 3011
悲哀的现实
悲哀的现实 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:12

    Why not just:

    public static boolean istPalindrom(char[] word){
        int i1 = 0;
        int i2 = word.length - 1;
        while (i2 > i1) {
            if (word[i1] != word[i2]) {
                return false;
            }
            ++i1;
            --i2;
        }
        return true;
    }
    

    Example:

    Input is "andna".
    i1 will be 0 and i2 will be 4.

    First loop iteration we will compare word[0] and word[4]. They're equal, so we increment i1 (it's now 1) and decrement i2 (it's now 3).
    So we then compare the n's. They're equal, so we increment i1 (it's now 2) and decrement i2 (it's 2).
    Now i1 and i2 are equal (they're both 2), so the condition for the while loop is no longer true so the loop terminates and we return true.

    0 讨论(0)
  • 2020-11-22 03:12

    IMO, the recursive way is the simplest and clearest.

    public static boolean isPal(String s)
    {   
        if(s.length() == 0 || s.length() == 1)
            return true; 
        if(s.charAt(0) == s.charAt(s.length()-1))
           return isPal(s.substring(1, s.length()-1));                
       return false;
    }
    
    0 讨论(0)
  • 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;    
    }
    
    0 讨论(0)
  • 2020-11-22 03:13

    Alternatively, recursion.

    For anybody who is looking for a shorter recursive solution, to check if a given string satisfies as a palindrome:

    private boolean isPalindrome(String s) {
        int length = s.length();
    
        if (length < 2) // If the string only has 1 char or is empty
            return true;
        else {
            // Check opposite ends of the string for equality
            if (s.charAt(0) != s.charAt(length - 1))
                return false;
            // Function call for string with the two ends snipped off
            else
                return isPalindrome(s.substring(1, length - 1));
        }
    }
    

    OR even shorter, if you'd like:

    private boolean isPalindrome(String s) {
        int length = s.length();
        if (length < 2) return true;
        return s.charAt(0) != s.charAt(length - 1) ? false :
                isPalindrome(s.substring(1, length - 1));
    }
    
    0 讨论(0)
  • 2020-11-22 03:15

    Recently I wrote a palindrome program which doesn't use StringBuilder. A late answer but this might come in handy to some people.

    public boolean isPalindrome(String value) {
        boolean isPalindrome = true;
        for (int i = 0 , j = value.length() - 1 ; i < j ; i ++ , j --) {
            if (value.charAt(i) != value.charAt(j)) {
                isPalindrome = false;
            }
        }
        return isPalindrome;
    }
    
    0 讨论(0)
  • 2020-11-22 03:15

    Amazing how many different solutions to such a simple problem exist! Here's another one.

    private static boolean palindrome(String s){
        String revS = "";
        String checkS = s.toLowerCase();
        String[] checkSArr = checkS.split("");
    
        for(String e : checkSArr){
            revS = e + revS;
        }
    
        return (checkS.equals(revS)) ? true : false;
    }
    
    0 讨论(0)
提交回复
热议问题