【每日刷题】Day34

【每日刷题】Day34

🥕个人主页:开敲🍉

🔥所属专栏:每日刷题🍍

🌼文章目录🌼

1. 1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

2. 1475. 商品折扣后的最终价格 - 力扣(LeetCode)

3. 1544. 整理字符串 - 力扣(LeetCode)

1. 1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode)

//思路:栈。将字符串中字符与栈顶元素比较是否相同,相同则推出栈顶元素,否则将字符入栈,如果栈中元素为0,直接入栈。

char* removeDuplicates(char* s)

{

    int size = strlen(s);

    char* arr = (char*)malloc(sizeof(char)*(size+1));

    int count = 0;

    for(int i = 0;i<size;i++)

    {

        if(count==0)//如果栈中元素为0,直接入栈

        {

            arr[count++] = s[i];

        }

        else if(s[i]!=arr[count-1])//不相同,将字符入栈

        {

            arr[count++] = s[i];

        }

        else if(count>0)//相同则推出栈顶元素

        {

            count--;

        }

    }

    arr[count] = '\0';

    return arr;

}

2. 1475. 商品折扣后的最终价格 - 力扣(LeetCode)

//思路:双指针。定义两个指针,slow用于定位当前商品价格,fast用于定位当前商品的折扣,如果折扣≤商品价格,则直接将商品价格减去折扣。

int* finalPrices(int* prices, int pricesSize, int* returnSize)

{

    int slow = 0;

    int fast = 1;

    while(slow<pricesSize-1)

    {

        if(prices[fast]<=prices[slow])//折扣≤商品价格

        {

            prices[slow]-=prices[fast];

            slow++;

            fast = slow+1;

        }

        else//否则继续向下遍历折扣

        {

            fast++;

        }

        if(fast==pricesSize)//如果走到了数组末尾,说明当前商品无折扣

        {

            slow++;

            fast = slow+1;

        }

    }

    *returnSize = pricesSize;

    return prices;

}

3. 1544. 整理字符串 - 力扣(LeetCode)

//0ms  100%思路:栈。遍历字符串,如果当前字符与栈顶字符是大小写关系,则推出栈顶;如果不是大小写关系并且栈内元素个数为0,则将当前字符入栈。

char* makeGood(char* s)

{

    int size = strlen(s);

    char* ans = (char*)malloc(sizeof(char)*(size+1));

    memset(ans,0,sizeof(char)*(size+1));

    int count = 0;

    for(int i = 0;i<size;i++)

    {

        if(count==0)//栈中没有元素,将当前字符入栈

        {

            ans[count++] = s[i];

        }

        else if(ans[count-1]==(s[i]+32)||ans[count-1]==(s[i]-32))//当前字符与栈顶元素为大小写关系,推出栈顶

        {

            count--;

        }

        else//否则,入栈

        {

            ans[count++] = s[i];

        }

    }

    ans[count] = '\0';

    return ans;

}

  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值