1. 程式人生 > >leetcode+ 棧的經典題

leetcode+ 棧的經典題

點選開啟連結
class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        if(s.size()==0) return true;
        for(int i=0;i<s.size(); i++){
            if(s[i]=='{' || s[i]=='[' || s[i]=='(') st.push(s[i]);
            else if(s[i]=='}'){
                if(!st.empty()&&st.top()=='{') st.pop();
                else return false;
            }
            else if(s[i]==']'){
                if(!st.empty()&&st.top()=='[') st.pop();
                else return false;
            }
            else if(s[i]==')'){
                if(!st.empty()&&st.top()=='(') st.pop();
                else return false;
            }
        }
        if(!st.empty()) return false;
        else return true;
    }
};


相關推薦

每日一LeetCode 簡單集合496,682,232,225,155,844,20

496 下一個最大的元素方法1 :自己瞎寫的 沒有考慮到棧的特性class Solution:def nextGreaterElement(self, nums1, nums2): L=[] for i in nums1: k=nums2.index(i)

POJ 2796:Feel Good 單調經典

題目大意:給出一組數字,求一區間,使得區間元素和乘以區間最小值最大,結果要求給出這個最大值和區間的左右端點。 Description Bill is developing a new mathematical theory for human emotions. His recent inv

leetcode經典系列------和佇列

題目一:用佇列實現棧描述:設計一個棧,支援如下操作,這些操作的演算法複雜度需要是常數級,O(1) , 棧的內部儲存資料的結構為佇列,佇列的方法只能包括push、peek(front)、pop、size、empty等標準的佇列方法思路:利用兩個佇列實現棧 class MyStack{ public:

leetcode+ 經典

點選開啟連結class Solution { public: bool isValid(string s) { stack<char> st; if(

leetcode經典系列------連結串列

定義如下: struct ListNode{ int val; ListNode *next; ListNode(int x):val(x),next(NULL){} }; 第一題:連結串列翻轉 easy ListNode * reverseList(Li

LeetCode第155 最小

str 兩個棧 pop 入棧 .get turn 如果 ros ... 設計一個支持 push,pop,top 操作,並能在常數時間內檢索到最小元素的棧。push(x) -- 將元素 x 推入棧中。pop() -- 刪除棧頂的元素。top() -- 獲取棧頂元素。getMi

leetcode--5、valid-parentheses(有效括號)

brackets lin white cnblogs 匹配 ria order == style 題目描述 Given a string containing just the characters‘(‘,‘)‘,‘{‘,‘}‘,‘[‘and‘]‘, determine

leetCode編程

lis 思考 span delet tno let lee 編程 clas 已知鏈表1->2->3->4,先需要刪除3這個節點,請完成函數。註意,這裏只給定要刪除的節點3,並不知道3之前的節點是哪個,以及整個鏈表是什麽節點。 void delete(Li

leetcode算法1: 兩個二進制數有多少位不相同?異或、位移、與運算的主場

output 判斷 ++ 輸入 urn ger ria 結果 ret /* The Hamming distance between two integers is the number of positions at which the corresponding bit

leetcode算法3:分組,讓每個組的最小者,相加之後和最大。想知道桶排序是怎麽樣的嗎?

get ons 表示 note stdlib.h 不為 ask include tor /* Given an array of 2n integers, your task is to group these integers into n pairs of intege

leetcode算法2: 合並兩個二叉樹。遞歸,如何切入並保持清醒?

leetcode算法題2: 合並兩個二叉樹。遞歸 如何切入並保持清醒? /* Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees

LeetCode 第 3 (Longest Substring Without Repeating Characters)

sts mod while find his 代碼 key 不存在 簡單 LeetCode 第 3 題(Longest Substring Without Repeating Characters) Given a string, find th

LeetCode 第 342 (Power of Four)

-m clas script pri ack ber 要求 case ret LeetCode 第 342 題(Power of Four) Given an integer (signed 32 bits), write a function

LeetCode 第 231 (Power of Two)

i++ ret write data mark hid article views pop LeetCode 第 231 題 (Power of Two) Given an integer, write a function to determi

leetcode算法筆記|two sum

blog cnblogs etc 答案 cti com 題目 return function 題目: 我的答案: /** * @param {number[]} nums * @param {number} target * @return {number[]}

linux經典 庫(一)

awk   grep   sed1.1創建一個目錄/data。1) 解答: mkdir /data擴展:mkdir後面可以加參數-p,表示遞歸創建,也可以加參數-v,表示可以顯示出創建的過程。1.2為oldboy.txt增加內容為“I am studying linux.”。解答:為文

最小割經典(兩個點依附在一起的情況)poj3469

minimum 兩個 color computer upd 情況 nim %d struct Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 25099

一些leetcode算法

遇到 記錄 一般來說 兩種 aaa 葉子 sub count led DFS算法 思想:一直往深處走,直到找到解或者走不下去為止 DFS(dep,...) // dep代表目前DFS的深度 { if (找到解或者走不下去了){ return;

寬搜經典之二——8數碼難題+康托展開

col out number ini 它的 proc == pty font 寬搜的定義在上次寬搜一中已講,現在直接看跟本題有關的”康托展開“。 什麽是”康托展開“?其實就是寬搜中實現其主要思想的一個工具——已經考察過的狀態就不再考察。 解釋:X=a[n]*(n-1)!+

Kotlin實現LeetCode算法之String to Integer (atoi)

測試案例 width 說明 max 根據 art system ise tar 題目String to Integer (atoi)(難度Medium) 大意是找出給定字串開頭部分的整型數值,忽略開頭的空格,註意符號,對超出Integer的數做取邊界值處理。