diff --git a/README.md b/README.md index e5b5c53e..24009809 100644 --- a/README.md +++ b/README.md @@ -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 > 欢迎关注微信公众号:「代码随想录」,这里将持续分享自己对互联网以及技术的想法与思考 + diff --git a/problems/0018.四数之和.md b/problems/0018.四数之和.md index 08da733d..6acc07c3 100644 --- a/problems/0018.四数之和.md +++ b/problems/0018.四数之和.md @@ -53,3 +53,4 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0026.删除排序数组中的重复项.md b/problems/0026.删除排序数组中的重复项.md index 15b41d7d..80a271e1 100644 --- a/problems/0026.删除排序数组中的重复项.md +++ b/problems/0026.删除排序数组中的重复项.md @@ -24,3 +24,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index e94c3679..7a1ade91 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -47,3 +47,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0028.实现strStr().md b/problems/0028.实现strStr().md new file mode 100644 index 00000000..c0b76d1a --- /dev/null +++ b/problems/0028.实现strStr().md @@ -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=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,共同学习,一起进步。 diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 5cea4c32..b34bb28a 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -152,4 +152,4 @@ public: ## 总结 希望通过这道题目 ,可以帮助大家对二分法有更深的理解 -> 笔者在BAT从事技术研发多年,利用工作之余重刷leetcode,更多原创文章请关注公众号:「代码随想录」。 +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index d8279e23..b7a8ca5a 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -53,3 +53,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index c5c41d36..70525ab2 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -71,3 +71,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0083.删除排序链表中的重复元素.md b/problems/0083.删除排序链表中的重复元素.md index 39c50b94..4d160a21 100644 --- a/problems/0083.删除排序链表中的重复元素.md +++ b/problems/0083.删除排序链表中的重复元素.md @@ -36,3 +36,4 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index 0e7c7a29..cf10d747 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -104,3 +104,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md new file mode 100644 index 00000000..c5433886 --- /dev/null +++ b/problems/0151.翻转字符串里的单词.md @@ -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,共同学习,一起进步。 diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 60c534c4..57f71a4f 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -39,3 +39,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index 3aef4d2e..521199eb 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -128,3 +128,5 @@ public: } }; ``` + +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md index ec5c4ac5..b2d4277f 100644 --- a/problems/0205.同构字符串.md +++ b/problems/0205.同构字符串.md @@ -30,3 +30,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 5746ae3b..52e04431 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -54,3 +54,5 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 + diff --git a/problems/0209.长度最小的子数组.md b/problems/0209.长度最小的子数组.md index 3fe73055..d3e4e05f 100644 --- a/problems/0209.长度最小的子数组.md +++ b/problems/0209.长度最小的子数组.md @@ -60,3 +60,4 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0220.存在重复元素III.md b/problems/0220.存在重复元素III.md index c6404765..09771d57 100644 --- a/problems/0220.存在重复元素III.md +++ b/problems/0220.存在重复元素III.md @@ -20,3 +20,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0237.删除链表中的节点.md b/problems/0237.删除链表中的节点.md index c908962d..333a463b 100644 --- a/problems/0237.删除链表中的节点.md +++ b/problems/0237.删除链表中的节点.md @@ -19,3 +19,4 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index d896abc1..342ad764 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -27,3 +27,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index fc0a779e..ad38a3eb 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -17,3 +17,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 06cabc67..e3e5c74b 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -23,3 +23,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0350.两个数组的交集II.md b/problems/0350.两个数组的交集II.md index f3cf2b19..2e54a56b 100644 --- a/problems/0350.两个数组的交集II.md +++ b/problems/0350.两个数组的交集II.md @@ -32,3 +32,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 1a736444..078aad2c 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -65,3 +65,4 @@ public: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0434.字符串中的单词数.md b/problems/0434.字符串中的单词数.md index bd4fd4f2..7ac4e120 100644 --- a/problems/0434.字符串中的单词数.md +++ b/problems/0434.字符串中的单词数.md @@ -26,3 +26,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md new file mode 100644 index 00000000..cebe8355 --- /dev/null +++ b/problems/0459.重复的子字符串.md @@ -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,共同学习,一起进步。 diff --git a/problems/0541.反转字符串II.md b/problems/0541.反转字符串II.md new file mode 100644 index 00000000..bf3f81a9 --- /dev/null +++ b/problems/0541.反转字符串II.md @@ -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,共同学习,一起进步。 diff --git a/problems/0575.分糖果.md b/problems/0575.分糖果.md index 463de557..f39d1439 100644 --- a/problems/0575.分糖果.md +++ b/problems/0575.分糖果.md @@ -34,3 +34,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0705.设计哈希集合.md b/problems/0705.设计哈希集合.md index cf196382..3e5cfb6e 100644 --- a/problems/0705.设计哈希集合.md +++ b/problems/0705.设计哈希集合.md @@ -31,3 +31,4 @@ public: } }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index 4e23bb18..6a1b937f 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -108,3 +108,4 @@ private: }; ``` +> 笔者在先后在腾讯和百度从事技术研发多年,利用工作之余重刷leetcode,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md new file mode 100644 index 00000000..80b5301e --- /dev/null +++ b/problems/剑指Offer05.替换空格.md @@ -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,共同学习,一起进步。