ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

牛客网刷题笔记(三)编程基础

2019-03-18 23:39:19  阅读:468  来源: 互联网

标签:return int res 编程 牛客 board 网刷题 prices 题目


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

题目描述:

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

解题思路:参考剑指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;
    }
};

 

标签:return,int,res,编程,牛客,board,网刷题,prices,题目
来源: https://www.cnblogs.com/ST-2017/p/10556018.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有