Skip to content

Commit 9d95fff

Browse files
Update
1 parent 22d32b5 commit 9d95fff

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ LeetCode 最强题解(持续更新中):
99
|[0018.四数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0018.四数之和) | 数组 |中等|**双指针**|
1010
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |简单|**模拟** |
1111
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |简单|**暴力** **快慢指针** |
12-
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针**|
12+
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针/双指针**|
1313
|[0028.实现strStr()](https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md) |字符串 |简单| **KMP** |
1414
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 |简单| **暴力** **二分**|
1515
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
1616
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**|
1717
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
18-
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**|
18+
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**快慢指针/双指针**|
1919
|[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md) |字符串 |中等|**模拟**|
2020
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**|
2121
|[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**|
@@ -34,7 +34,7 @@ LeetCode 最强题解(持续更新中):
3434
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
3535
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
3636
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
37-
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**模拟**|
37+
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**双指针**|
3838

3939
Leetcode精选:
4040

pics/剑指Offer05.替换空格.png

33.1 KB
Loading

problems/剑指Offer05.替换空格.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
## 题目地址
22
https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
33

4-
54
## 思路
65

76
如果想把这道题目做到极致,就不要只用额外的辅助空间,先扩充数组到每个空格替换成"%20"之后的大小
8-
然后从后向前替换空格
7+
然后从后向前替换空格,双指针法:
8+
9+
思路如下:
10+
<video src="../video/替换空格.mp4" controls="controls" width="640" height="320" autoplay="autoplay">
11+
Your browser does not support the video tag.
12+
</video>
13+
14+
时间复杂度,空间复杂度均超过100%的用户
915

16+
<img src='../pics/剑指Offer05.替换空格.png' width=600> </img></div>
1017

1118
## C++代码
1219

13-
时间复杂度,空间复杂度均超过100%的用户
1420

1521
```
1622
class Solution {
@@ -23,10 +29,11 @@ public:
2329
count++;
2430
}
2531
}
26-
s.resize(s.size() + count * 2); // 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
32+
// 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
33+
s.resize(s.size() + count * 2);
2734
int sNewSize = s.size();
2835
// 从后先前将空格替换为"%20"
29-
for (int i = sNewSize - 1, j = sOldSize - 1; i >= 0, j >= 0; i--, j--) {
36+
for (int i = sNewSize - 1, j = sOldSize - 1; j < i; i--, j--) {
3037
if (s[j] != ' ') {
3138
s[i] = s[j];
3239
} else {
@@ -39,5 +46,6 @@ public:
3946
return s;
4047
}
4148
};
49+
4250
```
4351
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master )https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。

video/替换空格.mp4

1000 KB
Binary file not shown.

0 commit comments

Comments
 (0)