mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -10,11 +10,13 @@ LeetCode 最强题解(持续更新中):
|
||||
|[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |简单|**模拟** |
|
||||
|[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |简单|**暴力** **快慢指针** |
|
||||
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **快慢指针**|
|
||||
|[0028.实现strStr()](https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md) |字符串 |简单| **KMP** |
|
||||
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 |简单| **暴力** **二分**|
|
||||
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
|
||||
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**|
|
||||
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
|
||||
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**|
|
||||
|[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md) |字符串 |中等|**模拟**|
|
||||
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**|
|
||||
|[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**|
|
||||
|[0205.同构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0205.同构字符串.md) |哈希表 |简单| **哈希**|
|
||||
@ -30,8 +32,9 @@ LeetCode 最强题解(持续更新中):
|
||||
|[0434.字符串中的单词数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0434.字符串中的单词数.md) |字符串 |简单|**模拟**|
|
||||
|[0454.四数相加II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.md) |哈希表 |中等| **哈希**|
|
||||
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
|
||||
|[0705.设计哈希集合.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
|
||||
|[0705.设计哈希集合](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|
|
||||
|[0707.设计链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0707.设计链表.md) |链表 |中等|**模拟**|
|
||||
|[剑指Offer05.替换空格](https://github.com/youngyangyang04/leetcode/blob/master/problems/剑指Offer05.替换空格.md) |字符串 |简单|**模拟**|
|
||||
|
||||
Leetcode精选:
|
||||
|
||||
@ -44,3 +47,4 @@ Leetcode精选:
|
||||
> 笔者介绍:
|
||||
> ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解,利用工作之余重新刷leetcode
|
||||
> 欢迎关注微信公众号:「代码随想录」,这里将持续分享自己对互联网以及技术的想法与思考
|
||||
|
||||
|
@ -53,3 +53,4 @@ public:
|
||||
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -24,3 +24,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -47,3 +47,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
49
problems/0028.实现strStr().md
Normal file
49
problems/0028.实现strStr().md
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/implement-strstr/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void preKmp(int* next, const string& s){
|
||||
next[0]=-1;
|
||||
int j=-1;
|
||||
for(int i=1;i<s.size();i++){
|
||||
while(j>=0 && s[i]!=s[j+1])
|
||||
j = next[j];
|
||||
if(s[i]==s[j+1])
|
||||
j++;
|
||||
next[i]=j;
|
||||
}
|
||||
}
|
||||
int strStr(string haystack, string needle) {
|
||||
if (needle.size() == 0) {
|
||||
return 0;
|
||||
}
|
||||
int next[needle.size()];
|
||||
preKmp(next, needle);
|
||||
|
||||
int j = -1;
|
||||
for (int i = 0; i < haystack.size(); i++) {
|
||||
while(j >= 0 && haystack[i] != needle[j + 1]) {
|
||||
j = next[j];
|
||||
}
|
||||
if (haystack[i] == needle[j + 1]) {
|
||||
j++;
|
||||
}
|
||||
if (j == (needle.size() -1) ) {
|
||||
return (i - needle.size() + 1);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
@ -152,4 +152,4 @@ public:
|
||||
## 总结
|
||||
希望通过这道题目 ,可以帮助大家对二分法有更深的理解
|
||||
|
||||
> 笔者在BAT从事技术研发多年,利用工作之余重刷leetcode,更多原创文章请关注公众号:「代码随想录」。
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -53,3 +53,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -71,3 +71,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -36,3 +36,4 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -104,3 +104,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
87
problems/0151.翻转字符串里的单词.md
Normal file
87
problems/0151.翻转字符串里的单词.md
Normal file
@ -0,0 +1,87 @@
|
||||
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/reverse-words-in-a-string/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## 代码
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
// 反转字符串s中左闭又闭的区间[start, end]
|
||||
void reverse(string& s, int start, int end) {
|
||||
int offset = (end - start + 1) / 2;
|
||||
for (int i = start, j = end; i < start + offset; i++, j--) {
|
||||
swap(s[i], s[j]);
|
||||
}
|
||||
}
|
||||
// 字符串去掉冗余的空格
|
||||
// 使用快慢指针发
|
||||
void removeExtraSpaces(string& s) {
|
||||
int slowIndex = 0, fastIndex = 0; // 定义快指针,慢指针
|
||||
// 去掉字符串前面的空格
|
||||
while (s.size() > 0 && fastIndex < s.size() && s[fastIndex] == ' ') {
|
||||
fastIndex++;
|
||||
}
|
||||
for (; fastIndex < s.size(); fastIndex++) {
|
||||
// 去掉字符串中间部分的冗余空格
|
||||
if (fastIndex - 1 > 0
|
||||
&& s[fastIndex - 1] == s[fastIndex]
|
||||
&& s[fastIndex] == ' ') {
|
||||
continue;
|
||||
} else {
|
||||
s[slowIndex++] = s[fastIndex];
|
||||
}
|
||||
}
|
||||
if (slowIndex - 1 > 0 && s[slowIndex - 1] == ' ') { // 去掉字符串末尾的空格
|
||||
s.resize(slowIndex - 1);
|
||||
} else {
|
||||
s.resize(slowIndex); // 重新设置字符串大小
|
||||
}
|
||||
}
|
||||
|
||||
// 费时间的写法
|
||||
void removeExtraSpaces1(string& s) {
|
||||
for (int i = s.size() - 1; i > 0; i--) {
|
||||
if (s[i] == s[i - 1] && s[i] == ' ') {
|
||||
s.erase(s.begin() + i);
|
||||
}
|
||||
}
|
||||
if (s.size() > 0 && s[s.size() - 1] == ' ') {
|
||||
s.erase(s.begin() + s.size() - 1);
|
||||
}
|
||||
if (s.size() > 0 && s[0] == ' ') {
|
||||
s.erase(s.begin());
|
||||
}
|
||||
}
|
||||
|
||||
string reverseWords(string s) {
|
||||
reverse(s, 0, s.size() - 1); // 将字符串全部反转
|
||||
removeExtraSpaces(s); // 去掉冗余空格
|
||||
int start = 0;
|
||||
int end = 0;
|
||||
bool entry = false;
|
||||
for (int i = 0; i < s.size(); i++) { // 开始反转单词
|
||||
if ((!entry) || (s[i] != ' ' && s[i - 1] == ' ')) {
|
||||
start = i; // 确定单词起始位置
|
||||
entry = true;
|
||||
}
|
||||
// 单词后面有空格的情况,空格就是分词符
|
||||
if (entry && s[i] == ' ' && s[i - 1] != ' ') {
|
||||
end = i - 1; // 确定单词终止位置
|
||||
entry = false;
|
||||
reverse(s, start, end);
|
||||
}
|
||||
// 最后一个结尾单词之后没有空格的情况
|
||||
if (entry && (i == (s.size() - 1)) && s[i] != ' ' ) {
|
||||
end = i;// 确定单词终止位置
|
||||
entry = false;
|
||||
reverse(s, start, end);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
@ -39,3 +39,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -128,3 +128,5 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -30,3 +30,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -54,3 +54,5 @@ public:
|
||||
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
||||
|
@ -60,3 +60,4 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -20,3 +20,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -19,3 +19,4 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -27,3 +27,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -17,3 +17,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -23,3 +23,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -32,3 +32,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -65,3 +65,4 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -26,3 +26,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
40
problems/0459.重复的子字符串.md
Normal file
40
problems/0459.重复的子字符串.md
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/repeated-substring-pattern/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void preKmp(int* next, const string& s){
|
||||
next[0] = -1;
|
||||
int j = -1;
|
||||
for(int i = 1;i < s.size(); i++){
|
||||
while(j >= 0 && s[i] !=s [j+1])
|
||||
j = next[j];
|
||||
if(s[i] == s[j+1])
|
||||
j++;
|
||||
next[i] = j;
|
||||
}
|
||||
}
|
||||
bool repeatedSubstringPattern(string s) {
|
||||
if (s.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
int next[s.size()];
|
||||
preKmp(next, s);
|
||||
int len = s.size();
|
||||
if (next[len - 1] != -1 && len % (len - (next[len - 1] + 1)) == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
37
problems/0541.反转字符串II.md
Normal file
37
problems/0541.反转字符串II.md
Normal file
@ -0,0 +1,37 @@
|
||||
|
||||
## 题目地址
|
||||
|
||||
https://leetcode-cn.com/problems/reverse-string-ii/
|
||||
|
||||
## 思路
|
||||
|
||||
|
||||
## C++代码
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
void reverse(string& s, int start, int end) {
|
||||
int offset = (end - start + 1) / 2;
|
||||
for (int i = start, j = end; i < start + offset; i++, j--) {
|
||||
swap(s[i], s[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// C++ 自带了reverse 方法,但是我依然建议自己实现
|
||||
string reverseStr(string s, int k) {
|
||||
bool entry = false;
|
||||
int i;
|
||||
for (i = 0; i < s.size(); i += (2 * k)) {
|
||||
if (i + k <= s.size()) {
|
||||
// std::reverse(s.begin() + i, s.begin() + i + k );
|
||||
reverse(s, i, i + k - 1);
|
||||
continue;
|
||||
}
|
||||
// std::reverse(s.begin() + i, s.begin() + s.size());
|
||||
reverse(s, i, s.size() - 1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
@ -34,3 +34,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -31,3 +31,4 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
@ -108,3 +108,4 @@ private:
|
||||
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
||||
|
43
problems/剑指Offer05.替换空格.md
Normal file
43
problems/剑指Offer05.替换空格.md
Normal file
@ -0,0 +1,43 @@
|
||||
## 题目地址
|
||||
https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
如果想把这道题目做到极致,就不要只用额外的辅助空间,先扩充数组到每个空格替换成"%20"之后的大小
|
||||
然后从后向前替换空格
|
||||
|
||||
|
||||
## C++代码
|
||||
|
||||
时间复杂度,空间复杂度均超过100%的用户
|
||||
|
||||
```
|
||||
class Solution {
|
||||
public:
|
||||
string replaceSpace(string s) {
|
||||
int count = 0; // 统计空格的个数
|
||||
int sOldSize = s.size();
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (s[i] == ' ') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
s.resize(s.size() + count * 2); // 扩充字符串s的大小,也就是每个空格替换成"%20"之后的大小
|
||||
int sNewSize = s.size();
|
||||
// 从后先前将空格替换为"%20"
|
||||
for (int i = sNewSize - 1, j = sOldSize - 1; i >= 0, j >= 0; i--, j--) {
|
||||
if (s[j] != ' ') {
|
||||
s[i] = s[j];
|
||||
} else {
|
||||
s[i] = '0';
|
||||
s[i - 1] = '2';
|
||||
s[i - 2] = '%';
|
||||
i -= 2;
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
```
|
||||
> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。
|
Reference in New Issue
Block a user