Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.

翻译:
给定一个字符串,检测它是否是回文对称的,只考虑其中字母与数字的字符。
例如”A man, a plan, a canal: Panama”是回文对称的,”race a car”不是。
提示:你考虑过字符串可能是空的情况吗?这是一个面试中应该问出的好问题。为了处理这个问题,我们假定空串是回文对称的。

判断回文数(字符串里有字符、数字、标点符号,需要去掉标点符号)

import java.util.*;

public class PalindromeNumber {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        //Scanner scan = new Scanner(System.in);

        //System.out.println("请输入数字:");

        //String strNum = scan.next();

String strNum="q1Qq1q";

        boolean result = isPalindrome(strNum);

        System.out.println(result);

    }

   

    public static Boolean isPalindrome(String str){

      char[] charArr=str.toCharArray();

      char[] ch=new char[charArr.length];

         int fix='a'-'A';

  for(int i=0;i<=charArr.length-1;i++){

  if((charArr[i]>='a'&&charArr[i]<='z')||(charArr[i]>='0'&&charArr[i]<='9')){

    ch[i]=charArr[i];

  }else if ((charArr[i]>='A'&&charArr[i]<='Z'))

    ch[i]=(char)(charArr[i]+fix);

        }

        boolean result=false;

   int m=0;

   int n=ch.length-1;

         while(m<=n){

  if(ch[m]==ch[n]){

  result=true;

  m++;

  n--;

  }else {

  return result=false;

}

}

        return result;

    }

}

猜你喜欢

转载自www.cnblogs.com/yayayou/p/10674524.html