[LeetCode] 125. Valid Palindrome Java
题目:
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相关推荐
- 【回文串3】LeetCode 125. Valid Palindrome
LeetCode 125. Valid Palindrome Solution1:我的答案 复杂度为O(n)O(n)O(n),写法不是很简练啊. class Solution { public:boo ...
- [勇者闯LeetCode] 125. Valid Palindrome
[勇者闯LeetCode] 125. Valid Palindrome Description Given a string, determine if it is a palindrome, con ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...
- LeetCode 125. Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 125 Valid Palindrome
125 Valid Palindrome 链接:https://leetcode.com/problems/valid-palindrome/ 问题描写叙述: Given a string, dete ...
- 【回文串14】LeetCode 680. Valid Palindrome II
LeetCode 680. Valid Palindrome II Solution1:我的答案 复杂度是O(n)O(n)O(n),不算好啊.. 注意:对于c++中string对象常用的insert( ...
- leetcode python3 简单题125. Valid Palindrome
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百二十五题 (1)题目 英文: Given a string, determin ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- LeetCode OJ - Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 125. Valid Palindrome
Title 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a pla ...
最新文章
- php怎样验证验证码对错,PHP生成中文验证码并检测对错实例
- 前端三十五:图片的基本概念
- python蒙特卡洛模拟_基于Python的21点游戏蒙特卡洛模拟
- linux安装常用命令工具包wget,cmake等
- Linux Enterprise Cluster选译
- Linux中vi命令替换字符串的操作
- windows上比较好用的截图软件+Gif录制软件+看图软件,建议收藏!
- vnc全屏界面怎么设置小_如何设置 才能 修改 vnc 有效窗口大小
- po层和vo层中po和vo是什么意思
- 从苏宁电器到卡巴斯基(第二部)第08篇:我在卡巴的日子 VIII
- 面向金融的R语言_L3
- linux添加mx记录,linux下nslookup操作实例,查找域名的a记录、mx记录、cname记录、ns记录...
- 草料二维码-免费的二维码生成工具
- 给幼儿园小朋友看的网络安全扫盲篇
- Linux man tar cn翻译
- DeepARG——一种基于深度学习更加准确预测ARG的方法
- 接口之----手机号验证接口api
- 第4章 数据可视化答案
- Go 编写开机自启动服务
- 双评价:新时期国土空间规划的前提与基础
热门文章
- Hyper-V 2016 系列教程40 使用 PowerShell 实现虚拟机自动化和管理虚拟机
- 复制class文件到as中出现非法字符,须要class,interface货enum
- es 测试数据进行 增删查改
- Java计算文件MD5值(支持大文件)
- Linux磁盘管理之df命令详解和使用实例(查看磁盘空间占用情况)
- 面试官:设计一个对外的接口,需要考虑那些安全问题?我有点懵...
- 新来的妹子把几百万数据放入了内存,系统立马爆了,我不得已做到了妹子傍边,手把手教妹子...
- 为什么要减少代码中该死的 if else 嵌套
- 移动开发 or web 前端?
- Big day coming...