(4) the use of stacks and queues of data structures and algorithms

Stack of applications

Since the stack having "last in, first out" inherent characteristics;

  1. Parenthesis matching;
    as leetcode on one question: check the correctness of parentheses:

    (){}->true

    []]->false

    [(])->false

    [()]->true

java code as follows

class Solution {
    public boolean isValid(String s) {
        char[] chars = s.toCharArray();
        ArrayList<Character> data = new ArrayList<>();
        for(int j = chars.length-1;j>=0;j--){
            if(chars[j]==41||chars[j]==93||chars[j]==125){
                data.add(chars[j]);
            }else if(chars[j]==40||chars[j]==91||chars[j]==123){
                try{
                    char c = data.get(data.size()-1);
                    if(c==41){
                        if(c==chars[j]+1){
                            data.remove(data.size()-1);
                            continue;
                        }
                    }else{
                        if(c==chars[j]+2){
                            data.remove(data.size()-1);
                            continue;
                        }
                    }
                return false;
                }catch(Exception e){
                    return false;
                }
            }else return false;
        }
        if(data.size()==0) return true;
        return false;
    }
}
  1. Implementing stacks and recursive calls

    Recursion: a function (method) call itself directly or indirectly;

    In order to dead cycle is not recursive, so the effective-containing recursive termination condition;

    Contact java virtual machine stack, heap, the method area of ​​the memory structure, understandable push, playing stack, stack overflow and other terms;

Queue application

  1. Apply the queue in the hierarchy traversal

    Layer by layer or progressive process, this solution to the problem is often the column processing of the current layer in the current row or to the next layer or preprocessing of the next row, the order of processing is arranged, a current layer to be processed or the current line, or one can handle the next row. Use queue processing procedure in order to save the next step ;

    Such as breadth-first search (BFS):

    Given consisting of '1'(land), and '0'(water) consisting of two-dimensional grid, the number of islands of computing. An island surrounded by water, and it is through the horizontal or vertical direction is connected to each adjacent land. You can assume that the water surrounding the four sides of the grid are.

    输入:
    11110
    11010
    11000
    00000
    
    输出: 1
    
    输入:
    11000
    11000
    00100
    00011
    
    输出: 3
    
    class Solution {
      public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
          return 0;
        }
    
        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;
    
        for (int r = 0; r < nr; ++r) {
          for (int c = 0; c < nc; ++c) {
            if (grid[r][c] == '1') {
              ++num_islands;
              grid[r][c] = '0'; // mark as visited
              Queue<Integer> link = new LinkedList<>();
              link.add(r * nc + c);
              while (!link.isEmpty()) {
                int id = link.remove();
                int row = id / nc;
                int col = id % nc;
                if (row - 1 >= 0 && grid[row-1][col] == '1') {
                  link.add((row-1) * nc + col);
                  grid[row-1][col] = '0';
                }
                if (row + 1 < nr && grid[row+1][col] == '1') {
                  link.add((row+1) * nc + col);
                  grid[row+1][col] = '0';
                }
                if (col - 1 >= 0 && grid[row][col-1] == '1') {
                  link.add(row * nc + col-1);
                  grid[row][col-1] = '0';
                }
                if (col + 1 < nc && grid[row][col+1] == '1') {
                  link.add(row * nc + col+1);
                  grid[row][col+1] = '0';
                }
              }
            }
          }
        }
    
        return num_islands;
      }
    }
    
  2. Application queues in computing and engineering

    1. Problem-solving speed between the host and the external device do not match

      Such as: the host and the printer does not match the speed, - "buffer zone, the buffer, the data ready in advance;

    2. Resolve resource contention issues caused by multiple users;

      cpu resource competition, a plurality of users need to run their own programs cpu, cpu occupancy of each request, each request is typically the operating system in time sequence, arranged in a queue, the respective ends of a predetermined program to operate or run after a time interval, so that it is out of the column;

Published 17 original articles · won praise 0 · Views 370

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/104025180