合法回文(字符串)

问题描述

给定一个字符串,判断是不是回文字符串。只考虑字符串中的数字、字母,并且同一个字母的大写和小写是等价的。
例如:
A man, a plan, a canal: Panama 是回文。
race a car 不是回文。
注意:在这道题目中,我们认为空串是回文字符串。

输入格式

输入有多行,以 EOF 结束。每行输入一个长度不超过 500 的字符串。

输出格式

对于每行输入,判断是否是符合题目要求的回文。如果是,则输出一行 true;否则输出一行 false

代码

参考代码一

package javaexam;

import java.util.Scanner;

public class Palindrome
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        while(input.hasNext())
        {
            boolean flag = true;
            String line = input.nextLine().toLowerCase();
            int left = 0;
            int right = line.length() - 1;
            
            while(left < right)
            {
                // 若非字母和数字,跳过
                while(!isCharNum(line.charAt(left)) && left < right)
                    ++left;
                while(!isCharNum(line.charAt(right)) && left < right)
                    --right;
                
                if((left < right) && (line.charAt(left++) != line.charAt(right--)))
                {
                    flag = false;
                    break;
                }
            }
            System.out.println(flag ? "true" : "false");
        }
    }
    
    // 也可用库函数 Character.isLetter(Char) || Character.isDigit(Char)
    static boolean isCharNum(char c)
    {
        if(('0' <= c && c <= '9') || ('a' <= c && c <= 'z'))
            return true;
        else
            return false;
    }
}

参考代码二

import java.util.Scanner;

public class Main
{
    public static void palia(String str)
    {
        // 把字符串str中的大写转小写
        str = str.toLowerCase();

        // 统计字符串str中字母和数字加起来一共有多少个,以便初始化ch字符数组大小。
        int count = 0;
        for (int i = 0; i < str.length(); ++i)
        {
            char temp = str.charAt(i);
            if ((temp >= '0' && temp <= '9') || (temp >= 'a' && temp <= 'z'))
            {
                ++count;
            } else
                continue;
        }

        char[] ch = new char[count]; // 初始化字符数组ch大小为str中字母和数字的总和

        // 把字符串str中的所有字符和数字依次放入字符数组中
        for (int i = 0, j = 0; i < str.length(); ++i)
        {
            char temp = str.charAt(i);
            if ((temp >= '0' && temp <= '9') || (temp >= 'a' && temp <= 'z'))
            {
                ch[j++] = temp;
            } else
                continue;
        }

        // 判断字符数组ch中的内容是否满足回文条件
        for (int i = 0, j = count - 1; i < j; ++i, --j)
        {
            if (ch[i] == ch[j])
            {
                continue;
            } else
            {
                System.out.println("false");
                return;
            }
        }
        System.out.println("true");
    }

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        while (input.hasNext())
        {
            String str = input.nextLine();
            palia(str);
        }
    }
}

测试样例

样例一:

A man, a plan, a canal: Panama
true

样例二:

race a car
false

猜你喜欢

转载自www.cnblogs.com/narisu/p/9057911.html