添加C++、计算机网络、音视频、Leetcode118、119、122

This commit is contained in:
huihut
2018-02-28 17:36:34 +08:00
parent 72327ba94b
commit b763595484
7 changed files with 247 additions and 3 deletions

View File

@@ -0,0 +1,21 @@
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> triangle;
for(int i = 0; i < numRows; i++){
vector<int> v;
if(i==0){
v.push_back(1);
}
else{
v.push_back(1);
for(int j = 0; j < triangle[i-1].size()-1; j++){
v.push_back(triangle[i-1][j] + triangle[i-1][j+1]);
}
v.push_back(1);
}
triangle.push_back(v);
}
return triangle;
}
};

View File

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

View File

@@ -0,0 +1,26 @@
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max=0, begin=0, end=0;
bool up=false, down=false;
for (int i=1; i<prices.size(); i++) {
if (prices[i] > prices[i-1] && up==false){ // goes up
begin = i-1;
up = true;
down = false;
}
if (prices[i] < prices[i-1] && down==false) { // goes down
end = i-1;
down = true;
up = false;
max += (prices[end] - prices[begin]);
}
}
// edge case
if (begin < prices.size() && up==true){
end = prices.size() - 1;
max += (prices[end] - prices[begin]);
}
return max;
}
};