Skip to content

Commit ca5e65e

Browse files
committed
三刷3
1 parent dd7c814 commit ca5e65e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,35 @@ image::images/0003-09.png[{image_attr}]
6262
image::images/0003-10.png[{image_attr}]
6363

6464
[[src-0003]]
65+
[tabs]
66+
====
67+
一刷::
68+
+
69+
--
6570
[{java_src_attr}]
6671
----
6772
include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters.java[tag=answer]
6873
----
74+
--
6975
76+
二刷::
77+
+
78+
--
7079
[{java_src_attr}]
7180
----
7281
include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters_2.java[tag=answer]
7382
----
83+
--
84+
85+
三刷::
86+
+
87+
--
88+
[{java_src_attr}]
89+
----
90+
include::{sourcedir}/_0003_LongestSubstringWithoutRepeatingCharacters_3.java[tag=answer]
91+
----
92+
--
93+
====
7494

7595
== 参考资料
7696

logbook/202406.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@
833833
|{doc_base_url}/0400-nth-digit.adoc[题解]
834834
|❌ 纯纯的数学运算,一脸懵逼
835835

836+
|{counter:codes}
837+
|{leetcode_base_url}/longest-substring-without-repeating-characters/[3. Longest Substring Without Repeating Characters^]
838+
|{doc_base_url}/0003-longest-substring-without-repeating-characters.adoc[题解]
839+
|✅ 滑动窗口。滑动窗口的模板不是很熟悉。可以不存字符格式,存字符最大的坐标。
836840

837841
|===
838842

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _0003_LongestSubstringWithoutRepeatingCharacters_3 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2024-09-21 20:41:51
12+
*/
13+
public int lengthOfLongestSubstring(String s) {
14+
Map<Character, Integer> map = new HashMap<>();
15+
int result = 0, left = 0;
16+
for (int right = 0; right < s.length(); right++) {
17+
char c = s.charAt(right);
18+
int cnt = map.getOrDefault(c, 0);
19+
if (cnt == 0) {
20+
map.put(c, 1);
21+
result = Math.max(result, right - left + 1);
22+
} else {
23+
map.put(c, cnt + 1);
24+
}
25+
while (map.get(c) > 1) {
26+
char lc = s.charAt(left);
27+
left++;
28+
map.put(lc, map.get(lc) - 1);
29+
}
30+
}
31+
return result;
32+
}
33+
// end::answer[]
34+
}

0 commit comments

Comments
 (0)