检查回文串 [英] Check string for palindrome

查看:176
本文介绍了检查回文串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个回文是一个词,短语,数字或单位其他序列中可以读取相同的方式无论是方向。

要检查一个字是回文我得到了这个词的字符数组和比较字符。我测试了它,它似乎工作。不过,我想知道这是否是正确的,或者有什么改善。

下面是我的code:

 公共类Aufg1 {
    公共静态无效的主要(字串[] args){
        字符串麦汁=reliefpfpfeiller;
        的char [] = warray wort.toCharArray();
        的System.out.println(istPalindrom(warray));
    }    公共静态布尔istPalindrom(的char []麦汁){
        布尔回文= FALSE;
        如果(wort.length%2 == 0){
            的for(int i = 0; I< wort.length / 2-1;我++){
                如果(麦芽汁由[i]!=麦汁[wort.length-I-1]){
                    返回false;
                }其他{
                    回文= TRUE;
                }
            }
        }其他{
            对(INT I = 0; I&≤(wort.length-1)/ 2-1;我++){
                如果(麦芽汁由[i]!=麦汁[wort.length-I-1]){
                    返回false;
                }其他{
                    回文= TRUE;
                }
            }
        }
        返回回文;
    }
}


解决方案

为什么不干脆:

 公共静态布尔istPalindrom(的char []字){
    INT的i1 = 0;
    INT I2 = word.length - 1;
    而(I2> I1){
        如果(字[I1]!=字[12]){
            返回false;
        }
        ++ I1;
        --i2;
    }
    返回true;
}

示例:

输入为andna。结果
I1将是0和i2会4.

第一循环迭代,我们将比较字[0] 字[4] 。他们是平等的,所以我们增加的i1(它现在1)和减量12(它现在3)。结果
因此,我们再比较n个的。他们是平等的,所以我们增加的i1(它现在2)和减量12(这是2)。结果
现在I1和I2是相等的(他们都是2),因此对于while循环的条件不再为true,因此循环终止,我们返回true。

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 the char array of the word and compare the chars. I tested it and it seems to work. However I want to know if it is right or if there is something to improve.

Here is my code:

public class Aufg1 {
    public static void main(String[] args) {
        String wort = "reliefpfpfeiller";
        char[] warray = wort.toCharArray(); 
        System.out.println(istPalindrom(warray));       
    }

    public static boolean istPalindrom(char[] wort){
        boolean palindrom = false;
        if(wort.length%2 == 0){
            for(int i = 0; i < wort.length/2-1; i++){
                if(wort[i] != wort[wort.length-i-1]){
                    return false;
                }else{
                    palindrom = true;
                }
            }
        }else{
            for(int i = 0; i < (wort.length-1)/2-1; i++){
                if(wort[i] != wort[wort.length-i-1]){
                    return false;
                }else{
                    palindrom = true;
                }
            }
        }
        return palindrom;
    }
}

解决方案

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.

这篇关于检查回文串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆