*算法训练(leetcode)第八天 | 151. 反转字符串中的单词、右旋字符串、28. 找出字符串中第一个匹配项的下标、459. 重复的子字符串

151. 反转字符串中的单词

leetcode题目地址

反转单词由一个巧妙的思路就是先把字符串整体反转,再把每个单词里的字符反转。接下来就是处理细节:删除多余空格。使用双指针,左、右侧指针初始化为0,右侧指针遍历字符串中的字符,左侧指针记录删除空格后当前元素应该存储的位置。删除空格后修改字符串长度。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

// c++
class Solution {
public:
    void removeSpace(string &s){
        int i, idx=0;
        for(i=0; i<s.size(); i++){
        	// 第一个位置不存空格
            if(idx==0 && s[i]==' ') continue;
            // 重复出现的空格只存储一次
            if(i>0 && s[i]==' ' && s[idx-1]==' '){
                continue;
            }
            s[idx++] = s[i];
        }
        // 删除末尾多余空格
        if(idx>0 && s[idx-1]==' ') idx--;
        s.resize(idx);
    }
    
    string reverseWords(string s) {
        int left = 0, right=0;
        // 移除多余空格
        removeSpace(s);
        // 整串反转
        reverse(s.begin(), s.end());
        for(right=0; right<s.size(); right++){
        	// 最后一个单词后边没有空格,所以需要单独处理
            if(s[right]==' '){
            	// 单词内的字符反转
                reverse(s.begin()+left, s.begin()+right);
                left = right+1;
            }
        }
		// 反转最后一个单词
        reverse(s.begin()+left, s.begin()+right);
        return s;
        
    }
};

右旋字符串

卡码网题目地址

a b c d e f g abcdefg abcdefg 右旋 2 2 2 个字符变为 f g a b c d e fgabcde fgabcde ,不难发现这个字符串其实是进行了三次反转:

  • 首先,整串反转,得到: g f e d c b a gfedcba gfedcba
  • 其次,反转前 k k k个字符(此例k=2),得到: f g e d c b a fgedcba fgedcba
  • 最后,反转剩余字符,得到: f g a b c d e fgabcde fgabcde

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

// c++
#include<bits/stdc++.h>
using namespace std;

int main(){
    int k;
    string s;
    cin>>k>>s;
    // cout<<k<<s;
    reverse(s.begin(), s.end());
    reverse(s.begin(), s.begin()+k);
    reverse(s.begin()+k, s.end());
    cout<<s;
    return 0;
}

28. 找出字符串中第一个匹配项的下标

leetcode题目地址

思路一:暴力求解

遍历 haystack ,当找到 needle 的第一个元素相同的元素时,向后查看 needle.size() 位判断是否与needle完全匹配。

时间复杂度: O ( n ∗ m ) O(n * m) O(nm)
空间复杂度: O ( 1 ) O(1) O(1)

// c++
class Solution {
public:
    int strStr(string haystack, string needle) {
        for(int i=0; i<haystack.size(); i++){
            if(haystack[i] == needle[0]){
                int flag = 1;
                for(int j=0; j<needle.size(); j++){
                    if(haystack[i+j]!=needle[j]){
                        flag=0;
                        break;
                    }
                }
                if(flag) return i;
            }
        }
        return -1;
    }
};

*思路二:KMP算法

使用KMP算法做模式匹配。

时间复杂度: O ( n + m ) O(n + m) O(n+m)
空间复杂度: O ( m ) O(m) O(m)

// c++
class Solution {
public:
    void getNext(int* next, string s){
        int j=0, i;
        next[0] = 0;
        for(i=1; i<s.size(); i++){
            while(j>0 && s[i]!=s[j]) j = next[j-1];
            if(s[i]==s[j]) j++;
            next[i] = j;
        }
    }
    int strStr(string haystack, string needle) {
        int* next = new int[needle.size()];
        getNext(next, needle);
        int j = 0;
        for(int i=0; i<haystack.size(); i++){
            while(j>0 && haystack[i]!=needle[j]){
                j = next[j-1];
            }
            if(haystack[i] == needle[j]) j++;

            if(j==needle.size()){
                return i-needle.size()+1;
            }

        }

        return -1;
    }
};

459. 重复的子字符串

leetcode题目地址

思路一:重复子串的性质

思路来源。 若字符串s可以由子串重复组成,那么s+s的中间位置一定会出现s。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

// c++
class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        string t = s+s;
        t.erase(t.begin());
        t.erase(t.end()-1);
        if(t.find(s) != -1) return true;
        return false;
    }
};

*思路二:KMP算法

KMP算法是做前后缀的最长匹配,前后缀的最长公共子串不包含的子串即位最小重复子串。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

// c++
class Solution {
public:

    void getNext(int *next, const string& s){
        int j=0, i;
        next[0] = 0;
        for(int i=1; i<s.size(); i++){
            while(j > 0 && s[i]!=s[j]) j = next[j-1];
            if(s[i] == s[j]) j++;
            next[i] = j;
        }
    }
    bool repeatedSubstringPattern(string s) {
        int *next = new int[s.size()];
        getNext(next, s);
        int len = s.size();
        if(next[len-1] > 0 && len%(len - next[len-1])==0) return true;
        return false;
    }
};
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值