• [LeetCode#125]Valid Palindrome


    The problem:

    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.

    My analysis:

    This problem is not difficult, but we need to understand clearly about which cases are valid.
    1. In this problem, we only recognize the character in 'a - z' , 'A - Z', '0 - 9' as valid characters. We treat other characters as invalid, we don't take them into consideration, just skip them.
    We can separate a function to achieve this goal:
    String temp = s.toLowerCase();
    ...
    private boolean isValid(char c) {
        if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')
            return true;
        return false;
    }
    Skill: we always separte a function the check if a character is valid.
    
    2. Since we need to skip invalid characters, we may move poiters in the outside loop. 
    This kind of move is very dangerous, we should be very careful in the inner movemoent, they might exceed the boundary!!!
    while (ptr1 < ptr2) {
        while (!isValid(temp.charAt(ptr1))) {
            ptr1++; //it's dangerous!!!
            ...
        }
    }
    How to fix it? once we move the pointer, we should make check over the pointer. The pointer could reach following state!
    1. stop at a valid character, pointer1's position is different from pointer2's position.
    2. pointer1 is in collision with pointer2(at the same location).
    
    In situation 1, we could continue the invariant to check.
    In situation 2, we could directly return true, because:
    2.1 the previous checking over ptr1 and ptr2 must meet the standard of Palindrome, otherwise, we have already return false.
    2.2 when pointer1 is in collision with pointer2, they at the same position.
        tmep.charAt(ptr1) == temp.charAt(ptr2);

    My solution:

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0)
                return true;
            String temp = s.toLowerCase();
            int ptr1 = 0;
            int ptr2 = temp.length() - 1;
            while (ptr1 < ptr2) {//note the space and other commons, qutoes are allowed!
                while (!isValid(temp.charAt(ptr1))) {
                    ptr1++;
                    if (ptr1 == ptr2) return true;
                }
                while (!isValid(temp.charAt(ptr2))) {
                    ptr2--;
                    if (ptr1 == ptr2) return true;
                }
                if (temp.charAt(ptr1) != temp.charAt(ptr2))
                    return false;
                ptr1++;
                ptr2--;
            }
            return true;
        }
        
        private boolean isValid(char c) {
            if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')
                return true;
            return false;
        }
    }

    My improved analysis:

    In fact, the checking
    if (ptr1 == ptr2) return true;
    is the same as the outer while loop checking: while (ptr1 < ptr2), 
    we could include the inner checking into the outside checking, by adjusting the codes a little.
    while (!isValid(temp.charAt(ptr1))) {
        ptr1++;
        continue;
    }
    What a beautiful way!!!

    My improved solution:

    public class Solution {
        public boolean isPalindrome(String s) {
            if (s == null || s.length() == 0)
                return true;
            String temp = s.toLowerCase();
            int ptr1 = 0;
            int ptr2 = temp.length() - 1;
            while (ptr1 < ptr2) {//note the space and other commons, qutoes are allowed!
                while (!isValid(temp.charAt(ptr1))) {
                    ptr1++;
                    continue;
                }
                while (!isValid(temp.charAt(ptr2))) {
                    ptr2--;
                    continue;
                }
                if (temp.charAt(ptr1) != temp.charAt(ptr2))
                    return false;
                ptr1++;
                ptr2--;
            }
            return true;
        }
        
        private boolean isValid(char c) {
            if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')
                return true;
            return false;
        }
    }
  • 相关阅读:
    工资是用来支付给责任的,责任越大,工资越高。 涨工资,是因为承担了更大的责任。
    水平分库分表的关键问题及解决思路
    APP多版本共存,服务端如何兼容?
    ListView动态加载数据分页(使用Handler+线程和AsyncTask两种方法)
    Java 并发专题 :闭锁 CountDownLatch 之一家人一起吃个饭
    Java进阶 创建和销毁对象
    Java OCR tesseract 图像智能字符识别技术
    网页信息抓取进阶 支持Js生成数据 Jsoup的不足之处
    从原理角度解析Android (Java) http 文件上传
    android pop3与imap方式接收邮件(javamail)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4237856.html
Copyright © 2020-2023  润新知