检查字符串是否是Java中的回文结构

时间:2015-01-17 15:41:36

标签: java string algorithm palindrome

我正在使用一个简单的函数来检查字符串是否是回文。但程序进入无限循环。

public static boolean checkPalindrome(String s){

        boolean check = true;
        int mid = s.length()/2;
        int j = s.length() -1;
        int i = 0;
        if (s.length()%2 == 0) {
            while(i <= mid){
                if (s.charAt(i) != s.charAt(j)){
                    check = false;
                    j--;
                    i++;
                }
            }
        }else if(s.length()%2 != 0){
            while(i < mid +1 ){
                if (s.charAt(i) == s.charAt(j)){
                    check = false;
                    j--;
                    i++;
                }
            }
        }
        return check;

    }

5 个答案:

答案 0 :(得分:4)

你需要将i的增量和j的减量移出内部if条件,它不会进入无限循环。

答案 1 :(得分:2)

为什么不这样做:

String rev = new StringBuilder(s).reverse().toString();
return rev.equals(s);

可替换地:

int len = s.length();
for (int i = 0; i < len; i++)
    if (s.charAt(i) != s.charAt(len - i - 1)) return false;
return true;

答案 2 :(得分:0)

请尝试以下代码:

public class Palindrome {
private static Scanner input;

public static void main(String args[]) {
    System.out.print("Enter a string:");
    input = new Scanner(System.in);
    String str = input.nextLine();

    System.out.println(palindromeLoop(str));
    System.out.println(palindromeCheck(str));
    System.out.println(palindromeRecursion(str));
}

// Using StringBuffer
public static String palindromeCheck(String str) {
    StringBuffer strName = new StringBuffer(str);
    strName.reverse();

    if (strName.toString().equals(str)) {
        return "The String is a Palindrome";
    } else {
        return "Not a Palindrome!!!";
    }
}

// Using For-Loop
public static String palindromeLoop(String str) {
    String original = str;
    String reverse = "";

    for (int i = str.length() - 1; i >= 0; i--) {
        reverse = reverse + original.charAt(i);
    }

    if (original.equals(reverse)) {
        return "The String is a Palindrome";
    } else {
        return "Not a Palindrome!!!";
    }
}

// Using Recursion
public static String palindromeRecursion(String str) {
    if (str.length() <= 1) {
        return "The String is a Palindrome";
    } else if (str.charAt(0) != str.charAt(str.length() - 1)) // Base case
        return "Not a Palindrome!!!";
    else
        return palindromeRecursion(str.substring(1, str.length() - 1));
}

答案 3 :(得分:0)

class CheckPalindrome {
    boolean isPalindrome(String text) {
        int i = 0;
        int j = text.length() - 1;
        while (i < j) {
            while (!Character.isLetter(text.charAt(i)) && i < j) {
                ++i;
            }
            while (!Character.isLetter(text.charAt(j)) && i < j) {
                --j;
            }
            if (Character.toLowerCase(text.charAt(i++)) != Character.toLowerCase(text.charAt(j--))) {
                return false;
            }
        }
        return true;
    }
}




 @Test
    public void isPalindrome() throws Exception {

        CheckPalindrome checkPalindrome = new CheckPalindrome();
        String text = "No, it is opposition";
        assertTrue(checkPalindrome.isPalindrome(text));

    }

答案 4 :(得分:0)

实际上只能通过使用for循环和很少的lists来做到这一点,如下所示:

public class Palindrome {

    public static void main(String[] args) {

        String str = "racecar";
        isPalindrome(str);

    }

    public static boolean isPalindrome(String str) {

        // if number is odd
        if (str.length() % 2 != 0) {
            List<String> counter = new ArrayList<>();

            for (int i = 0; i <= (str.length() - 1) / 2; i++) {
                for (int j = str.length() - 1; j >= (str.length() - 1) / 2; j--) {
                    if (str.charAt(i) == str.charAt(j)) {
                        counter.add("+");
                    }

                }
            }
            System.out.println(counter.toString());
            if (counter.size() == (str.length() + 1) / 2) {
                System.out.println("String is an odd palindrome");
            } else
                System.out.println("String is not a palindrome");
        }
        // if number is even
        else {
            List<String> counter_even = new ArrayList<>();

            for (int i = 0; i < (str.length()) / 2; i++) {
                for (int j = str.length() - 1; j >= (str.length()) / 2; j--) {
                    if (str.charAt(i) == str.charAt(j)) {
                        counter_even.add("+");
                    }

                }
            }
            System.out.println(counter_even.toString());
            if (counter_even.size() == (str.length()) / 2) {
                System.out.println("String is an even palindrome");
            } else
                System.out.println("String is not a palindrome");
        }
        return false;

    }

}

在这里,我们基本上将字符串拆分,并将其上半部分与下半部分进行比较。如果它们彼此相同,则我们通过在两个半部的每个相同字母上加“ +”来增加列表。如果我们列表中'+'的数量(只要可以帮助我们计数,可以用任何随机字母代替)等于我们的奇数字符串的(str.length() + 1) / 2的结果,或者等于{ {1}}表示我们的偶数字符串,那么我们可以得出结论,我们的字符串是回文。