Skip to content

Commit e06bad1

Browse files
committed
fix: add lengthOfLongestSubstring
1 parent 0653bf0 commit e06bad1

File tree

1 file changed

+45
-1
lines changed
  • packages/core/src/algorithmic-complexity/time-complexity

1 file changed

+45
-1
lines changed

packages/core/src/algorithmic-complexity/time-complexity/contact.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ export function reversePairs(nums: number[]): number {
343343
export function verifyPostorder(postorder: number[]): boolean {
344344
return recur(postorder, 0, postorder.length - 1)
345345
};
346-
347346
function recur(postorder:number[],i:number,j:number):boolean{
348347
if(i >= j){ return true }
349348
let p:number = 0
@@ -360,3 +359,48 @@ function recur(postorder:number[],i:number,j:number):boolean{
360359

361360
}
362361

362+
/**
363+
* 剑指 Offer 48. 最长不含重复字符的子字符串
364+
*
365+
* 请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
366+
*
367+
* 示例 1:
368+
* 输入: "abcabcbb"
369+
* 输出: 3
370+
* 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
371+
*
372+
* 示例 2:
373+
* 输入: "bbbbb"
374+
* 输出: 1
375+
* 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
376+
*
377+
* 示例 3:
378+
* 输入: "pwwkew"
379+
* 输出: 3
380+
* 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
381+
* 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
382+
*
383+
* 提示:s.length <= 40000
384+
*
385+
* @param s 字符串
386+
* @returns 最长子字符串的长度
387+
*/
388+
export function lengthOfLongestSubstring(s: string): number {
389+
let i = 0
390+
let j = 0
391+
let max = 0
392+
while(i < s.length) {
393+
for(let k = j; k < i; k++) {
394+
if (s[k] !== s[i]) {
395+
continue
396+
}
397+
else {
398+
max = Math.max(max, i - j)
399+
j = k + 1
400+
}
401+
}
402+
i++
403+
}
404+
return Math.max(max, i - j)
405+
406+
};

0 commit comments

Comments
 (0)