mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加(0459.重复的子字符串.md):增加typescript版本
This commit is contained in:
@ -361,7 +361,65 @@ var repeatedSubstringPattern = function (s) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
> 前缀表统一减一
|
||||
|
||||
```typescript
|
||||
function repeatedSubstringPattern(s: string): boolean {
|
||||
function getNext(str: string): number[] {
|
||||
let next: number[] = [];
|
||||
let j: number = -1;
|
||||
next[0] = j;
|
||||
for (let i = 1, length = str.length; i < length; i++) {
|
||||
while (j >= 0 && str[i] !== str[j + 1]) {
|
||||
j = next[j];
|
||||
}
|
||||
if (str[i] === str[j + 1]) {
|
||||
j++;
|
||||
}
|
||||
next[i] = j;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
let next: number[] = getNext(s);
|
||||
let sLength: number = s.length;
|
||||
let nextLength: number = next.length;
|
||||
let suffixLength: number = next[nextLength - 1] + 1;
|
||||
if (suffixLength > 0 && sLength % (sLength - suffixLength) === 0) return true;
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
||||
> 前缀表不减一
|
||||
|
||||
```typescript
|
||||
function repeatedSubstringPattern(s: string): boolean {
|
||||
function getNext(str: string): number[] {
|
||||
let next: number[] = [];
|
||||
let j: number = 0;
|
||||
next[0] = j;
|
||||
for (let i = 1, length = str.length; i < length; i++) {
|
||||
while (j > 0 && str[i] !== str[j]) {
|
||||
j = next[j - 1];
|
||||
}
|
||||
if (str[i] === str[j]) {
|
||||
j++;
|
||||
}
|
||||
next[i] = j;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
let next: number[] = getNext(s);
|
||||
let sLength: number = s.length;
|
||||
let nextLength: number = next.length;
|
||||
let suffixLength: number = next[nextLength - 1];
|
||||
if (suffixLength > 0 && sLength % (sLength - suffixLength) === 0) return true;
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user