zoukankan      html  css  js  c++  java
  • 牛客网刷题笔记(三)编程基础

    题目一:二维数组中的查找

    题目描述:

    在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    解题思路:参考剑指0ffer

    首先选数组中右上角的数字,如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数,剔除这个数字所在的列;如果该数字小于要查找的数,,剔除这个数字所在的行;这样每一步可以缩小查找的范围,直到查找到要查找的数字,或者查找范围为空。

    程序代码:

    class Solution:
        def Find(self, target, array):
            xend = len(array)-1
            yend = len(array[0])-1
            x = 0
            while x <= xend and yend >= 0:
                if array[x][yend] == target:
                    return True
                elif array[x][yend] > target:
                    yend -= 1
                else:
                    x += 1
            return False

    题目二:井字棋

    题目描述:

    对于一个给定的井字棋棋盘,请设计一个高效算法判断当前玩家是否获胜。

    给定一个二维数组board,代表当前棋盘,其中元素为1的代表是当前玩家的棋子,为0表示没有棋子,为-1代表是对方玩家的棋子。

    解题思路:

    检查行、列或者对角线的三个元素求和是否为3。(井字棋类似于五子棋,但是只要三个相同的棋子相连即可)此题中要求判断当前玩家是否获胜。

    程序代码:

    class Board {
    public:
        bool checkWon(vector<vector<int> > board) {
            if (board[0][0] + board[1][1] + board[2][2] == 3 )
                return true;
            if (board[0][2] + board[1][1] + board[2][0] == 3 )
                return true;
            for(int i = 0;i < 3;i++){
                if (board[i][0] + board[i][1] + board[i][2] == 3 )
                    return true;
                if (board[0][i] + board[1][i] + board[2][i] == 3 )
                    return true;
            }
            return false;   
        }
    }; 

    题目三:best-time-to-buy-and-sell-stock-ii

    题目描述:

    有一个长度为n的数组,其中第i个元素代表第i天股票的价格。

    设计一个算法找出最大收益,交易次数不限。但是一次不能投入多期交易,即购买之前要赎回。

    解题思路:

    判断相邻是否递增,将连续递增作为一次买卖,统计所有的递增量即所求结果。

    程序代码:

    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int n =  prices.size();
            vector<int> profit(n,0);
            int maxprofit = 0;
            for (int i = 1;i < n;i++){
                if(prices[i] > prices[i-1]){
                    profit[i] = prices[i] - prices[i-1];
                    maxprofit += profit[i];
                }
            }
            return maxprofit;
        }
    };

    题目四:best-time-to-buy-and-sell-stock

    题目描述:

    有一个长度为n的数组,其中第i个元素代表第i天股票的价格。

    设计一个算法找出最大收益,只能进行一次交易。

    解题思路:

    与上一道题对比,此题中需要定义一个变量来记录最小值。

    程序代码:

    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int n =  prices.size();
            //vector<int> profit(n,0);
            int maxprofit = 0;
            int min = prices[0];
            for (int i = 1;i < n;i++){
                if(prices[i] > min)
                    maxprofit = max(maxprofit,prices[i]-min);
                else
                    min = prices[i];
            }
            return maxprofit;
        }
    };

    题目五: triangle

    题目描述:

    给定一个三角形,找到从顶到底的最小路径长度。每一步都移动到下一行的相邻数字。

    解题思路:

    每一步找到下一行相邻两个数字中的最小值叠加即可。

    程序代码:

    class Solution {
    public:
        int minimumTotal(vector<vector<int> > &triangle) {
            for (int i = triangle.size()-2;i >= 0;--i){
                for (int j = 0;j < triangle[i].size();++j){
                    triangle[i][j] += min(triangle[i+1][j],triangle[i+1][j+1]);
                }
            }
            return triangle[0][0];
        }
    };

    题目六:pascals-triangle-ii

    题目描述:

    给定一个索引值,返回杨辉三角的第k行。

    程序代码:

    class Solution {
    public:
        vector<int> getRow(int rowIndex) {
            vector<int> res(rowIndex + 1,0);
            res[0] = 1;
            for (int i = 1;i <= rowIndex; ++i){
                for (int j = i - 1;j >= 1; --j){
                    res[j] += res[j - 1];
                }
                res[i] = 1;
            }
            return res;
        }
    };

    题目七:加法运算替代

    题目描述:

    请编写一个方法,实现整数的乘法、减法和除法运算(这里的除指整除)。只允许使用加号。

    给定两个正整数int a,int b,同时给定一个int type代表运算的类型,1为求a * b,0为求a / b,-1为求a - b。请返回计算的结果,保证数据合法且结果一定在int范围内。

    解题思路:

    分情况讨论:

    (1)乘法:转为加法;

    ( a * b) 相当于 a 个 b 相加;

    (2)除法:转为乘法;

    令 a / b = x;则 a = b * x

    (3)减法:转为加法;

    令 a - b = x;则 a = b + x;若a < b ,则a + x = b ;

    程序代码:

    class AddSubstitution {
    public:
        int calc(int a, int b, int type) {
            // write code here
            if(type == 1){
                int res = a;
                for(int i = 1;i < b;i++)
                    a += res;
            }
            if(type == 0){
                int res = 0;
                int temp = b;
                for(;;res++){
                    if(a < b)
                        break;
                    b += temp;
                }
                a = res;
            }
            if(type == -1){
                if(a >= b){
                    for(int i = 0;;i++){
                        if(b + i == a){
                            a = i;
                            break;
                        }
                    }
                }
                else {
                   for(int i=0;;i--){
                       if(b + i == a){
                           a = i;
                           break;
                       }
                   }
                }
            }
            return a;
        }
    };

     

  • 相关阅读:
    用于视频行为识别的双流卷积网络
    python更改默认版本
    ubuntu16安装ROS(包括win10子系统ubuntu同样能用)
    js中的new做了什么?
    Promise是什么?
    滚动轴 scroll
    什么是ES6?
    什么是token及怎样生成token
    mongodb 删库跑路
    css如何设置背景图片?background属性添加背景图片
  • 原文地址:https://www.cnblogs.com/ST-2017/p/10556018.html
Copyright © 2011-2022 走看看