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;
    }
};
相关推荐
点云SLAM几秒前
Tracy Profiler 是目前 C++ 多线程程序实时性能分析工具
开发语言·c++·算法·slam·算法性能分析·win环境性能分析·实时性能分析工具
每天回答3个问题1 分钟前
leetcodeHot100|148.排序链表
数据结构·c++·链表·ue4
We་ct2 分钟前
LeetCode 17. 电话号码的字母组合:回溯算法入门实战
前端·算法·leetcode·typescript·深度优先·深度优先遍历
吃着火锅x唱着歌6 分钟前
LeetCode 447.回旋镖的数量
算法·leetcode·职场和发展
承渊政道7 分钟前
C++学习之旅【unordered_map和unordered_set的使⽤以及哈希表的实现】
c语言·c++·学习·哈希算法·散列表·hash-index
我能坚持多久7 分钟前
【初阶数据结构08】——深入理解树与堆
数据结构·算法
Trouvaille ~10 分钟前
【贪心算法】专题(一):从局部到全局,数学证明下的最优决策
c++·算法·leetcode·面试·贪心算法·蓝桥杯·竞赛
iAkuya16 分钟前
(leetcode)力扣100 92.最小路径和(动态规划)
算法·leetcode·动态规划
shehuiyuelaiyuehao18 分钟前
算法5,有效三角形个数
算法·leetcode·排序算法
丶小鱼丶24 分钟前
数据结构和算法之【数组】
java·数据结构·算法