7.12-7.15 学习记录

目录

344. 反转字符串

541. 反转字符串II

剑指Offer 05.替换空格

151.翻转字符串里的单词

剑指Offer58-II.左旋转字符串

28. 实现 strStr()


344. 反转字符串

344. 反转字符串 - 力扣(LeetCode)

class Solution {
public:
    void reverseString(vector& s) {
        int left =0,right = s.size()-1;
        char tmp;
        while(left

541. 反转字符串II

 541. 反转字符串 II - 力扣(LeetCode)

class Solution {
public:
    void reverseK(int left, int right, string& s){
        if (right>s.size()-1) right = s.size()-1;
        char tmp;
        while(left

剑指Offer 05.替换空格

剑指 Offer 05. 替换空格 - 力扣(LeetCode)

class Solution {
public:
    string replaceSpace(string s) {
        int count = 0 ;
        for (auto c:s){
            if(c ==' ')count++;
        }
        if (count==0)return s;
        int left = s.size(),right = left+2*count;
        s.resize(right);
        while(left>-1){
            if(s[left]!=' '){
                s[right--] = s[left--];
            }
            else{
                s[right--] = '0';
                s[right--] = '2';
                s[right--] = '%';
                left--;
            }
        }
        return s;
    }
};

151.翻转字符串里的单词

151. 反转字符串中的单词 - 力扣(LeetCode)

class Solution {
public:
    void reversePart(string &s,int left,int right){
        char tmp;
        while(left

剑指Offer58-II.左旋转字符串

剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode)

class Solution {
public:
    string reverseLeftWords(string s, int n) {
        reverse(s.begin(),s.begin()+n);
        reverse(s.begin()+n,s.end());
        reverse(s.begin(),s.end());
        return s;
    }
};

28. 实现 strStr()

28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode)

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        left = 0
        right = 0
        n = len(haystack)
        t = len(needle)
        tmp = self.getBack(needle)
        while (left0 and needle[left]!=needle[right]):
                    left = tmp[left-1]
                if (left==0 and needle[left]!=needle[right]):
                    tmp[right] = 0
                    right+=1
        return tmp
class Solution {
public:
    void getBack(string needle,int tmp[],int t){
        int left = 0,right = 1;
        tmp[0] = 0;
        while(right

你可能感兴趣的:(学习,leetcode,python,数据结构,算法,c++,字符串)