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;
    }
};
相关推荐
WL学习笔记1 小时前
vector模拟实现
c++
蜗牛一步步往上爬4 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
likexuec6 小时前
数据结构(2)
数据结构
文丑颜不良啊7 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z7 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
wo_jack7 小时前
C++ 入门笔记(十六):第16课 vector 模拟实现十问十答 —— 从三指针到迭代器失效
c++·后端·面试·stl·vector
D e re k ZH8 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶9 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊9 小时前
LeetCode2161~2180题目解析
java·leetcode