代码随想录算法训练营第七天| 344.反转字符串 、 541. 反转字符串II、 剑指Offer 05.替换空格 、 151.翻转字符串里的单词 、 剑指Offer58-II.左旋转字符串
344.反转字符串
代码:
1 void reverseString(vector<char>& s) { 2 3 int i = 0; 4 int j = s.size() - 1; 5 while (i < j) 6 { 7 char mid = s[i]; 8 s[i] = s[j]; 9 s[j] = mid; 10 11 i++; 12 j--; 13 } 14 }
541. 反转字符串II
思路:
1,先步数为k,那么他就是进行几轮
2,如果轮数为偶数,就计算,如果为奇数,就不计算
代码:
1 string reverseStr(string s, int k) { 2 3 for (int n = 0; n < s.size(); n+=k) 4 { 5 if ((n / k) % 2 != 0) 6 { 7 continue; 8 } 9 10 11 int _i = n; 12 int _j = k -1 + n; 13 _j = _j > s.size() - 1 ? s.size() - 1 : _j; 14 15 cout << s[_i] << "\t" << s[_j] << endl; 16 while (_i < _j) 17 { 18 swap(s[_i ], s[_j ]); 19 20 _i++; 21 _j--; 22 } 23 } 24 25 return s; 26 }
学习:
也可以用 n+=2*k,来代替:
1 for (int n = 0; n < s.size(); n+=k) 2 { 3 if ((n / k) % 2 != 0) 4 { 5 continue; 6 }}
剑指Offer 05.替换空格
1,通过打散的方式,然后拼接
代码:
1 string replaceSpace(string s) { 2 vector<string> combination; 3 4 string cur; 5 for (int i=0;i<s.size();i++) 6 { 7 char item = s[i]; 8 if (item != ' ') 9 { 10 cur +=item; 11 } 12 else 13 { 14 combination.push_back(cur); 15 cur = ""; 16 combination.push_back("%20"); 17 } 18 if (i == s.size()-1) 19 { 20 combination.push_back(cur); 21 } 22 } 23 24 cur = ""; 25 for (string item : combination) 26 { 27 cur += item; 28 } 29 30 return cur; 31 }
2,用指针
思路:
1,先扩容-》resize
2,从后往前,两个指针输入值
代码:
1 string replaceSpace(string s) { 2 int oldsize = s.size(); 3 int newSize = s.size(); 4 5 // 需要想怎么扩容 6 for (char c : s) 7 { 8 if (c == ' ') 9 newSize += 2; 10 } 11 12 s.resize(newSize); 13 14 //思考两个 i j 应该怎么写 15 int j = newSize - 1; 16 for (int i = oldsize - 1; i >= 0; i--) 17 { 18 if (s[i] != ' ') 19 { 20 s[j] = s[i]; 21 22 } 23 else 24 { 25 s[j] = '0'; 26 s[j-1] = '2'; 27 s[j - 2] = '%'; 28 29 j = j - 2; 30 } 31 j--; 32 } 33 34 return s; 35 }
151.翻转字符串里的单词
1,简单版: 自己做split,然后进行反转
代码:
1 string reverseWords(string s) { 2 vector<string> words; 3 4 string cur = ""; 5 for (int i = 0; i < s.size(); i++) 6 { 7 if (s[i] != ' ') 8 { 9 cur += s[i]; 10 } 11 else if(cur.size()>0) 12 { 13 words.push_back(cur); 14 cur = ""; 15 } 16 if (i == s.size() - 1 && cur.size() > 0) 17 { 18 words.push_back(cur); 19 } 20 } 21 cur = ""; 22 for (int i = words.size() - 1; i >= 0; i--) 23 { 24 cur += words[i]; 25 if (i!= 0) 26 { 27 cur += " "; 28 } 29 } 30 31 return cur; 32 }
2,稍微复杂版
思路:
1,删掉空格(需要重点思考)
2,反转字符串
3,反转单词
代码:
1 string reverseWords(string s) { 2 3 for (int i=0;i<s.size()-1;i++) 4 { 5 if (s[i] == ' ' && s[i + 1] == ' ') 6 { 7 s.erase(s.begin() + i + 1); 8 i--; 9 } 10 } 11 if(s[0]==' ') s.erase(s.begin()); 12 if (s[s.size()-1] == ' ') s.erase(s.begin()+ s.size() - 1); 13 14 int i = 0; 15 int j = s.size()-1; 16 while (j > i) 17 { 18 swap(s[i], s[j]); 19 i++; 20 j--; 21 } 22 23 //用双指针,去反转单词 24 //快指针->遇到空格了之后,先记录 空格的位置,然后再反转,随后继续 25 for (int fast = 0, slow = 0; fast < s.size(); fast++) 26 { 27 if (s[fast] == ' ' || fast==s.size()-1) 28 { 29 int left = slow; 30 int right = fast == s.size() - 1? fast: fast - 1; 31 while (right > left) 32 { 33 swap(s[left], s[right]); 34 left++; 35 right--; 36 } 37 slow = fast + 1; 38 } 39 } 40 41 return s; 42 }
剑指Offer58-II.左旋转字符串
1,简单版:
截取前n个,然后对他们消除,在添加到后面
代码:
1 string reverseLeftWords(string s, int n) { 2 string topStr; 3 for (int i = 0; i < n; i++) 4 { 5 topStr += s[i]; 6 } 7 s.erase(s.begin(),s.begin() + n); 8 s = s + topStr; 9 return s; 10 }