Leetcode 栈

20. Valid Parentheses
Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

Example 1:

Input: “()”
Output: true
Example 2:

Input: “()[]{}”
Output: true
Example 3:

Input: “(]”
Output: false
Example 4:

Input: “([)]”
Output: false
Example 5:

Input: “{[]}”
Output: true
C++

class Solution {
public:
    bool isValid(string s) {
        stack<char> r;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='(' || s[i]=='[' || s[i]=='{')
                r.push(s[i]);
            else
            {
                if(r.empty())
                    return false;
                char t=r.top();
                r.pop();
                if(s[i]==')'&&t!='(') return false;
                if(s[i]==']'&&t!='[') return false;
                if(s[i]=='}'&&t!='{') return false;
            }
        }
        if(!r.empty()) return false;
        return true;
    }
};

155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
getMin() – Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
设计一个最小栈,实现push, pop, top, getMin四个功能。相比原来的栈多了一个功能,可以返回当前栈内的最小值
解法:使用2个栈,栈1记录进来的数,栈2记录目前的最小值。当有新数push进来的时候,如果栈2为空或者这个数小于栈2顶上的值,就把这个数推入栈2。当pop的数正好等于最小值时,说明当前栈内的最小值变化了,要弹出这个最小值,记录的下一个最小值来到栈顶。
C++

class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {
        
    }
    
    void push(int x) {
        s1.push(x);
        if(s2.empty() || x<=s2.top())
            s2.push(x);
    }
    
    void pop() {
        if(s1.top()==s2.top()) s2.pop();
        s1.pop();
    }
    
    int top() {
        return s1.top();
    }
    
    int getMin() {
        return s2.top();
    }
    private:
    stack<int> s1,s2;
};

224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .

Example 1:

Input: “1 + 1”
Output: 2
Example 2:

Input: " 2-1 + 2 "
Output: 3
Example 3:

Input: “(1+(4+5+2)-3)+(6+8)”
Output: 23
JAVA(难)

class Solution {
    public int calculate(String s) {
         if (s.length() == 0) {
            return 0;
        }
        s = "(" + s + ")";
        int[] pos = {0};
        return eval(s, pos);
    }
 
    private static int eval(String s, int[] pos) {
        int val = 0, i = pos[0], sign = 1, num = 0;
        while(i < s.length()) {
            char c = s.charAt(i);
            switch(c) {
                case '+': val = val + sign * num; num = 0; sign = 1; i++; break;
                case '-': val = val + sign * num; num = 0; sign = -1; i++; break;
                case '(': pos[0] = i + 1; val = val + sign * eval(s, pos); i = pos[0]; break;
                case ')': pos[0] = i + 1; return val + sign * num; 
                case ' ': i++; continue;
                default : num = num * 10 + c - '0'; i++;
            }
        }
        return val;
    }
}

232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.

push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Example:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
这道题让我们用栈来实现队列。栈和队列的核心不同点就是栈是先进后出,而队列是先进先出,那么我们要用栈的先进后出的特性来模拟出队列的先进先出。那么怎么做呢,其实很简单,只要我们在插入元素的时候每次都都从前面插入即可,比如如果一个队列是1,2,3,4,那么我们在栈中保存为4,3,2,1,那么返回栈顶元素1,也就是队列的首元素,则问题迎刃而解。所以此题的难度是push函数,我们需要一个辅助栈tmp,把s的元素也逆着顺序存入tmp中,此时加入新元素x,再把tmp中的元素存回来,这样就是我们要的顺序了,其他三个操作也就直接调用栈的操作即可
C++

class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        stack<int> tmp;
        while(!s.empty())
        {
            tmp.push(s.top());
            s.pop();
        }
        s.push(x);
        while(!tmp.empty())
        {
            s.push(tmp.top());
            tmp.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int r=s.top();
        s.pop();
        return r;
    }
    
    /** Get the front element. */
    int peek() {
        return s.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s.empty();
    }
    private:
      stack<int> s;
};

496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.

Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
题目其实很简单,关键在于理解:
以nums1 = [4, 1, 2],nums2 = [1, 3, 4, 2]为例,
nums1[0] = 4,对应为nums2[2],在nums2中,4右边没有大于4的值,因此返回-1
nums1[1] = 1,对应为nums2[0],在nums2中,1右边第一个大于1的值为3,因此返回3
分析:使用栈,从后往前遍历nums2[i],每当栈不为空的时候,一直出栈直到遇到比nums2[i]大的数字停止。设立一个map<int, int> m,存储nums2中每一个元素以及它对应的下一个最大元素构成的映射。如果停止后栈为空就将m[nums2[i]]标记为-1,否则就写栈的栈顶元素~
最后将Nums1中出现的每一个元素对应的map的值放入result数组中返回~
C++

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> result;
        stack<int> s;
        map<int,int> m;
        for(int i=nums2.size()-1;i>=0;i--)
        {
            while(!s.empty() && s.top()<=nums2[i])
                s.pop();
            m[nums2[i]]=s.empty()?-1:s.top();
            s.push(nums2[i]);
        }
        for(int i=0;i<nums1.size();i++)
            result.push_back(m[nums1[i]]);
        return result;
    }
};

682. Baseball Game
You’re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

Integer (one round’s score): Directly represents the number of points you get in this round.
“+” (one round’s score): Represents that the points you get in this round are the sum of the last two valid round’s points.
“D” (one round’s score): Represents that the points you get in this round are the doubled data of the last valid round’s points.
“C” (an operation, which isn’t a round’s score): Represents the last valid round’s points you get were invalid and should be removed.
Each round’s operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example 1:
Input: [“5”,“2”,“C”,“D”,"+"]
Output: 30
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2’s data was invalid. The sum is: 5.
Round 3: You could get 10 points (the round 2’s data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.
题目的要求是,按照给定的字符串,计算出每个回合得到的分数,同时计算出总分,最后返回总分。计算分数的规则如下:
数字:当前回合的直接得分
C:上一回合的得分取消
D:本回合得到的分数为上一次得到的分数的两倍
+:本回合的得分为前两次得到的分数之和
解决问题的思路是:遍历给定的字符串,判断每次回合得到的分数。利用vector去存储每一回合得到的分数:
数字:入栈
C:取消上一回合得到的分数,即vector中弹出最近添加的数字,出栈
D:新入栈的元素为上一元素的两倍
+:新入栈的元素为之前两个元素之和
C++

class Solution {
public:
    int getNum(string& s) {
        int num;
        stringstream ss;
        ss << s;
        ss >> num;
        return num;
    }
    int calPoints(vector<string>& ops) {
       int sum = 0;
        vector<int> round;
        for (int i = 0; i < ops.size(); i++) {
            if (ops[i] == "C") {
                sum -= round.back();
                round.pop_back();
            } else if (ops[i] == "D") {
                int temp = 2 * round.back();
                round.push_back(temp);
                sum += temp;
            } else if (ops[i] == "+") {
                int index = round.size() - 1;
                int temp = round[index] + round[index-1];
                round.push_back(temp);
                sum += temp;
            } else {
                int temp = getNum(ops[i]);
                sum += temp;
                round.push_back(temp);
            }
        }
        return sum;
    }
};

844. Backspace String Compare
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

Example 1:

Input: S = “ab#c”, T = “ad#c”
Output: true
Explanation: Both S and T become “ac”.
给两个字符串,字符串中的#代表退格,遇到退格就删除前面一个有效字符,经过处理之后比较两个字符是否相等。
由于退格符号作用于最近的一个有效字符,因此可以使用栈存储string的每一个有效字符,当遍历到一个# 并且栈非空的时候就从栈顶pop出一个元素,遇到有效字符则直接入栈。最后比较两个栈中的字符是否完全相等。
C++

class Solution {
public:
    bool backspaceCompare(string S, string T) {
        return getS(S)==getS(T);  
    }  
         
    string getS(string& s) {  
        string a = "";  
        for (char c: s) {  
            if (c=='#'){  
                if (!a.empty()) a.pop_back();//a = a.substr(0, a.size()-1);  
            }  
            else a+=c;  
        }  
        return a;   
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值