首页 > 其他分享 >LeetCode刷题记录.Day26

LeetCode刷题记录.Day26

时间:2022-11-26 22:33:36浏览次数:83  
标签:string Day26 value st result 字符串 LeetCode 刷题

删除字符串中所有相邻重复项

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

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for(char value : s){
            if(st.empty() || st.top() != value){
                st.push(value);
            }
            else{
                st.pop();
                
            }
        }
        string result = "";
        while (!st.empty()) { // 将栈中元素放到result字符串汇总
            result += st.top();
            st.pop();
        }
        reverse (result.begin(), result.end()); // 此时字符串需要反转一下
        return result;
    }
};

栈的应用题,主要考察能不能灵活应用栈的特点来解决字符匹配问题。个人读题的时候一时之间想不到这个方法,大概只能多做多练了

标签:string,Day26,value,st,result,字符串,LeetCode,刷题
From: https://www.cnblogs.com/tianmaster/p/16928512.html

相关文章

  • 刷题02
    像这样写,输出的数组值仍然跟原来的数组一样,因为老数组被去除掉0之后就没有值赋给新数组,那么新数组那个位置的值就是初始值0,所以变来变去还是跟原来一样。所以新的数组需要新......
  • 力扣 leetcode 882. 细分图中的可到达节点
    问题描述给你一个无向图(原始图),图中有n个节点,编号从0到n-1。你决定将图中的每条边细分为一条节点链,每条边之间的新节点数各不相同。图用由边组成的二维数组edg......
  • leetcode-1175-easy
    leetcode-1175-easyReturnthenumberofpermutationsof1tonsothatprimenumbersareatprimeindices(1-indexed.)(Recallthatanintegerisprimeifand......
  • leetcode-929-easy
    UniqueEmailAddressesEveryvalidemailconsistsofalocalnameandadomainname,separatedbythe'@'sign.Besideslowercaseletters,theemailmaycontai......
  • leetcode-696-easy
    CountBinarySubstringsGivenabinarystrings,returnthenumberofnon-emptysubstringsthathavethesamenumberof0'sand1's,andallthe0'sandallth......
  • leetcode-796-easy
    RotateStringGiventwostringssandgoal,returntrueifandonlyifscanbecomegoalaftersomenumberofshiftsons.Ashiftonsconsistsofmovingthe......
  • leetcode-1299-easy
    ReplaceElementswithGreatestElementonRightSideGivenanarrayarr,replaceeveryelementinthatarraywiththegreatestelementamongtheelementstoit......
  • leetcode-110-easy
    BalancedBinaryTreeGivenabinarytree,determineifitisheight-balanced.Example1:Input:root=[3,9,20,null,null,15,7]Output:trueExample2:Input......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:反转字符串中的单词 III
    题目:给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例1:输入:s="Let'stakeLeetCodecontest"输出:"s'teLekatedoCteeL......
  • leetcode_D3_27移除元素
    1.题目   2.解一   主要思路:解一为本人解法,主要思路是先利用循环删除掉所有数组中值等于val的元素,然后可以直接返回数组的长度和其中的元素。感觉是没经过......