代码随想录算法训练营第八天 | 第四章 字符串part01 | 344.反转字符串 541. 反转字符串II LCR 122.路径加密 151.翻转字符串里的单词 LCR 182.动态口令

344.反转字符串

题目来源:leetcode 题库 344.反转字符串

思路

基本思路

使用双指针

题目要求原地修改,直接修改传入指针即可

起始

left = 0;

right = sSize - 1;

终止条件

不满足left < right

题解

void reverseString(char* s, int sSize){
    int left = 0;
    int right = sSize - 1;
    char temp;
    while(left < right)
    {
        temp = s[left];
        s[left] = s[right];
        s[right] = temp;
        left++;
        right--;
    }
}

541. 反转字符串II

题目来源:541. 反转字符串II

思路

基本思路

题解

越界了,回头再看

==20==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000035 at pc 0x55c57483083f bp 0x7ffe22d48480 sp 0x7ffe22d48470 READ of size 1 at 0x602000000035 thread T0 #2 0x7f9f699e1082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) 0x602000000035 is located 0 bytes to the right of 5-byte region [0x602000000030,0x602000000035)

static void MyReverseString(char* s, int start, int end){
    char temp;
    while(start < end)
    {
        temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start++;
        end--;
    }
}

char * reverseStr(char * s, int k){
    char* Ret = s;
    int start = 0;
    int end = 0;
    int len = 0;
    while(s[end] != '\0')
    {   
        len++;
        end++;
        if(len == (2 * k))
        {
            MyReverseString(s, start, start + k -1);
            start = end;
            len = 0;
        }
    }
    if((len > 1) && (len < k))
    {
        MyReverseString(s, start, end - 1);
    }
    else if(len < (2 *k))
    {
        MyReverseString(s, start, start + k -1);
    }
    return Ret;
}

LCR 122.路径加密

题目来源:leetcode LCR 122.路径加密

思路

字符串以'\0'为结束符,遍历一遍,将'.'的字符替换成' '即可

题解

char* pathEncryption(char* path) {
    char* head = path;
    while((*path) != '\0')
    {
        if((*path) == '.')
        {
            *path = ' ';
        }
        path++;
    }
    return head;
}

看解析后

原题被删了,原题:

题目:剑指Offer 05.替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1: 输入:s = "We are happy."
输出:"We%20are%20happy."

思路

遍历一遍,判断有几个空格,得到最后的产犊,malloc创建数组空间;

从字符串尾到字符串首 遍历,赋字符


151.翻转字符串里的单词

题目来源:leetcode 题库 151.翻转字符串里的单词

思路

先去除首尾及中间多余的空格

若直接翻转字符串

 根据题意,还需要翻转字符串里的单词

所以去除多余空格,再翻转字符串,再识别反转后字符串里的单词将其翻转

去除多余空格

 使用双指针,fast指针从字符串头到尾遍历(s[fast] = '\0'即为队尾)

        若s[fast]不为空格,将fast索引的值赋给slow: s[slow++] = s[fast++]

        若s[fast]为空格

                若slow为0,在队首,不赋值

                若s[slow - 1]也为空格,中间多余空格,不赋值

遍历完成后,剩余字符串长度为slow

遍历完成后,队尾的空格还没处理,检查队尾若为空格,slow--;

    /* 去除多余空格 */
    while(s[fast] != '\0')
    {
        if((s[fast] == ' ') && ((slow == 0) || (s[slow - 1] == ' ')))
        {
            fast++;
        }
        else
        {
            s[slow++] = s[fast++];
        }
    }
    /* 还有结尾的空格需要去除,此时slow为字符串长度 */
    while(s[slow -1] == ' ')
    {
        slow--;
    }
    len = slow;

翻转字符串

static void MyReverseString(char* s, int start, int end){
    char temp;
    while(start < end)
    {
        temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start++;
        end--;
    }
}


/* 调用MyReverseString翻转字符串 */
MyReverseString(s, 0, slow-1);

找到翻转后的字符串中的单词并翻转之

由于已删除多余的空格,fast从字符串头遍历到尾,若s[fast]为空格,找到一个单词

slow记录单词的开始

遍历结束后,最后一个单词是以'\0'结尾的,还没翻转,将之翻转

    /* 找出反转后的字符串里的单词,翻转之 */
    slow = 0;
    for(fast = 0; fast < len; fast++)
    {
        if(s[fast] == ' ')
        {
            MyReverseString(s, slow, fast-1);
            slow = fast+1;
        }
    }
    MyReverseString(s, slow, fast-1);

在字符串结尾处填'\0'

由于函数返回是个指针,也没有返回长度,调用者会以字符串对返回的指针进行处理,由于我们删除了多余的空格,字符串的长度已经改变,我们要在s[字符串长度]处赋'\0'说明字符串到此结束

题解


static void MyReverseString(char* s, int start, int end){
    char temp;
    while(start < end)
    {
        temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start++;
        end--;
    }
}

char * reverseWords(char * s){
    int fast = 0;
    int slow = 0;
    int len;
    char* start = s;

    /* 去除多余空格 */
    while(s[fast] != '\0')
    {
        if((s[fast] == ' ') && ((slow == 0) || (s[slow - 1] == ' ')))
        {
            fast++;
        }
        else
        {
            s[slow++] = s[fast++];
        }
    }
    /* 还有结尾的空格需要去除,此时slow为字符串长度 */
    while(s[slow -1] == ' ')
    {
        slow--;
    }
    len = slow;

    /* 翻转字符串 */
    MyReverseString(s, 0, slow-1);

    /* 找出反转后的字符串里的单词,翻转之 */
    slow = 0;
    for(fast = 0; fast < len; fast++)
    {
        if(s[fast] == ' ')
        {
            MyReverseString(s, slow, fast-1);
            slow = fast+1;
        }
    }
    MyReverseString(s, slow, fast-1);
    s[fast] = '\0';
    return s;
}

 LCR 182.动态口令

leetcode题已删除:leetcode LCR 182.动态口令

解析:代码随想录

原题

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1:
输入: s = "abcdefg", k = 2
输出: "cdefgab"

示例 2:
输入: s = "lrloseumgh", k = 6
输出: "umghlrlose"

限制:
1 <= k < s.length <= 10000

删后题变成了

某公司门禁密码使用动态口令技术。初始密码为字符串 password,密码更新均遵循以下步骤:

  • 设定一个正整数目标值 target
  • 将 password 前 target 个字符按原顺序移动至字符串末尾

请返回更新后的密码字符串。

思路

暴力

创建target个字符的空间存储前target个字符,将字符串整体左移target, 将后target个字符赋值为初始的前target个字符

非暴力想不出来

看题解思路吧

看解析后思路

将一个字符串翻转后,前 n个字符就变成了翻转后的后n个字符(虽然顺序翻转了),把这部分再翻转过来,就是前n个字符整体移动到了最后n个字符。另一部分也要翻转才能保持原来顺序不变

因此步骤为

  1. 反转区间为前n的子串
  2. 反转区间为n到末尾的子串
  3. 反转整个字符串

题解

​​​​​​static void MyReverseString(char* s, int start, int end){
    char temp;
    while(start < end)
    {
        temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start++;
        end--;
    }
}

char* dynamicPassword(char* password, int target) {
    int len = strlen(password);
    MyReverseString(password, 0, target-1);
    MyReverseString(password, target, len-1);
    MyReverseString(password, 0, len-1);
    return password;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值