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;

        }

    }

  • 相关阅读:
    内网穿透教程
    深入浅出 TCP/IP 协议栈
    STM32CUBEMX配置RTC闹钟
    RT-Thread STM32 系列 BSP 制作教程
    RT-Thread 在stm小内存系列产品的nano+msh完整移植教程
    C语言字符串与数字相互转换
    SD卡 TF卡 接口引脚定义
    定位数据格式
    安信可ESP-12F连接阿里云教程
    电脑使用网络调试助手连接阿里云步骤
  • 原文地址:https://www.cnblogs.com/yayayou/p/10674524.html
Copyright © 2011-2022 走看看