mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -929,6 +929,83 @@ var strStr = function (haystack, needle) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TypeScript版本:
|
||||||
|
|
||||||
|
> 前缀表统一减一
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function strStr(haystack: string, needle: string): number {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
if (needle.length === 0) return 0;
|
||||||
|
let next: number[] = getNext(needle);
|
||||||
|
let j: number = -1;
|
||||||
|
for (let i = 0, length = haystack.length; i < length; i++) {
|
||||||
|
while (j >= 0 && haystack[i] !== needle[j + 1]) {
|
||||||
|
j = next[j];
|
||||||
|
}
|
||||||
|
if (haystack[i] === needle[j + 1]) {
|
||||||
|
if (j === needle.length - 2) {
|
||||||
|
return i - j - 1;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
> 前缀表不减一
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// 不减一版本
|
||||||
|
function strStr(haystack: string, needle: string): number {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
if (needle.length === 0) return 0;
|
||||||
|
let next: number[] = getNext(needle);
|
||||||
|
let j: number = 0;
|
||||||
|
for (let i = 0, length = haystack.length; i < length; i++) {
|
||||||
|
while (j > 0 && haystack[i] !== needle[j]) {
|
||||||
|
j = next[j - 1];
|
||||||
|
}
|
||||||
|
if (haystack[i] === needle[j]) {
|
||||||
|
if (j === needle.length - 1) {
|
||||||
|
return i - j;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Swift 版本
|
Swift 版本
|
||||||
|
|
||||||
> 前缀表统一减一
|
> 前缀表统一减一
|
||||||
|
@ -195,10 +195,20 @@ class ListNode {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class ListNode:
|
||||||
|
def __init__(self, val, next=None):
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```go
|
||||||
|
type ListNode struct {
|
||||||
|
Val int
|
||||||
|
Next *ListNode
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user