题目:

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.

题意及分析:给出一个字符串,只考虑其中的字母或者数字字符,问字符串是否是一个回文,注意空字符串也是回文。

方法一:先遍历一次字符串去除非数字字母字符,然后判断是否是回文,复杂度为n+n/2

class Solution {public boolean isPalindrome(String s) {if(s == null || s.length() == 0 ) return true;StringBuilder sb = new StringBuilder();for(int i=0;i<s.length();i++){      //取出标点符号,只留下字母char ch = s.charAt(i);if(Character.isLetterOrDigit(ch)){sb.append(ch);}}//全部变为小写字母String newStr = sb.toString().toLowerCase();for(int i=0;i<newStr.length()/2;i++){if(newStr.charAt(i)!=newStr.charAt(newStr.length()-1-i))return false;}return true;}
}

方法二:直接判断,每次分别从前和从后找到一个字母数字字符,若不相等直接返回false,复杂度最多为n

class Solution {public boolean isPalindrome(String s) {if(s == null || s.length() == 0 ) return true;int i = 0,j = s.length()-1;for(i=0,j=s.length()-1;i<j;++i,--j){while(i<j && !Character.isLetterOrDigit(s.charAt(i))) i++;while(i<j && !Character.isLetterOrDigit(s.charAt(j))) j--;if(i<j && Character.toLowerCase(s.charAt(i))!=Character.toLowerCase(s.charAt(j))) return false;}return true;}
}

转载于:https://www.cnblogs.com/271934Liao/p/7765360.html

[LeetCode] 125. Valid Palindrome Java相关推荐

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

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

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

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

  3. LeetCode 125 Valid Palindrome(有效回文)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...

  4. LeetCode 125. Valid Palindrome

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. 125 Valid Palindrome

    125 Valid Palindrome 链接:https://leetcode.com/problems/valid-palindrome/ 问题描写叙述: Given a string, dete ...

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

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

  7. leetcode python3 简单题125. Valid Palindrome

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

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

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  9. LeetCode OJ - Valid Palindrome

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  10. 125. Valid Palindrome

    Title 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a pla ...

最新文章

  1. php怎样验证验证码对错,PHP生成中文验证码并检测对错实例
  2. 前端三十五:图片的基本概念
  3. python蒙特卡洛模拟_基于Python的21点游戏蒙特卡洛模拟
  4. linux安装常用命令工具包wget,cmake等
  5. Linux Enterprise Cluster选译
  6. Linux中vi命令替换字符串的操作
  7. windows上比较好用的截图软件+Gif录制软件+看图软件,建议收藏!
  8. vnc全屏界面怎么设置小_如何设置 才能 修改 vnc 有效窗口大小
  9. po层和vo层中po和vo是什么意思
  10. 从苏宁电器到卡巴斯基(第二部)第08篇:我在卡巴的日子 VIII
  11. 面向金融的R语言_L3
  12. linux添加mx记录,linux下nslookup操作实例,查找域名的a记录、mx记录、cname记录、ns记录...
  13. 草料二维码-免费的二维码生成工具
  14. 给幼儿园小朋友看的网络安全扫盲篇
  15. Linux man tar cn翻译
  16. DeepARG——一种基于深度学习更加准确预测ARG的方法
  17. 接口之----手机号验证接口api
  18. 第4章 数据可视化答案
  19. Go 编写开机自启动服务
  20. 双评价:新时期国土空间规划的前提与基础

热门文章

  1. Hyper-V 2016 系列教程40 使用 PowerShell 实现虚拟机自动化和管理虚拟机
  2. 复制class文件到as中出现非法字符,须要class,interface货enum
  3. es 测试数据进行 增删查改
  4. Java计算文件MD5值(支持大文件)
  5. Linux磁盘管理之df命令详解和使用实例(查看磁盘空间占用情况)
  6. 面试官:设计一个对外的接口,需要考虑那些安全问题?我有点懵...
  7. 新来的妹子把几百万数据放入了内存,系统立马爆了,我不得已做到了妹子傍边,手把手教妹子...
  8. 为什么要减少代码中该死的 if else 嵌套
  9. 移动开发 or web 前端?
  10. Big day coming...