leetcode1047. 删除字符串中的所有相邻重复项
生活随笔
收集整理的這篇文章主要介紹了
leetcode1047. 删除字符串中的所有相邻重复项
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一:題目
二:上碼
方法一
class Solution { public:string removeDuplicates(string s) {if(s.size() == 0) return {};string str;stack<char> st;st.push(s[0]);for(int i = 1; i < s.size(); i++) {if(!st.empty() && st.top() == s[i]) {st.pop();}else{st.push(s[i]);}} while(!st.empty()) {str += st.top();st.pop();}reverse(str.begin(),str.end());return str;} };方法二
class Solution { public:string removeDuplicates(string s) {if(s.size() == 0) return {};string str;stack<char> st;stack<char> q;st.push(s[0]);for(int i = 1; i < s.size(); i++) {if(!st.empty() && st.top() == s[i]) {st.pop();}else{st.push(s[i]);}} while(!st.empty()) {q.push(st.top());st.pop();}while(!q.empty()) {str += q.top();q.pop();}return str;} }; 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的leetcode1047. 删除字符串中的所有相邻重复项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wps2016版中的宏启动如何启用
- 下一篇: 吃鸡怎么注销微信账号