leetcode 2810.故障键盘

思路:模拟

其中题解中有用双端队列做的,大家可以参考一下,这里我就展示一下暴力怎么用。

这里就不多说了,直接看模拟过程就行了,代码中会有注释。

注意:这里在反转的时候,作者用到了一个容器就是list容器,之所以用,是因为里面含有一个函数叫做reverse,就是反转的意思,在这里很方便。

复制代码
class Solution {
public:
    string finalString(string s) {
        list<char>L;
        int n=s.size();
        string buf;
        int index1=0;
        int index2=0;
        for(int i=0;i<n;i++){
            if(s[i]=='i'){
                index2=i;
                if(index1+1==index2&&index1!=0)
                L.reverse();
                else{
                for(int j=index1;j<index2;j++){
                    if(s[j]!='i')
                    L.push_back(s[j]);
                }
                L.reverse();
                }
                
                index1=index2;
            }
        }
        for(auto it:L)
        buf+=it;
        if(index2!=0)
        buf+=s.substr(index2+1);
        else
        buf+=s.substr(index2);
        return buf;
    }
};
相关推荐
不会就选b1 小时前
算法日常・每日刷题--<贪心>14
算法
初願致夕霞1 小时前
C++11:类型推导与完美转发复盘笔记
c++
小猪写代码2 小时前
C++中什么是类,什么是对象
c++
mmmmath_34 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z4 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
醇氧5 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战
数据结构·算法
大熊背5 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)
算法·白平衡·色度·色温
钓鱼的肝5 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
参.商.6 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang