文章目录
前言
打卡第八天,字符串模拟就行了
字符串
LeetCode 344. 反转字符串
LeetCode 344. 反转字符串
文章讲解
视频讲解
状态 : AC
- AC代码示例:
简单双指针…
class Solution {
public:
void reverseString(vector<char>& s) {
for(int i=0;i<s.size()/2;i++){
int j=s.size()-1-i;
swap(s[i],s[j]);
}
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(1)
当然之间用api也是可以的^^
class Solution {
public:
void reverseString(vector<char>& s) {
reverse(s.begin(),s.end());
}
};
LeetCode 541. 反转字符串 II
LeetCode 541. 反转字符串 II
文章讲解
视频讲解
状态 : AC
- AC代码示例:
没什么好说的,根据题意模拟就行了
class Solution {
public:
string reverseStr(string s, int k) {
int cnt=0;
for(int i=0;i<s.size();i+=(k*2)){
int j=i+(k*2);
if(j>s.size()-1) j=s.size()-1;
if(j-i+1>=k){
int l=i,r=i+k-1;
while(l<r){
swap(s[l],s[r]);
l++;
r--;
}
}else{
int l=i,r=j;
while(l<r){
swap(s[l],s[r]);
l++;
r--;
}
}
}
return s;
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(1)
卡码网 54. 替换数字
卡码网 54. 替换数字
文章讲解
[视频讲解]:无
状态 : AC
模拟
- AC代码示例:
#include <bits/stdc++.h>
using namespace std;
int main(){
string s,ans;
cin>>s;
for(auto i:s){
if(i>='a' && i<='z'){
ans+=i;
}else ans+="number";
}
cout <<ans;
return 0;
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
LeetCode 151. 反转字符串中的单词
LeetCode 151. 反转字符串中的单词
文章讲解
视频讲解
状态 : AC
自己模拟出来了…题解还没看
…
- AC代码示例:
class Solution {
public:
string reverseWords(string s) {
string ans;
stack<int> wordindex;
for (int i = 0; i < s.size(); i++) {
if (s[i] != ' ') {
wordindex.push(i);
}
while (s[i] != ' ' && i < s.size()) {
i++;
}
}
while (wordindex.size()) {
auto i = wordindex.top();
wordindex.pop();
while (i < s.size() && s[i] != ' ') {
ans += s[i];
i++;
}
ans += ' ';
}
ans = ans.substr(0, ans.length() - 1);
return ans;
}
};
- 时间复杂度:O(n)
- 空间复杂度:O(n)
卡码网 55. 右旋字符串
卡码网 55. 右旋字符串
文章讲解
[视频讲解]:无
状态 : AC
模拟
- AC代码示例:
#include <bits/stdc++.h>
using namespace std;
int main() {
string n, ans;
int k;
cin >> k >> n;
if (k > n.size()) {
cout << n << endl;
return 0;
}
for (int i = n.size() - k; i < n.size(); i++) {
ans += n[i];
}
n = n.substr(0, n.length() - k);
ans += n;
cout << ans << endl;
system("pause");
}
- 时间复杂度:O(n)
- 空间复杂度:O(1)
总结
事儿有点多,周末补上
2024- 4-25