zoukankan      html  css  js  c++  java
  • Lintcode415-Valid Palindrome-Medium

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

    Example

    Example 1:

    Input: "A man, a plan, a canal: Panama"
    Output: true
    Explanation: "amanaplanacanalpanama"
    

    Example 2:

    Input: "race a car"
    Output: false
    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 }

    代码优化:

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0) {
                return true;
            }
    
            int front = 0;
            int end = s.length() - 1;
            while (front < end) {
                while (front < s.length() && !isvalid(s.charAt(front))) { // nead to check range of a/b
                    front++;
                }
    
                if (front == s.length()) { // for empty string “.,,,”     
                    return true; 
                }           
    
                while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
                    end--;
                }
    
                if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
                    return false;
                } else {
                    front++;
                    end--;
                }
            }
    
            return true; 
        }
    
        private boolean isvalid (char c) {
            return Character.isLetter(c) || Character.isDigit(c);
        }
    }
  • 相关阅读:
    Oracle-通过创建索引加快SQL执行效率
    Oracle-DG,MRP进程无法正常应用问题处理,重启大法好
    Oracle-DG,12c pdb创建测试
    Oracle-DG,疑问主库添加日志后,备库未操作主库日志比备库日志数量多,有什么影响?
    Oracle-DG疑问,什么情况下主库会发出一个会话连接备库
    Oracle-DG 主库将log_archive_dest_state_2远程归档线程参数设置为defer,为什么dg还是处于实时同步状态?
    Oracle-rm误删除数据文件,如何强制删除文件启动db
    Oracle-buffer cache过小导致SQL执行时间长
    win10下完全卸载-重装MySQL
    VSCode配置详细教程
  • 原文地址:https://www.cnblogs.com/Jessiezyr/p/10643865.html
Copyright © 2011-2022 走看看