zoukankan      html  css  js  c++  java
  • 680. Valid Palindrome II 对称字符串-可删字母版本

    [抄题]:

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

    Example 1:

    Input: "aba"
    Output: True
    

    Example 2:

    Input: "abca"
    Output: True
    Explanation: You could delete the character 'c'.

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    忘了数据结构吧,基本就是指针的问题 

    ispalindrome是封装的基础方法

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. 抓关键字:最多移动一位,后面的都必须符合,所以基础判断也要用while
    2. 默认情况一般是返回true

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    抓关键字:最多移动一位,后面的都必须符合,所以基础判断也要用while

    [复杂度]:Time complexity: O(n) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    ispaindrome函数里用了while:因为最多只允许一位发生变化

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    public class Solution {
        /**
         * @param s: a string
         * @return: nothing
         */
        public boolean validPalindrome(String s) {
            //cc
            if (s.length() == 0) {
                return true;
            }
            int l = 0, r = s.length() - 1;
            while ((l++) < (r--)) {
                if (s.charAt(l) != s.charAt(r)) {
                    return isPalindrome(s, l - 1, r) || isPalindrome(s, l, r + 1);
                }
            }
            return true;
        }
        
        public boolean isPalindrome(String s, int left, int right) {
            while ((left++) < (right--))
                if (s.charAt(left) != s.charAt(right)) return false;
            return true;
        }
    }
    View Code
  • 相关阅读:
    HTTP 返回状态代码详细解释
    丁一的作业
    getIntent().getExtras().clear()未清空Bundle的数据
    activity android:launchMode="singleTask" 没用重现启动activity的问题
    判断email格式
    判断网络是否可用
    修改系统语言
    生成UUID
    css reset file
    智能指针(auto_ptr)vc版
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8612682.html
Copyright © 2011-2022 走看看