LeetCode每日一练【3】
作者:互联网
LeetCode每日一练
Longest Substring Without Repeating Characters
function lengthOfLongestSubstring(s) {
const scanner = []
let longest = 0
for (const element of s) {
// 查找当前元素是否在数组中
const possibleIndex = scanner.indexOf(element)
// 如果当前元素已经在数组中,数组将删除从索引0到查找到的索引间的所有元素,包括当前元素
if (possibleIndex !== -1) { scanner.splice(0, possibleIndex + 1) }
// 将当前元素添加到数组中
scanner.push(element)
// longest = Math.max(longest, scanner.length)
// 比较最大值
longest = longest > scanner.length ? longest : scanner.length
}
return longest
}
标签:const,scanner,每日,LeetCode,length,longest,element,possibleIndex 来源: https://www.cnblogs.com/mapodoufu/p/16168578.html