-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.js
More file actions
73 lines (57 loc) · 1.72 KB
/
3.js
File metadata and controls
73 lines (57 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 3_Longest Substring Without Repeating Characters.js
/**
* @param {string} s
* @return {number}
*/
// 使用 Sliding Window
const lengthOfLongestSubstring = function (s) {
let window = ""; // current substring
let count = 0; // longest length
for (let i = 0; i < s.length; i++) {
console.log(window);
if (window.includes(s[i])) {
// 如果原本就有了
// 比對當前長度
if (window.length > count) count = window.length;
// 從重複值往後一格重新開始
window = window.slice(window.indexOf(s[i]) + 1, window.length);
window += s[i];
} else {
// 如果原本沒有
window += s[i];
}
}
// 最後一次的比對
if (window.length > count) count = window.length;
return count;
};
lengthOfLongestSubstring("abcabcbb"); // 3
// lengthOfLongestSubstring("bbbbb"); // 1
// lengthOfLongestSubstring("pww kew"); // 3
// 原本做法 (執行超時)
// const isRepeated = (arr, item) => {
// if (arr.filter((c) => c === item).length > 1) return true;
// return false;
// };
// const lengthOfLongestSubstring = function (s) {
// if (!s) return 0;
// let longestSubstring = s[0];
// let result = 1;
// let firstIndex = 0;
// let lastIndex = 1;
// while (lastIndex < s.length) {
// const curSubArr = s.split("").slice(firstIndex, lastIndex + 1);
// if (isRepeated(curSubArr, s[lastIndex])) {
// // lastIndex 在 subString 中仍有重複
// firstIndex++;
// } else {
// // lastIndex 在 subString 沒有重複
// if (curSubArr.length > result) {
// result = curSubArr.length;
// longestSubstring = curSubArr.join("");
// }
// lastIndex++;
// }
// }
// console.log(result);
// };