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;
    }
};
相关推荐
生态学者1 小时前
Global Change Biology | 森林越“步调一致”,反而越怕干旱?树木生长与叶片活动的同步之谜
大数据·人工智能·算法·r语言·微信公众平台
所以遗憾是什么呢?1 小时前
【题解】Educational Codeforces Round 195 (Rated for Div. 2) ABCDE
c++·动态规划·acm·icpc·ccpc·xcpc·codeforecs
lsswear2 小时前
Agent 框架新手入门与实战部署指南
大数据·人工智能·算法
学逆向的2 小时前
C 循环结构的执行效率分析
c语言·开发语言·数据结构·网络安全
Lhan.zzZ3 小时前
Qt WebEngineView 在 Linux 上无法加载网页:从“看不见 HTML“到“禁用沙箱“
linux·c++·qt
Data analyse4563 小时前
rnweb 回放数据量太大怎么办?分片存储与断点续传的实现思路
前端·网络·算法·数据分析
sysu_chenxy4 小时前
二重循环输出二维图形
c++
Navigator_Z5 小时前
LeetCode //C - 1293. Shortest Path in a Grid with Obstacles Elimination
c语言·算法·leetcode
Nil2085 小时前
leetcode 64最小路径和
算法·leetcode·职场和发展
光影少年5 小时前
AI Agent、自动化与未来工作
人工智能·算法·架构