Valid Palindrome
一边看覃超的斗鱼直播一遍写的:
- in place comparison: s[i] == s[n-i-1] ?
- reverse and compare
- stack, queue
对应第一种方法:
这题还可以直接用Character.isLetterOrDigit
。
已犯错误:
- while里面少了 i<=s.length() - 1
public boolean isPalindrome(String s) {if (s.length() < 2) return true;int i = 0, j = s.length() - 1;while (i < j) {while (i<=s.length() - 1 && !isNumber(s.charAt(i)) && !isAlpha(s.charAt(i))) {i++;}while (j>=0 && !isNumber(s.charAt(j)) && !isAlpha(s.charAt(j))) {j--;}if (i >= j) return true;if (isSame(s.charAt(i), s.charAt(j))) {i++;j--;} else {return false;}}return true;}public boolean isNumber(char c) {//要用单引号if (c >= '0' && c <= '9') return true;else return false;}public boolean isAlpha(char c) {if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') return true;else return false;}public boolean isSame(char a, char b) {if (isNumber(a) && isNumber(b)) {return a == b;} else if (Character.toLowerCase(a) == Character.toLowerCase(b)) return true;else return false;}
复制代码
第二种方法
常规的排除干扰的Palindrome的写法,先用正则过滤掉非法字符,再用head和tail指针对比。其实跟第一种差不多。
public class ValidPalindrome {public static boolean isValidPalindrome(String s){if(s==null||s.length()==0) return false;s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();System.out.println(s);for(int i = 0; i < s.length() ; i++){if(s.charAt(i) != s.charAt(s.length() - 1 - i)){return false;}}return true;}public static void main(String[] args) {String str = "A man, a plan, a canal: Panama";System.out.println(isValidPalindrome(str));}
}
复制代码
第三种方法:
用Stack,同样先把用正则过滤。这个应该用手写一遍的。
public boolean isPalindrome(String s) {s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();int len = s.length();if (len < 2)return true;Stack<Character> stack = new Stack<Character>();int index = 0;while (index < len / 2) {stack.push(s.charAt(index));index++;}if (len % 2 == 1)index++;while (index < len) {if (stack.empty())return false;char temp = stack.pop();if (s.charAt(index) != temp)return false;elseindex++;}return true;
}
复制代码
see : http://www.programcreek.com/2013/01/leetcode-valid-palindrome-java/
转载于:https://juejin.im/post/5a313420f265da431523e975
Valid Palindrome相关推荐
- LeetCode 125 Valid Palindrome(有效回文)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5062 ...
- [LeetCode] Palindrome Number Valid Palindrome - 回文系列问题
题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如1 ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- valid Palindrome -- leetcode
125 Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric ...
- LeetCode OJ - Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- LeetCode 125. Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- C#LeetCode刷题之#125-验证回文串(Valid Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...
- leetcode Valid Palindrome
题目连接 https://leetcode.com/problems/valid-palindrome/ Valid Palindrome Description Given a string, de ...
- 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...
- 验证字符串是否回文 Valid Palindrome
为什么80%的码农都做不了架构师?>>> 问题: Given a string, determine if it is a palindrome, considering on ...
最新文章
- codevs 1203 判断浮点数是否相等
- 拥抱开源,Office 365开发迎来新时代
- POJ 2356 Find a multiple (抽屉原理)
- python等差分解一个数
- 基于JSP/SERVLET学生管理系统
- 高性能mysql主存架构
- java还是c 2017_2017年9月编程语言排行榜:Java、C与C++三巨头还能统治排行榜多久?...
- JS结合Cookie实现验证码功能
- axure 小程序 lib_使用maven和fat jar/war运行应用程序的对比
- 下划线间隔数字 排序_面试必备:经典算法动画解析之希尔排序
- python 实现的键盘记录器 小功能
- 阿里测试人员:8年经验分享自学软件测试学习路线
- Leetcode 刷题笔记(二十八) ——动态规划篇之子序列问题:连续子序列和不连续子序列
- 微信批量扫码进群系统
- 计算机组成原理不恢复余数法,计算机组成原理第七讲(除法-原码恢复余数法)(科大罗克露)...
- 邮箱大师支持html,网易邮箱大师使用过程的常见问题
- 农村环境保护学习资料
- 全景图(三):在Unity3D上实现360°球面投影
- 奖金高达200万!武汉市便利店行业发展专项资金支持条件、材料及流程
- Pyflink系列之使用pyflink实现flink大数据引挚的经典案例wordcount
热门文章
- SFP端口是什么?有什么作用?可以接RJ45吗?
- poj 1064 java_poj1064
- 华硕飞行堡垒6打开tpm
- 那你讲一下IntentService
- Inspection info:Detects duplicates in source code
- OSChina 周三乱弹 ——身为同事就要互相伤害么?
- 通达信板块监控指标_通达信辅助判断指标公式行业板块看盘幅图
- python实现数据可视化软件_基于Python实现交互式数据可视化的工具
- python如何查看函数参数_如何找到Python函数的参数数?
- 微信小程序播放音频,ios静音状态下无声音、音频播放创建多个等问题