代码随想录算法训练营第八天|LeetCode 344. 反转字符串 541. 反转字符串 II 151. 反转字符串中的单词 卡码网 54. 替换数字 55. 右旋字符串


前言

打卡第八天,字符串模拟就行了


字符串

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值