牛客網刷題筆記(三)編程基礎
題目一:二維數組中的查找
題目描述:
在一個二維數組中(每個一維數組的長度相同),每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數,輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。
解題思路:參考劍指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 returnFalse
題目二:井字棋
題目描述:
對於一個給定的井字棋棋盤,請設計一個高效算法判斷當前玩家是否獲勝。
給定一個二維數組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; } };
牛客網刷題筆記(三)編程基礎