leetcode (栈->简单) 496,1047,20,155,225,232,682,844,1544,1598

496(注释掉的地方是一开始的思路,提交后结果很差,才发现思路有问题,看了解题思路才发现  一个元素找到右边第一个比其大的元素说明这个元素和大的那个元素中间的所有元素都应该是满足的)

    public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Map<Integer,Integer> map = new HashMap<>(nums2.length);

        LinkedList<Integer> list = new LinkedList<>();
        for (Integer integer : nums2) {
            while (list.peek() != null && list.peek() < integer){
                map.put(list.poll(),integer);
            }
            list.push(integer);
        }
//        LinkedList<Integer> tpList = new LinkedList<>();
//        for (int i = nums2.length-1; i >= 0; i--) {
//
//            if (i==nums2.length-1){
//
//                map.put(nums2[nums2.length-1],-1);
//            }else {
//                Integer k ;
//                map.put(nums2[i],-1);
//                while ((k = list.poll()) != null){
//                    tpList.push(k);
//                    if (nums2[i] < k){
//                        map.put(nums2[i],k);
//                        break;
//                    }
//                }
//                while ((k = tpList.poll()) != null){
//                    list.push(k);
//                }
//
//            }
//            list.push(nums2[i]);
//
//        }
        int[] result = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
                result[i] = map.getOrDefault(nums1[i],-1);
        }

        return result;
    }

1047

class Solution {
    public String removeDuplicates(String S) {
//char[] chars = S.toCharArray();
        LinkedList<Character> stack = new LinkedList<>();
        for (int i = 0;i < S.length() ; i++) {
            if (!stack.isEmpty() && stack.peek() == S.charAt(i)){
                stack.pop();
            }else {
                stack.push(S.charAt(i));
            }

        }
        StringBuilder s = new StringBuilder();
        while (!stack.isEmpty()){
            s.append(stack.poll());
        }
        s.reverse();
        return s.toString();
    }
}

20

class Solution {
    public boolean isValid(String s) {
Map<Character,Character> map = new HashMap<>();
        map.put('(',')');
        map.put('{','}');
        map.put('[',']');

        LinkedList<Character> list = new LinkedList<>();

        for (int i = 0; i < s.length(); i++) {
            if(!list.isEmpty() && map.get(list.peek()) != null && map.get(list.peek()).equals(s.charAt(i))){
                list.poll();
            }else {
                list.push(s.charAt(i));
            }
        }

        return list.isEmpty();
    }
}

155

public class StaskSimpl4 {
    int min;
    Node root;


    /** initialize your data structure here. */
    public StaskSimpl4() {

    }

    public void push(int x) {
        if (root == null){
            root = new Node(x);
            min = x;
            root.min = x;
        }else {
            Node n = new Node(x);
            if (x <= min){
                min = x;
                n.min = x;
                n.next = root;
                root = n;
            }else {
                n.min = min;
                n.next = root;
                root = n;
            }

        }
    }

    public void pop() {
        if (root.next == null){
            min = 0;
            root = null;
        }else {
            min = root.next.min;
            root = root.next;
        }

    }

    public int top() {
        return root.val;
    }

    public int getMin() {
        return min;
    }

    static class Node{
        int val;
        int min;
        Node next;

        public Node(int val) {
            this.val = val;
        }
    }


    public static void main(String[] args) {

        StaskSimpl4 staskSimpl4 = new StaskSimpl4();
        staskSimpl4.push(45);
        staskSimpl4.push(4);
        staskSimpl4.push(2);
        staskSimpl4.push(3);
        staskSimpl4.push(10);


        staskSimpl4.push(-2);
        staskSimpl4.push(0);
        staskSimpl4.push(-3);
        System.out.println(staskSimpl4.getMin());
        staskSimpl4.pop();
        System.out.println( staskSimpl4.top());
        System.out.println(staskSimpl4.getMin());


    }
}

225 看到的一个大佬的思路,用两个队列来实现

class MyStack {

    Deque<Integer> input;
    Deque<Integer> output;

    public MyStack() {
        input = new ArrayDeque<>();
        output = new ArrayDeque<>();
    }


    /** Push element x onto stack. */
    public void push(int x) {
        input.offer(x);
        while (!output.isEmpty()){
            input.offer(output.poll());
        }
        Deque t = input;
        input = output;
        output = t;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return output.pop();
    }

    /** Get the top element. */
    public int top() {
        return output.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return output.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

232  思路和上题基本一致

public class StaskSimpl6 {

    Deque<Integer> inqueue ;
    Deque<Integer> tpQueue;

    /** Initialize your data structure here. */
    public StaskSimpl6() {
        inqueue = new ArrayDeque<>();
        tpQueue = new ArrayDeque<>();
    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        //inqueue.push(x);
        while (!inqueue.isEmpty()){
            tpQueue.push(inqueue.poll());
        }
        tpQueue.push(x);
       while (!tpQueue.isEmpty()){
           inqueue.push(tpQueue.poll());
       }
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        return inqueue.pop();
    }

    /** Get the front element. */
    public int peek() {
        return inqueue.peek();
    }

    /** Returns whether the queue is empty. */
    public boolean empty() {
        return inqueue.isEmpty();
    }
}

682

class Solution {
    public int calPoints(String[] ops) {
  ArrayDeque<Integer> queue = new ArrayDeque<>();
        for (String op : ops) {
            switch (op) {
                case "+":
                    Integer t1 = queue.pop();
                    Integer t2 = queue.pop();
                    queue.push(t2);
                    queue.push(t1);
                    queue.push(t1 + t2);
                    break;
                case "D":
                    Integer t3 = queue.pop();
                    queue.push(t3);
                    queue.push(t3 * 2);
                    break;
                case "C":
                    queue.pop();
                    break;
                default:
                    queue.push(Integer.valueOf(op));
            }
        }
        int k = 0;
        while (!queue.isEmpty()){
            k+=queue.pop();
        }
        return k;
    }
}

844  

  用栈解决是这样

class Solution {
    public boolean backspaceCompare(String S, String T) {
        ArrayDeque<Character> arrayDeque1 = new ArrayDeque<>();
        ArrayDeque<Character> arrayDeque2 = new ArrayDeque<>();
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) == '#'){
                if (!arrayDeque1.isEmpty()){
                    arrayDeque1.pop();
                }
            }else {
                arrayDeque1.push(S.charAt(i));
            }
        }
        for (int i = 0; i < T.length(); i++) {
            if (T.charAt(i) == '#'){
                if (!arrayDeque2.isEmpty()){
                    arrayDeque2.pop();
                }
            }else {
                arrayDeque2.push(T.charAt(i));
            }
        }
        if (arrayDeque1.size() != arrayDeque2.size()){
            return false;
        }
        while (!arrayDeque1.isEmpty()){
            if (arrayDeque1.pop() != arrayDeque2.pop()){
                return false;
            }
        }

        return true;
    }
}

  不过看到有大神用一个变量指针来模拟栈,这样空间复杂度就是O(1),所以最标准的做法应该是如下

class Solution {
        public static boolean backspaceCompare(String S, String T) {
        char[] chars1 = S.toCharArray();
        char[] chars2 = T.toCharArray();
        int change = change(chars1);
        int change1 = change(chars2);
        if (change != change1){
            return false;
        }
        for (int i = 0; i <= change && change >= 0; i++) {
            if (chars1[i] != chars2[i]){
                return false;
            }
        }

        return true;
    }

    public static int change(char[] s){
        int k = -1;
        for (char c : s) {
            if (c == '#'){
               if (k != -1){
                   k--;
               }
            }else {
                s[++k] = c;
            }
        }
        return k;

    }
}

1544 原理同上 可以不用栈

class Solution {
public static String makeGood(String s) {
        char[] chars = s.toCharArray();
        int k = -1;
        for (char c : chars) {
            if (k < 0){

                k++;
                chars[k] = c;
            }else {
                if (Character.isUpperCase(c) && Character.isLowerCase(chars[k]) && (Character.toLowerCase(c) == Character.toLowerCase(chars[k]) )){
                    k--;
                }else if (Character.isLowerCase(c) && Character.isUpperCase(chars[k]) && (Character.toLowerCase(c) == Character.toLowerCase(chars[k]) )){
                    k--;
                }else {
                    chars[++k] = c;
                }

            }
        }
        if (k == -1){
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <=k ; i++) {
            sb.append(chars[i]);
        }

        return sb.toString();
    }
}

1598

public static int minOperations(String[] logs) {
        int k = 0;
        for (String log : logs) {
            if (log.equals("../")){
                if (k !=0){
                    k--;
                }
            }else if (log.equals("./")){

            }else {
                k++;
            }
        }
        return k;
    }
原文地址:https://www.cnblogs.com/hetutu-5238/p/14309569.html