描述
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? is is a good question to ask during an
interview.
For the purpose of this problem, we define empty string as valid palindrome.

分析
去标点符号,看是不是回文
代码

 1 public class ValidPalindrome {
 2     public static void main(String[] args) {
 3         String str= "A man, a plan, a canal: Panama";
 4         System.out.println(validPalindrome(str));
 5     }
 6     public static boolean validPalindrome(String str) {
 7         if (str=="")
 8             return true;
 9 //        String regex= "/[^\\u4e00-\\u9fa5\\w]/g";
10         String regex= "[\\p{Punct}\\p{Space}]+"; //去标点符号的正则表达式, 但不能去“”和中文标点
11         str=str.replaceAll(regex,"");
12         String strlow=str.toLowerCase();
13 //        return strlow;
14         char[] ch=strlow.toCharArray();
15
16         for(int i=0;i<ch.length;i++) {
17             int j=ch.length-1-i;
18             if(i<=j) {
19                if(ch[i]==ch[j]) {
20                    continue;
21                }else {
22                    return false;
23                }
24             }
25         }
26         return true;
27     }
28 }

方法2(转载)

不用正则

 1 public static boolean isPalindrome(String s) {
 2                       if(s.length()==0)
 3                           return true;
 4
 5                       s = s.toUpperCase();
 6                       int low1 = 'A', high1 = 'Z';
 7                       int low2 = '0', high2 = '9';
 8                       int low = 0, high = s.length()-1;
 9
10                      while(low < high){
11                          if((s.charAt(low)<low1||s.charAt(low)>high1)
12                              && (s.charAt(low)<low2||s.charAt(low)>high2)){
13                                  low++;
14                                  continue;
15                              }
16
17                          if((s.charAt(high)<low1||s.charAt(high)>high1)
18                              && (s.charAt(high)<low2||s.charAt(high)>high2)){
19                                  high--;
20                                  continue;
21                              }
22                          if(s.charAt(low) == s.charAt(high)){
23                              low++;
24                              high--;
25                          }else
26                              return false;
27                      }
28                      return true;
29                  }
30         

转载于:https://www.cnblogs.com/ncznx/p/9170416.html

Valid Palindrome LeetCode Java相关推荐

  1. valid Palindrome -- leetcode

    125 Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric ...

  2. Valid Sudoku leetcode java

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  3. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  4. C#LeetCode刷题之#125-验证回文串(Valid Palindrome)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...

  5. leetcode Valid Palindrome

    题目连接 https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Description Given a string, de ...

  6. 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...

  7. leetcode python3 简单题125. Valid Palindrome

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十五题 (1)题目 英文: Given a string, determin ...

  8. 【回文串14】LeetCode 680. Valid Palindrome II

    LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...

  9. 【回文串3】LeetCode 125. Valid Palindrome

    LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊. class Solution { public:boo ...

  10. [勇者闯LeetCode] 125. Valid Palindrome

    [勇者闯LeetCode] 125. Valid Palindrome Description Given a string, determine if it is a palindrome, con ...

最新文章

  1. 【leetcode】1032. Stream of Characters
  2. android游戏加载,Android 游戏引擎libgdx 资源加载进度百分比显示案例分析
  3. gitHub网站上常见英语翻译
  4. 用CSS实现的模式窗口效果,弹出固定大小的窗口
  5. 计算机考研新大纲,2020考研计算机新大纲考情分析
  6. python继承多重继承
  7. 百度顶会论文复现(1):课程概述
  8. extern C用法 - 笔试题
  9. 103. SPL 标志库
  10. Spring+Stomp+ActiveMq实现websocket长连接
  11. android 浏览器抓包工具下载,WebSee app下载-WebSee抓包工具v1.2.1 安卓版-腾牛安卓网...
  12. HarmonyOS无法添加outlook日历(报错该账户名称已被使用)
  13. 机器学习(四):决策树绘画(基础篇)
  14. 著者四角号码查询_著者姓名汉语拼音与四角号码数字混编书次号的研究
  15. 一篇文章带你发中文核心期刊《计算机科学》
  16. 一阶电路暂态响应的结果分析。_《电路原理》——相量法
  17. 2021年上半年最可靠的计算机领域投稿资源---着急毕业的同学必看
  18. 业务中台、技术中台、数据中台、AI中台
  19. 观点:开放式元宇宙将释放每个人的创造力
  20. 狼性团队五要素:沟通+信任+慎重+换位+快乐

热门文章

  1. Redis配置和持久性
  2. 【好书试读】Docker全攻略
  3. videojs--跨浏览器的HTML视频播放器(可自定义样式)
  4. Sleep v.s. sleep
  5. SSH框架 openSessionInView的配置
  6. 使用git bash提交代码到github托管
  7. Android View的生命周期详解
  8. 一个必用的javascript框架:underscore.js - wine的思考 - ITeye技术网站
  9. NSA的各种***工具
  10. Idea 进行断点调试的 快捷键