• 《剑指offer》递推与循环,栈和队列,回溯 (牛客10.27)


    难度 题目 知识点
    07. 斐波那契数列 递推递归 - 两变量写法-
    08. 跳台阶 同上
    09. 变态跳台阶 dp
    10. 矩形覆盖 同上
    05. 用两个栈实现队列 模拟
    20. 包含min函数的栈
    21. 栈的压入弹出序列 模拟出栈序列
    65. 矩阵中的路径 回溯
    66. 机器人的运动范围 dfs 求连通块大小

    07 - 10 斐波那契数列 - 递推递归 - 两变量写法

    07. 斐波那契数列

    T7:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39。

    class Solution {
    public:
        int Fibonacci(int n) {
            if(n==0) return 0;
            if(n==1) return 1;
            if(n==2) return 1;
            return Fibonacci(n-1)+Fibonacci(n-2);
        }
    };
    

    08. 跳台阶

    T8:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

    // 两变量
    class Solution {
    public:
        int jumpFloor(int number) {
            if(number==0)return 1;
            if(number==1)return 1;
            if(number==2)return 2;
            int f=1,g=2;
            number-=2;
            while(number--){
                g=f+g;
                f=g-f;
            }
            return g;
        }
    };
    

    09. 变态跳台阶

    T9:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

    // dp
    class Solution {
    public:
        int jumpFloorII(const int number) {
            int** dp=new int*[number+10];
            for(int i=0;i<number+10;i++){
                dp[i]=new int[number+10];
            }
            
            memset(dp,0,sizeof dp);
            
            for(int i=1;i<=number;i++){
                dp[1][i]=1;
            }
            // dp[i][j] 用i步跳上台阶j
            for(int i=2;i<=number;i++){
                for(int j=i;j<=number;j++){
                    for(int k=i-1;k<j;k++){
                        dp[i][j]+=dp[i-1][k];
                    }
                }
            }
            
            int ans=0;
            for(int i=1;i<=number;i++){
                ans+=dp[i][number];  
            }
            return ans;// 返回的变量打错,不可原谅,,
        }
    };
    

    10. 矩形覆盖

    T10:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

    类似于前面几题。

    class Solution {
    public:
        int rectCover(int number) {
            if(number==0)    return 0;
            if(number==1)    return 1;
            if(number==2)    return 2;
            int f=1,g=2;
            number-=2;
            while(number--){
                g=f+g;
                f=g-f;
            }
            return g;
        }
    };
    

    05. 用两个栈实现队列

    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

    栈1接收入队列元素,栈2存储出队列元素,当栈2空时,把栈1元素倒到栈2中。

    class Solution
    {
    public:
        void push(int node) {
            stack1.push(node);
        }
    
        int pop() {
            if(stack2.size()==0){
                while(!stack1.empty()){
                    int x=stack1.top();
                    stack1.pop();
                    stack2.push(x);
                }
            }
            int x=stack2.top();
            stack2.pop();
            return x;
        }
    
    private:
        stack<int> stack1;
        stack<int> stack2;
    };
    

    20. 包含min函数的栈

    题目描述

    定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

    每次对压入一对元素(当前元素和目前的最小值)。

    Java Code
    
    import java.util.Stack;
    public class Solution {
        private Stack st=new Stack<>();
        public void push(int node) {
            int min=node;
            if(!st.empty()) min=Math.min(min,st.peek());
            st.push(node);
            st.push(min);
        }
        public void pop() {
            st.pop();
            st.pop();
        }
        public int top() {
            int x=st.peek();
            st.pop();
            int y=st.peek();
            st.push(x);
            return y;
        }
        public int min() {
            return st.peek();
        }
    }
        

    更省空间的做法如下:
    应用一个辅助栈,压的时候,如果A栈的压入比B栈压入大,B栈不压,,,,小于等于,AB栈同时压入,出栈,如果,AB栈顶元素不等,A出,B不出。

    21. 栈的压入弹出序列

    模拟

    题目描述

    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

    import java.util.ArrayList;
    import java.util.Stack;
    public class Solution {
        public boolean IsPopOrder(int [] pushA,int [] popA) {
            if(pushA.length == 0 || popA.length == 0)
                return false;
            Stack<Integer> s = new Stack<Integer>();
            //用于标识弹出序列的位置
            int popIndex = 0;
            for(int i = 0; i< pushA.length;i++){
                s.push(pushA[i]);
                //如果栈不为空,且栈顶元素等于弹出序列
                while(!s.empty() &&s.peek() == popA[popIndex]){
                    //出栈
                    s.pop();
                    //弹出序列向后一位
                    popIndex++;
                }
            }
            return s.empty();
        }
    }
    

    65. 矩阵中的路径

    回溯

    题目描述

    请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s d e e 矩阵中包含一条字符串"bcced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

    dfs回溯。

    Java Code
    
    public class Solution {
        private int rows, cols;
        char[][] G;
        char[] str;
        boolean[][] vis;
        int[] dx = new int[]{1, 0, -1, 0};
        int[] dy = new int[]{0, 1, 0, -1};
        public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
            if (matrix == null || matrix.length < str.length)
                return false;
            this.str = str;
            this.rows = rows;
            this.cols = cols;
            G = new char[rows][cols];
            vis = new boolean[rows][cols];
            for (int i = 0; i < matrix.length; i++) {
                G[i / cols][i % cols] = matrix[i];
            }
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    if (G[i][j] == str[0]) {
                        if (str.length == 1) return true;
                        vis[i][j] = true;
                        for (int k = 0; k < 4; k++) {
                            if (check(i + dx[k], j + dy[k])
                                    && dfs(i + dx[k], j + dy[k], 1))
                                return true;
                        }
                        vis[i][j] = false;
                    }
                }
            }
            return false;
        }
        private boolean dfs(int x, int y, int idx) {
            if (G[x][y] != str[idx]) return false;
            if (idx == str.length - 1) return true;
            vis[x][y] = true;
            for (int k = 0; k < 4; k++) {
                if (check(x + dx[k], y + dy[k])
                        && dfs(x + dx[k], y + dy[k], idx + 1))
                    return true;
            }
            vis[x][y] = false;
            return false;
        }
        private boolean check(int x, int y) {
            return x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y];
        }
    }
        

    66. 机器人的运动范围

    dfs求连通块大小

    题目描述
    地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?

    dfs求连通块大小。注意要用vis数组避免重复计数。

    Java Code
    
    public class Solution {
        int cnt=0;
        boolean [][]vis;
        public int movingCount(int threshold, int rows, int cols)
        {
            vis=new boolean[rows][cols];
            dfs(0,0,rows,cols,threshold);
            return cnt;
        }
        private void dfs(int x,int y,int rows,int cols,int thrsh){
            if(!check(x,y,rows,cols,thrsh))  return;
            cnt++;
            vis[x][y]=true;
            dfs(x,y+1,rows,cols,thrsh);
            dfs(x+1,y,rows,cols,thrsh);
            dfs(x,y-1,rows,cols,thrsh);
            dfs(x-1,y,rows,cols,thrsh);
        }
        private boolean check(int x,int y,int rows,int cols,int thrsh){
            if(x<0||x>=rows||y<0||y>=cols||vis[x][y])    return false;
            int sum=0;
            while(x>0){ sum+=x%10;x/=10; }
            while(y>0){ sum+=y%10;y/=10; }
            return sum<=thrsh;
        }
    }
        
  • 相关阅读:
    session的一个问题
    (Struts)ActionForm类及表单数据验证
    javaee添加验证码
    struts的上传和下载
    logic标签用法
    jstl标签用法
    bean标签
    /WEB-INF/userManage.jsp(31,82) Unterminated ${ tag
    自定义EL
    jstl
  • 原文地址:https://www.cnblogs.com/weseewe/p/11750123.html
Copyright © 2020-2023  润新知