leetcode-探索(队列+栈)小结

1.用栈实现队列
/*
用两个栈来实现队列
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:37.5 MB, 在所有 Java 提交中击败了7.14%的用户
*/
 private  Stack<Integer> s1 ;
    private Stack<Integer> s2 ;
    /** Initialize your data structure here. */
    public MyQueue() {
        s1 =  new Stack<>();
        s2 =  new Stack<>();

    }

    /** Push element x to the back of queue. */
    public void push(int x) {
        s1.add(x);
    }

    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
      if (s1.isEmpty()&&s2.isEmpty()){
          return -1;
      }
      if (!s1.isEmpty()&&s2.isEmpty()){
          while (!s1.isEmpty()){
              s2.add(s1.pop());
          }
      }
      return s2.pop();
    }

    /** Get the front element. */
    public int peek() {
        if (s1.isEmpty()&&s2.isEmpty()){
            return -1;
        }
        if (!s1.isEmpty()&&s2.isEmpty()){
            while (!s1.isEmpty()){
                s2.add(s1.pop());
            }
        }
        return s2.peek();
    }


    /** Returns whether the queue is empty. */
     public boolean empty() {
        if (s1.isEmpty()&&s2.isEmpty()){
            return true;
        }else {
            return false;
        }
    }
2.用队列实现栈
/*
执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗:37.5 MB, 在所有 Java 提交中击败了7.41%的用户
*/
class MyStack {

    private Queue<Integer> q1;
    private Queue<Integer> q2;

    /**
     * Initialize your data structure here.
     */
    public MyStack() {
        q1 = new LinkedList<>();
        q2 = new LinkedList<>();
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        q1.add(x);
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {
        if (q1.isEmpty() && q2.isEmpty()) {
            return -1;
        }
        int temp = 0;
        if (!q1.isEmpty()) {
            while (!q1.isEmpty()) {
                temp = q1.remove();
                q2.add(temp);
            }
            q2.remove(temp);
        } else {
            while (!q2.isEmpty()) {
                temp = q2.remove();
                q1.add(temp);
            }
            q1.remove(temp);
        }

        return temp;
    }

    /**
     * Get the top element.
     */
    public int top() {
        if (q1.isEmpty() && q2.isEmpty()) {
            return -1;
        }
        int temp = 0;
        if (!q1.isEmpty()) {
            while (!q1.isEmpty()) {
                temp = q1.remove();
                q2.add(temp);
            }
        } else {
            while (!q2.isEmpty()) {
                temp = q2.remove();
                q1.add(temp);
            }

        }
        return temp;
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        if (q1.isEmpty() && q2.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}

3.	01 矩阵  
/*
寻找最短路径,采用BFS来做,不过这个时间消耗是真的多
执行用时:49 ms, 在所有 Java 提交中击败了6.47%的用户
内存消耗:43.3 MB, 在所有 Java 提交中击败了100.00%的用户
*/
class Solution {
   
    public int[][] updateMatrix(int[][] matrix) {
       
        int len = matrix.length, len2 = matrix[0].length;
         int[][] ans = new int[len][len2];
        for (int i =0;i<len;i++){
            for (int j = 0;j<len2; j++){
                ans[i][j]=BFS(matrix,len,len2,i,j);
            }
        }
        return ans;
    }

    private int BFS(int[][] matrix, int len, int len2, int i, int j) {
        int count=0;
        Queue<Integer> queue = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        queue.add(i*len2+j);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int k=0;k<size;k++){
                int temp = queue.remove();
                int a = temp/len2,b = temp%len2;
                if (visited.contains(temp)) continue;
                else visited.add(temp);
                if (matrix[a][b]==0){
                    return count;
                }
                if (a-1>=0) queue.add((a-1)*len2+b);
                if (a+1<len) queue.add((a+1)*len2+b);
                if (b-1>=0) queue.add(a*len2+b-1);
                if (b+1<len2) queue.add(a*len2+b+1);
            }
            count++;
        }
        return count;
    }
}
4.图像渲染
/*
BFS
执行用时:3 ms, 在所有 Java 提交中击败了9.75%的用户
内存消耗:40.7 MB, 在所有 Java 提交中击败了60.00%的用户
*/
class Solution {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int len = image.length,len2 = image[0].length;
        int target = image[sr][sc];
        BFS(image,len,len2,sr,sc,target,newColor);
        return image;
    }

    private void BFS(int[][] image, int len, int len2, int sr, int sc,int target ,int newColor) {
        Queue<Integer> queue = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        queue.add(sr*len2+sc);
        image[sr][sc]=newColor;
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i=0;i<size;i++){
                int temp = queue.poll(),a = temp/len2,b=temp%len2;
                if (visited.contains(temp)) continue;
                else visited.add(temp);image[a][b]=newColor;
                if (a-1>=0&&image[a-1][b]==target) queue.add((a-1)*len2+b);
                if (a+1<len&&image[a+1][b]==target) queue.add((a+1)*len2+b);
                if (b-1>=0&&image[a][b-1]==target) queue.add(a*len2+b-1);
                if (b+1<len2&&image[a][b+1]==target) queue.add(a*len2+b+1);

            }
        }
    }
}


/*
DFS:这里有个大坑点,必须首先判断当前颜色和newColor是否为同一颜色,如果不判断,就会出现反复横跳,导致栈溢出
执行用时:1 ms, 在所有 Java 提交中击败了95.49%的用户
内存消耗:40.5 MB, 在所有 Java 提交中击败了60.00%的用户
*/
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int len = image.length,len2 = image[0].length;
        int target = image[sr][sc];
        //判断当前颜色和newColor是否为同一颜色
        if (target!=newColor){
            DFS(image,len,len2,sr,sc,target,newColor);
        }

        return image;
    }

    private void DFS(int[][] image, int len, int len2, int a, int b, int target, int newColor) {
        image[a][b]=newColor;
        if (a-1>=0&&image[a-1][b]==target)  DFS(image,len,len2,a-1,b,target,newColor);;
        if (a+1<len&&image[a+1][b]==target) DFS(image,len,len2,a+1,b,target,newColor);;
        if (b-1>=0&&image[a][b-1]==target)  DFS(image,len,len2,a,b-1,target,newColor);;
        if (b+1<len2&&image[a][b+1]==target) DFS(image,len,len2,a,b+1,target,newColor);;
    }
}
5.钥匙和房间
/*
BFS:利用count来保存到达的房间数,最后比较总房间数和count是否相等
执行用时:3 ms, 在所有 Java 提交中击败了44.00%的用户
内存消耗:40.1 MB, 在所有 Java 提交中击败了100.00%的用户
*/
class Solution {
    private int count;

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        count = BFS(rooms,0);
        if (count == rooms.size()) {
            return true;
        }
        return false;
    }

    private int BFS(List<List<Integer>> rooms, int i) {
        Queue<Integer> queue  = new LinkedList<>();
        Set<Integer> visited = new HashSet<>();
        queue.add(i);
        while (!queue.isEmpty()){
            int temp = queue.poll();
            if (visited.contains(temp)) continue;
            else visited.add(temp);
            for (int j=0;j<rooms.get(temp).size();j++){
                queue.add(rooms.get(temp).get(j));
            }
            count++;
        }
        return count;
    }

}
6.字符串解码
搞了半天,搞混了,以后再看看

你可能感兴趣的:(leetcode-探索(队列+栈)小结)