File tree Expand file tree Collapse file tree 1 file changed +89
-0
lines changed Expand file tree Collapse file tree 1 file changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 時間複雜度:
2
+ // 空間複雜度:
3
+ /*
4
+ * @lc app=leetcode.cn id=3 lang=golang
5
+ *
6
+ * [3] 无重复字符的最长子串
7
+ *
8
+ * https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
9
+ *
10
+ * algorithms
11
+ * Medium (39.64%)
12
+ * Likes: 10085
13
+ * Dislikes: 0
14
+ * Total Accepted: 2.8M
15
+ * Total Submissions: 7.1M
16
+ * Testcase Example: '"abcabcbb"'
17
+ *
18
+ * 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
19
+ *
20
+ *
21
+ *
22
+ * 示例 1:
23
+ *
24
+ *
25
+ * 输入: s = "abcabcbb"
26
+ * 输出: 3
27
+ * 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
28
+ *
29
+ *
30
+ * 示例 2:
31
+ *
32
+ *
33
+ * 输入: s = "bbbbb"
34
+ * 输出: 1
35
+ * 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
36
+ *
37
+ *
38
+ * 示例 3:
39
+ *
40
+ *
41
+ * 输入: s = "pwwkew"
42
+ * 输出: 3
43
+ * 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
44
+ * 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
45
+ *
46
+ *
47
+ *
48
+ *
49
+ * 提示:
50
+ *
51
+ *
52
+ * 0 <= s.length <= 5 * 10^4
53
+ * s 由英文字母、数字、符号和空格组成
54
+ *
55
+ *
56
+ */
57
+
58
+ // @lc code=start
59
+ func lengthOfLongestSubstring (s string ) int {
60
+ slength := len (s )
61
+ if slength == 0 || slength == 1 {
62
+ return slength
63
+ }
64
+
65
+ // m := make(map[byte]bool, len(s))
66
+ // 使用slice優化
67
+ m := [256 ]bool {}
68
+ left , right := 0 , 0
69
+ maxLen := 0
70
+ for left < slength {
71
+ if ok := m [s [right ]]; ok {
72
+ m [s [left ]] = false
73
+ left += 1
74
+ } else {
75
+ m [s [right ]] = true
76
+ right += 1
77
+ }
78
+ if maxLen < (right - left ) {
79
+ maxLen = right - left
80
+ }
81
+ if (left + maxLen ) >= slength || right >= slength {
82
+ break
83
+ }
84
+ }
85
+ return maxLen
86
+ }
87
+
88
+ // @lc code=end
89
+
You can’t perform that action at this time.
0 commit comments