Skip to content

Commit 81c6d77

Browse files
committed
四刷3
1 parent c3856a7 commit 81c6d77

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

docs/0003-longest-substring-without-repeating-characters.adoc

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11

22
[#0003-longest-substring-without-repeating-characters]
3-
= 3. Longest Substring Without Repeating Characters
3+
= 3. 无重复字符的最长子串
44

5-
{leetcode}/problems/longest-substring-without-repeating-characters/[LeetCode - Longest Substring Without Repeating Characters^]
5+
https://leetcode.cn/problems/longest-substring-without-repeating-characters/[LeetCode - 3. 无重复字符的最长子串 ^]
66

7-
Given a string, find the length of the *longest substring* without repeating characters.
7+
给定一个字符串 `s` ,请你找出其中不含有重复字符的 **最长子串** 的长度。
88

9+
*示例 1:*
910

10-
.Example 1:
11-
[subs="verbatim,quotes,macros"]
12-
----
13-
*Input:* "abcabcbb"
14-
*Output:* 3
15-
*Explanation:* The answer is `"abc"`, with the length of 3.
16-
----
11+
....
12+
输入: s = "abcabcbb"
13+
输出: 3
14+
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
15+
....
1716

17+
*示例 2:*
1818

19-
.Example 2:
20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* "bbbbb"
23-
*Output:* 1
24-
*Explanation:* The answer is `"b"`, with the length of 1.
25-
----
19+
....
20+
输入: s = "bbbbb"
21+
输出: 1
22+
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
23+
....
2624

25+
*示例 3:*
2726

28-
.Example 3:
29-
[subs="verbatim,quotes,macros"]
30-
----
31-
*Input:* "pwwkew"
32-
*Output:* 3
33-
*Explanation:* The answer is `"wke"`, with the length of 3.
34-
Note that the answer must be a *substring*, `"pwke"` is a _subsequence_ and not a substring.
35-
----
27+
....
28+
输入: s = "pwwkew"
29+
输出: 3
30+
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
31+
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
32+
....
33+
34+
35+
*提示:*
36+
37+
* `0 \<= s.length \<= 5 * 10^4^`
38+
* `s` 由英文字母、数字、符号和空格组成
3639
3740
== 思路分析
3841

@@ -90,6 +93,15 @@ include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters_2.java[tag
9093
include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters_3.java[tag=answer]
9194
----
9295
--
96+
97+
四刷::
98+
+
99+
--
100+
[{java_src_attr}]
101+
----
102+
include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters_4.java[tag=answer]
103+
----
104+
--
93105
====
94106

95107
== 参考资料

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ endif::[]
100100
|{doc_base_url}/0124-binary-tree-maximum-path-sum.adoc[题解]
101101
|⭕️ 深度优先搜索,注意处理负数情况
102102

103+
|{counter:codes2503}
104+
|{leetcode_base_url}/longest-substring-without-repeating-characters/[3. 无重复字符的最长子串^]
105+
|{doc_base_url}/0003-longest-substring-without-repeating-characters.adoc[题解]
106+
|✅ 滑动窗口
107+
103108
|===
104109

105110
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _0003_LongestSubstringWithoutRepeatingCharacters_4 {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-04-01 17:03:21
11+
*/
12+
public int lengthOfLongestSubstring(String s) {
13+
if (s == null || s.isEmpty()) {
14+
return 0;
15+
}
16+
int result = 0;
17+
Map<Character, Integer> counter = new HashMap<>();
18+
int left = 0, right = 0;
19+
char[] chars = s.toCharArray();
20+
while (right < chars.length) {
21+
char rc = chars[right];
22+
right++;
23+
int cnt = counter.getOrDefault(rc, 0) + 1;
24+
counter.put(rc, cnt);
25+
if (cnt == 1) {
26+
// 这里使用 right - left (right已经++了)更好
27+
result = Math.max(result, counter.size());
28+
}
29+
while (left < chars.length && counter.get(rc) > 1) {
30+
char lc = chars[left];
31+
left++;
32+
int nc = counter.get(lc) - 1;
33+
if (nc == 0) {
34+
counter.remove(lc);
35+
} else {
36+
counter.put(lc, nc);
37+
}
38+
}
39+
}
40+
return result;
41+
}
42+
// end::answer[]
43+
44+
public static void main(String[] args) {
45+
new _0003_LongestSubstringWithoutRepeatingCharacters_4()
46+
.lengthOfLongestSubstring("pwwkew");
47+
}
48+
}

0 commit comments

Comments
 (0)