Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 860001e

Browse files
Merge pull request SharingSource#682 from SharingSource/ac_oier
✨feat: add 1624
2 parents 036e43c + bce3f88 commit 860001e

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

‎Index/模拟.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@
170170
| [1608. 特殊数组的特征值](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-array-with-x-elements-greater-than-or-equal-x/solution/by-ac_oier-z525/) | 简单 | 🤩🤩🤩🤩🤩 |
171171
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
172172
| [1619. 删除某些元素后的数组均值](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/) | 简单 | 🤩🤩🤩🤩 |
173-
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 |
173+
| [1624. 两个相同字符之间的最长子字符串](https://leetcode.cn/problems/largest-substring-between-two-equal-characters/) | [LeetCode 题解链接](https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/by-ac_oier-73w7/) | 简单 | 🤩🤩🤩🤩 |
174+
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](=https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/by-ac_oier-ki3t/) | 简单 | 🤩🤩🤩🤩🤩 |
174175
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
175176
| [1656. 设计有序流](https://leetcode.cn/problems/design-an-ordered-stream/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-an-ordered-stream/solution/by-ac_oier-5pe8/) | 简单 | 🤩🤩🤩🤩 |
176177
| [1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/) | 简单 | 🤩🤩🤩🤩 |

‎LeetCode/1621-1630/1622. 奇妙序列(困难).md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### 题目描述
22

3-
这是 LeetCode 上的 **[1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/)** ,难度为 **困难**
3+
这是 LeetCode 上的 **[1622. 奇妙序列]()** ,难度为 **困难**
44

55
Tag : 「线段树」
66

@@ -49,7 +49,7 @@ fancy.getIndex(2); // 返回 20
4949

5050
### 线段树(多个懒标记)
5151

52-
52+
使用多个懒标记来解决 `add``mul` 问题。
5353

5454
代码:
5555
```Java
@@ -136,7 +136,7 @@ class Fancy {
136136
}
137137
```
138138
* 时间复杂度:查询次数为 $m,ドル值域大小为 $n,ドル插入和查询复杂度均为 $O(\log{n})$
139-
* 空间复杂度:$O(m * \log{n})$
139+
* 空间复杂度:$O(m \times \log{n})$
140140

141141
---
142142

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1624. 两个相同字符之间的最长子字符串](https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/by-ac_oier-ki3t/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个字符串 `s`,请你返回 两个相同字符之间的最长子字符串的长度 ,计算长度时不含这两个字符。如果不存在这样的子字符串,返回 `-1`
10+
11+
子字符串 是字符串中的一个连续字符序列。
12+
13+
示例 1:
14+
```
15+
输入:s = "aa"
16+
17+
输出:0
18+
19+
解释:最优的子字符串是两个 'a' 之间的空子字符串。
20+
```
21+
示例 2:
22+
```
23+
输入:s = "abca"
24+
25+
输出:2
26+
27+
解释:最优的子字符串是 "bc" 。
28+
```
29+
示例 3:
30+
```
31+
输入:s = "cbzxy"
32+
33+
输出:-1
34+
35+
解释:s 中不存在出现出现两次的字符,所以返回 -1 。
36+
```
37+
示例 4:
38+
```
39+
输入:s = "cabbac"
40+
41+
输出:4
42+
43+
解释:最优的子字符串是 "abba" ,其他的非最优解包括 "bb" 和 "" 。
44+
```
45+
46+
提示:
47+
* 1ドル <= s.length <= 300$
48+
* `s` 只含小写英文字母
49+
50+
---
51+
52+
### 模拟
53+
54+
根据题意继续模拟即可:使用数组 `idxs` 记录下每个字符最开始出现的下标即可。
55+
56+
Java 代码:
57+
```Java
58+
class Solution {
59+
public int maxLengthBetweenEqualCharacters(String s) {
60+
int[] idxs = new int[26];
61+
Arrays.fill(idxs, 310);
62+
int n = s.length(), ans = -1;
63+
for (int i = 0; i < n; i++) {
64+
int u = s.charAt(i) - 'a';
65+
idxs[u] = Math.min(idxs[u], i);
66+
ans = Math.max(ans, i - idxs[u] - 1);
67+
}
68+
return ans;
69+
}
70+
}
71+
```
72+
Typescript 代码:
73+
```Typescript
74+
function maxLengthBetweenEqualCharacters(s: string): number {
75+
const idxs = new Array<number>(26).fill(310)
76+
let n = s.length, ans = -1
77+
for (let i = 0; i < n; i++) {
78+
const u = s.charCodeAt(i) - 'a'.charCodeAt(0)
79+
idxs[u] = Math.min(idxs[u], i)
80+
ans = Math.max(ans, i - idxs[u] - 1)
81+
}
82+
return ans
83+
};
84+
```
85+
Python 代码:
86+
```Python
87+
class Solution:
88+
def maxLengthBetweenEqualCharacters(self, s: str) -> int:
89+
idxs = {}
90+
ans = -1
91+
for i, c in enumerate(s):
92+
idxs[c] = idxs[c] if c in idxs else i
93+
ans = max(ans, i - idxs[c] - 1)
94+
return ans
95+
```
96+
* 时间复杂度:$O(n)$
97+
* 空间复杂度:$O(C),ドル其中 $C = 26$ 为字符集大小
98+
99+
---
100+
101+
### 最后
102+
103+
这是我们「刷穿 LeetCode」系列文章的第 `No.1624` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
104+
105+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
106+
107+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
108+
109+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
110+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /