zoukankan      html  css  js  c++  java
  • 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;

        }

    }

  • 相关阅读:
    UVa OJ 148 Anagram checker (回文构词检测)
    UVa OJ 134 LoglanA Logical Language (Loglan逻辑语言)
    平面内两条线段的位置关系(相交)判定与交点求解
    UVa OJ 130 Roman Roulette (罗马轮盘赌)
    UVa OJ 135 No Rectangles (没有矩形)
    混合函数继承方式构造函数
    html5基础(第一天)
    js中substr,substring,indexOf,lastIndexOf,split等的用法
    css的textindent属性实现段落第一行缩进
    普通的css普通的描边字
  • 原文地址:https://www.cnblogs.com/yayayou/p/10674524.html
Copyright © 2011-2022 走看看