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
  • 相关阅读:
    关于Servelet在Tomcat中执行的原理
    类变量被final修饰编译时结果确定变为宏
    本地无法连接远程服务器(Host is not allowed to connect to this MySQL server)解决办法(Windows)
    leetcode_227. 基本计算器 II
    leetcode_150. 逆波兰表达式求值
    leetcode_145. 二叉树的后序遍历
    leetcode_144. 二叉树的前序遍历
    leetcode_94. 二叉树的中序遍历
    leetcode_71. 简化路径
    1598. 文件夹操作日志搜集器
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8612682.html
Copyright © 2011-2022 走看看