Skip to content

Commit e9f0df2

Browse files
committed
feat: leetcode 面试题 01.09
1 parent 6571219 commit e9f0df2

File tree

8 files changed

+145
-2
lines changed

8 files changed

+145
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:507-green" alt="进度:507">
5+
<img src="https://img.shields.io/badge/-进度:508-green" alt="进度:508">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,12 @@
25662566
"title": "滑动窗口的平均值",
25672567
"path": "./problemset/average-value-of-sliding-window/README.md",
25682568
"difficulty": "简单"
2569+
},
2570+
{
2571+
"id": "面试题 01.09",
2572+
"title": "字符串轮转",
2573+
"path": "./problemset/string-rotation-lcci/README.md",
2574+
"difficulty": "简单"
25692575
}
25702576
]
25712577
},

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -5068,5 +5068,15 @@
50685068
"difficulty": "中等",
50695069
"url": "https://leetcode.cn/problems/get-kth-magic-number-lcci/",
50705070
"path": "./problemset/get-kth-magic-number-lcci/README.md"
5071+
},
5072+
{
5073+
"id": "面试题 01.09",
5074+
"title": {
5075+
"cn": "字符串轮转",
5076+
"en": "string-rotation-lcci"
5077+
},
5078+
"difficulty": "简单",
5079+
"url": "https://leetcode.cn/problems/string-rotation-lcci/",
5080+
"path": "./problemset/string-rotation-lcci/README.md"
50715081
}
50725082
]

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@
469469
| 面试题 17.11. [单词距离](../../problemset/find-closest-lcci/README.md) | 中等 |
470470
| 剑指 Offer II 029. [排序的循环链表](../../problemset/sorted-circular-linked-list/README.md) | 中等 |
471471
| 剑指 Offer II 041. [滑动窗口的平均值](../../problemset/average-value-of-sliding-window/README.md) | 简单 |
472+
| 面试题 01.09. [字符串轮转](../../problemset/string-rotation-lcci/README.md) | 简单 |
472473

473474
## 回溯
474475

assets/docs/TOPICS.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1012,4 +1012,6 @@
10121012

10131013
[面试题 01.02. 判定是否互为字符重排](../../problemset/check-permutation-lcci/README.md)
10141014

1015-
[面试题 17.09. 第 k 个数](../../problemset/get-kth-magic-number-lcci/README.md)
1015+
[面试题 17.09. 第 k 个数](../../problemset/get-kth-magic-number-lcci/README.md)
1016+
1017+
[面试题 01.09. 字符串轮转](../../problemset/string-rotation-lcci/README.md)
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 字符串轮转
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/string-rotation-lcci/
6+
7+
## 题目
8+
9+
字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。
10+
11+
### 示例
12+
13+
#### 示例1:
14+
15+
```
16+
输入:s1 = "waterbottle", s2 = "erbottlewat"
17+
输出:True
18+
```
19+
20+
#### 示例2:
21+
22+
```
23+
输入:s1 = "aa", s2 = "aba"
24+
输出:False
25+
```
26+
27+
## 解题
28+
29+
### 模拟
30+
31+
```ts
32+
/**
33+
* 模拟
34+
* @desc 时间复杂度 O(N²) 空间复杂度 O(1)
35+
* @param s1
36+
* @param s2
37+
* @returns
38+
*/
39+
export function isFlipedString(s1: string, s2: string): boolean {
40+
const m = s1.length
41+
const n = s2.length
42+
if (m !== n) return false
43+
if (n === 0) return true
44+
45+
for (let i = 0; i < n; i++) {
46+
let flag = true
47+
for (let j = 0; j < n; j++) {
48+
if (s1[(i + j) % n] !== s2[j]) {
49+
flag = false
50+
break
51+
}
52+
}
53+
if (flag) return true
54+
}
55+
return false
56+
}
57+
```
58+
59+
### 搜索子字符串
60+
61+
```ts
62+
/**
63+
* 搜索子字符串
64+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
65+
* @param s1
66+
* @param s2
67+
* @returns
68+
*/
69+
export function isFlipedString2(s1: string, s2: string): boolean {
70+
return s1.length === s2.length && (s1 + s1).includes(s2)
71+
}
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { isFlipedString, isFlipedString2 } from '.'
3+
4+
describe('字符串轮转', () => {
5+
describe('模拟', () => testCase(isFlipedString))
6+
describe('搜索子字符串', () => testCase(isFlipedString2))
7+
})
8+
9+
function testCase(fn: (s1: string, s2: string) => boolean) {
10+
it.each([
11+
['waterbottle', 'erbottlewat', true],
12+
['aa', 'aba', false],
13+
])('示例%#', (s1, s2, expected) => {
14+
expect(fn(s1, s2)).toBe(expected)
15+
})
16+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 模拟
3+
* @desc 时间复杂度 O(N²) 空间复杂度 O(1)
4+
* @param s1
5+
* @param s2
6+
* @returns
7+
*/
8+
export function isFlipedString(s1: string, s2: string): boolean {
9+
const m = s1.length
10+
const n = s2.length
11+
if (m !== n) return false
12+
if (n === 0) return true
13+
14+
for (let i = 0; i < n; i++) {
15+
let flag = true
16+
for (let j = 0; j < n; j++) {
17+
if (s1[(i + j) % n] !== s2[j]) {
18+
flag = false
19+
break
20+
}
21+
}
22+
if (flag) return true
23+
}
24+
return false
25+
}
26+
27+
/**
28+
* 搜索子字符串
29+
* @desc 时间复杂度 O(N) 空间复杂度 O(N)
30+
* @param s1
31+
* @param s2
32+
* @returns
33+
*/
34+
export function isFlipedString2(s1: string, s2: string): boolean {
35+
return s1.length === s2.length && (s1 + s1).includes(s2)
36+
}

0 commit comments

Comments
 (0)