一边看覃超的斗鱼直播一遍写的:

  1. in place comparison: s[i] == s[n-i-1] ?
  2. reverse and compare
  3. stack, queue

对应第一种方法:

这题还可以直接用Character.isLetterOrDigit

已犯错误:

  1. 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相关推荐

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

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

  2. [LeetCode] Palindrome Number Valid Palindrome - 回文系列问题

    题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如1 ...

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

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

  4. valid Palindrome -- leetcode

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

  5. LeetCode OJ - Valid Palindrome

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

  6. LeetCode 125. Valid Palindrome

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

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

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

  8. leetcode Valid Palindrome

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

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

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

  10. 验证字符串是否回文 Valid Palindrome

    为什么80%的码农都做不了架构师?>>>    问题: Given a string, determine if it is a palindrome, considering on ...

最新文章

  1. codevs 1203 判断浮点数是否相等
  2. 拥抱开源,Office 365开发迎来新时代
  3. POJ 2356 Find a multiple (抽屉原理)
  4. python等差分解一个数
  5. 基于JSP/SERVLET学生管理系统
  6. 高性能mysql主存架构
  7. java还是c 2017_2017年9月编程语言排行榜:Java、C与C++三巨头还能统治排行榜多久?...
  8. JS结合Cookie实现验证码功能
  9. axure 小程序 lib_使用maven和fat jar/war运行应用程序的对比
  10. 下划线间隔数字 排序_面试必备:经典算法动画解析之希尔排序
  11. python 实现的键盘记录器 小功能
  12. 阿里测试人员:8年经验分享自学软件测试学习路线
  13. Leetcode 刷题笔记(二十八) ——动态规划篇之子序列问题:连续子序列和不连续子序列
  14. 微信批量扫码进群系统
  15. 计算机组成原理不恢复余数法,计算机组成原理第七讲(除法-原码恢复余数法)(科大罗克露)...
  16. 邮箱大师支持html,网易邮箱大师使用过程的常见问题
  17. 农村环境保护学习资料
  18. 全景图(三):在Unity3D上实现360°球面投影
  19. 奖金高达200万!武汉市便利店行业发展专项资金支持条件、材料及流程
  20. Pyflink系列之使用pyflink实现flink大数据引挚的经典案例wordcount

热门文章

  1. SFP端口是什么?有什么作用?可以接RJ45吗?
  2. poj 1064 java_poj1064
  3. 华硕飞行堡垒6打开tpm
  4. 那你讲一下IntentService
  5. Inspection info:Detects duplicates in source code
  6. OSChina 周三乱弹 ——身为同事就要互相伤害么?
  7. 通达信板块监控指标_通达信辅助判断指标公式行业板块看盘幅图
  8. python实现数据可视化软件_基于Python实现交互式数据可视化的工具
  9. python如何查看函数参数_如何找到Python函数的参数数?
  10. 微信小程序播放音频,ios静音状态下无声音、音频播放创建多个等问题