From 814b2dd0ee1dedf00ad05c96081d869760d6aff4 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sat, 22 Jan 2022 07:15:03 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=201332?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\217\214\346\214\207\351\222\210.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 84 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 "LeetCode/1331-1340/1332. \345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\345\217\214\346\214\207\351\222\210.md" "b/Index/\345\217\214\346\214\207\351\222\210.md" index e2d53a17..72db2ae1 100644 --- "a/Index/\345\217\214\346\214\207\351\222\210.md" +++ "b/Index/\345\217\214\346\214\207\351\222\210.md" @@ -33,6 +33,7 @@ | [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 | | [1052. 爱生气的书店老板](https://leetcode-cn.com/problems/grumpy-bookstore-owner/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grumpy-bookstore-owner/solution/hua-dong-chuang-kou-luo-ti-by-ac_oier-nunu/) | 中等 | 🤩🤩🤩 | | [1221. 分割平衡字符串](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-wumnk/) | 简单 | 🤩🤩🤩🤩 | +| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 | | [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 | | [1610. 可见点的最大数目](https://leetcode-cn.com/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 | | [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 1c8cad3d..e93c92da 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -81,6 +81,7 @@ | [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 | | [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 | | [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 | +| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 | | [1436. 旅行终点站](https://leetcode-cn.com/problems/destination-city/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/destination-city/solution/gong-shui-san-xie-jian-dan-fang-jia-mo-n-y47c/) | 简单 | 🤩🤩🤩🤩🤩 | | [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 | | [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/1331-1340/1332. \345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1331-1340/1332. \345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..6486ffd0 --- /dev/null +++ "b/LeetCode/1331-1340/1332. \345\210\240\351\231\244\345\233\236\346\226\207\345\255\220\345\272\217\345\210\227\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,84 @@ +### 题目描述 + +这是 LeetCode 上的 **[1332. 删除回文子序列](https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-7okx/)** ,难度为 **简单**。 + +Tag : 「双指针」、「模拟」 + + + +给你一个字符串 `s`,它仅由字母 `'a'` 和 `'b'` 组成。每一次删除操作都可以从 `s` 中删除一个回文 **子序列**。 + +返回删除给定字符串中所有字符(字符串为空)的最小删除次数。 + +「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。 + +「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。 + +示例 1: +``` +输入:s = "ababa" + +输出:1 + +解释:字符串本身就是回文序列,只需要删除一次。 +``` +示例 2: +``` +输入:s = "abb" + +输出:2 + +解释:"abb" -> "bb" -> "". +先删除回文子序列 "a",然后再删除 "bb"。 +``` +示例 3: +``` +输入:s = "baabb" + +输出:2 + +解释:"baabb" -> "b" -> "". +先删除回文子序列 "baab",然后再删除 "b"。 +``` + +提示: +* $1 <= s.length <= 1000$ +* `s` 仅包含字母 `'a'`  和 `'b'` + +--- + +### 双指针 + +由于 `s` 只有字母 `a` 和 `b`,并且删除的是「子序列」,因此最大的删除次数为 $2$(先删除所有的 `a`,再删除所有的 `b`)。 + +同时 `s` 本身不为空串(不存在删除次数为 $0$ 的情况),因此如果我们不能一次删除的话(`s` 本身为回文),只能通过 $2$ 次进行删除。 + +代码: +```Java +class Solution { + public int removePalindromeSub(String s) { + int n = s.length(); + int i = 0, j = n - 1; + while (i < j) { + if (s.charAt(i) != s.charAt(j)) return 2; + i++; j--; + } + return 1; + } +} +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1332` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +