Skip to content

Commit 712010a

Browse files
Merge pull request SharingSource#328 from SharingSource/ac_oier
✨feat: Add 1332
2 parents 68677b6 + 814b2dd commit 712010a

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Index/双指针.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| [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/) | 中等 | 🤩🤩🤩 |
3434
| [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/) | 中等 | 🤩🤩🤩 |
3535
| [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/) | 简单 | 🤩🤩🤩🤩 |
36+
| [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/) | 中等 | 🤩🤩🤩🤩 |
3637
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
3738
| [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/) | 困难 | 🤩🤩🤩🤩 |
3839
| [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/) | 中等 | 🤩🤩🤩🤩 |

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
| [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/) | 中等 | 🤩🤩🤩 |
8282
| [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/) | 简单 | 🤩🤩🤩🤩 |
8383
| [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/) | 简单 | 🤩🤩🤩🤩 |
84+
| [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/) | 中等 | 🤩🤩🤩🤩 |
8485
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
8586
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
8687
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### 题目描述
2+
3+
这是 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/)** ,难度为 **简单**
4+
5+
Tag : 「双指针」、「模拟」
6+
7+
8+
9+
给你一个字符串 `s`,它仅由字母 `'a'``'b'` 组成。每一次删除操作都可以从 `s` 中删除一个回文 **子序列**
10+
11+
返回删除给定字符串中所有字符(字符串为空)的最小删除次数。
12+
13+
「子序列」定义:如果一个字符串可以通过删除原字符串某些字符而不改变原字符顺序得到,那么这个字符串就是原字符串的一个子序列。
14+
15+
「回文」定义:如果一个字符串向后和向前读是一致的,那么这个字符串就是一个回文。
16+
17+
示例 1:
18+
```
19+
输入:s = "ababa"
20+
21+
输出:1
22+
23+
解释:字符串本身就是回文序列,只需要删除一次。
24+
```
25+
示例 2:
26+
```
27+
输入:s = "abb"
28+
29+
输出:2
30+
31+
解释:"abb" -> "bb" -> "".
32+
先删除回文子序列 "a",然后再删除 "bb"。
33+
```
34+
示例 3:
35+
```
36+
输入:s = "baabb"
37+
38+
输出:2
39+
40+
解释:"baabb" -> "b" -> "".
41+
先删除回文子序列 "baab",然后再删除 "b"。
42+
```
43+
44+
提示:
45+
* $1 <= s.length <= 1000$
46+
* `s` 仅包含字母 `'a'`  和 `'b'`
47+
48+
---
49+
50+
### 双指针
51+
52+
由于 `s` 只有字母 `a``b`,并且删除的是「子序列」,因此最大的删除次数为 $2$(先删除所有的 `a`,再删除所有的 `b`)。
53+
54+
同时 `s` 本身不为空串(不存在删除次数为 $0$ 的情况),因此如果我们不能一次删除的话(`s` 本身为回文),只能通过 $2$ 次进行删除。
55+
56+
代码:
57+
```Java
58+
class Solution {
59+
public int removePalindromeSub(String s) {
60+
int n = s.length();
61+
int i = 0, j = n - 1;
62+
while (i < j) {
63+
if (s.charAt(i) != s.charAt(j)) return 2;
64+
i++; j--;
65+
}
66+
return 1;
67+
}
68+
}
69+
```
70+
* 时间复杂度:$O(n)$
71+
* 空间复杂度:$O(1)$
72+
73+
---
74+
75+
### 最后
76+
77+
这是我们「刷穿 LeetCode」系列文章的第 `No.1332` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
78+
79+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
80+
81+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
82+
83+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
84+

0 commit comments

Comments
 (0)