Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ignoring cases.

Example

Example 1:

  1. Input: "A man, a plan, a canal: Panama"
  2. Output: true
  3. Explanation: "amanaplanacanalpanama"

Example 2:

  1. Input: "race a car"
  2. Output: false
  3. Explanation: "raceacar"

Challenge

O(n) time without extra memory.

Notice

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.

思路:

字符串中只考虑字母和数字是否构成一个回文串,no extra space可以定义两个指针。

注意:

  1. 两个指针命名,front 和 end,不要用i, j
  2. 同类中:方法与方法之间直接用方法名来互相调用。Java方法的调用。
  3. 多个if并列 和 else if 区别:If you have used multiple if statements then if the condition is true all will be executed. If you have used if and else if combination only one will be executed where first comes the true value 
    所以,如果把line 10 12 14 变成并列if,output会出错。因为如果连续出现两个或以上的非字母数字字符的话,就会输出false。比如 String s = a'+ba;
  4. boolean方法命名以is开头。
  5. line 9, 不能写成 while (front != end) , 举例 s = "aa", 指针front, end 交叉后会outOfIndexException.

代码:

  1. public class Solution {
  2.  
  3. public boolean isPalindrome(String s) {
  4. if (s == null || s.length() == 0)
  5. return true;
  6.  
  7. int front = 0;
  8. int end = s.length() - 1;
  9. while (front < end) {
  10. if (!isValid(s.charAt(front)))
  11. front++;
  12. else if (!isValid(s.charAt(end)))
  13. end--;
  14. else {
  15. if (isValidCase(s.charAt(front), s.charAt(end))){
  16. front++;
  17. end--;
  18. } else {
  19. return false;
  20. }
  21. }
  22.  
  23. }
  24. return true;
  25. }
  26.  
  27. public boolean isValid(char c) {
  28. return Character.isLetter(c) || Character.isDigit(c);
  29. }
  30. public boolean isValidCase(char a, char b) {
  31. if (a == b)
  32. return true;
  33. else if (Character.toUpperCase(a) == Character.toUpperCase(b))
  34. return true;
  35. else
  36. return false;
  37. }
  38. }

代码优化:

  1. public class Solution {
  2. public boolean isPalindrome(String s) {
  3. if (s == null || s.length() == 0) {
  4. return true;
  5. }
  6.  
  7. int front = 0;
  8. int end = s.length() - 1;
  9. while (front < end) {
  10. while (front < s.length() && !isvalid(s.charAt(front))) { // nead to check range of a/b
  11. front++;
  12. }
  13.  
  14. if (front == s.length()) { // for empty string “.,,,”
  15. return true;
  16. }
  17.  
  18. while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
  19. end--;
  20. }
  21.  
  22. if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
  23. return false;
  24. } else {
  25. front++;
  26. end--;
  27. }
  28. }
  29.  
  30. return true;
  31. }
  32.  
  33. private boolean isvalid (char c) {
  34. return Character.isLetter(c) || Character.isDigit(c);
  35. }
  36. }

Lintcode415-Valid Palindrome-Medium的更多相关文章

  1. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. 【leetcode】Valid Palindrome

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

  3. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. [LintCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  6. 25. Valid Palindrome

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

  7. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. Valid Palindrome [LeetCode]

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  10. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

随机推荐

  1. flask渲染模板时报错TypeError: 'UnboundField' object is not callable

    渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm ...

  2. scss简单用法

  3. P1258 小车问题

    P1258 小车问题 蒟蒻精神自强不息蒟蒻精神永不言败加油加油ヾ(◍°∇°◍)ノ゙yeah yeah yeah 据说这是道小学奥数题抱歉蒟蒻的我没学过奥数,算了大概三大张演草纸,不得不说这题对于蒟蒻本 ...

  4. mxnet下如何查看中间结果

    https://blog.csdn.net/disen10/article/details/79376631 固定权重:https://www.cnblogs.com/chenyliang/p/678 ...

  5. 【react懒加载组件】--react-lazyload

    组件安装: npm install react-lazyload --save-dev 组件使用: //引入 import LazyLoad from 'react-lazyload'; //rend ...

  6. c++实现“扫描检测硬件改动”

    这里需要用到cfgmgr32.h,参考了网上好几篇博文. #include <windows.h> #include <stdio.h> #include <cfgmgr ...

  7. java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy解决方法

    今天集成es-job到公司的框架时,启动时出现上述错误 java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy at storm. ...

  8. Angular 父子组件传值

    Angular 父子组件传值 @Input  @Output  @ViewChild 新建一个头部组件 newsheader 在主组件引用 news 组件,在news组件添加 newsheader 组 ...

  9. GDI的 点 线 面 双缓冲 位图的绘制

    1.输出文本 // 输出文本 ,,)); //设置字体颜色,但最后都要返回原来的字体格式 COLORREF clrBackColor = SetBkColor(hDC, RGB(,,)); //设置背 ...

  10. MSF基础应用

    实践目标 掌握metasploit的基本应用方式. 具体需要完成(1)ms08_067;(2)ms11_050:(3)Adobe(4)成功应用任何一个辅助模块. 报告 虚拟机:可以找我拷贝(我一般都在 ...