Problem 118. Pascal’s Triangle
-
题目描述
Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it.
For example,
Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]
-
解题思路
这是一道杨辉三角题,每一行满足以下规律:
- 首元素和末元素均为1;
- 设cur为当前行,v为上一行,则满足 cur[i] = v[i] + v[i-1];
- 代码实现
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > res;
if(numRows == 0)
return res;
vector<int> v = {1};
res.push_back(v);
for(int i = 1; i < numRows; ++i){
v.push_back(0);
vector<int> cur = v;
for(int j = 1; j <= i; ++j)
cur[j] = v[j-1] + v[j];
res.push_back(cur);
v = cur;
}
return res;
}
};
Problem 121. Best Time to Buy and Sell Stock
-
题目描述
Say you have an array for which the i element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example
Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
-
解题思路
要求最大收益,只要对数组 prices 进行一次遍历,令 _min 为当前访问过的元素中的最小值, prices[i] - _min 为当前利润,每次对当前利润求最大值即可。 -
代码实现
class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size() == 0) return 0; int res = 0, _min = prices[0]; for(int i = 1; i < prices.size(); ++i){ _min = min(prices[i], _min); res = max(res, prices[i]-_min); } return res; } };
Problem 122. Best Time to Buy and Sell Stock II
-
题目描述
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
-
解题思路
与上题相比,本题可以多次进行 buy 和 sell,即求累积最大利润 res。对 prices 从后向前进行遍历,如果 prices[i] > prices[i-1],表明在 i-1 和 i 这段时间内可以获得利润为 prices[i] - prices[i-1],将其累计到 res 中。 -
代码实现
class Solution {
public:
int maxProfit(vector<int>& prices) {
int len = prices.size();
if(len == 0)
return 0;
int res = 0;
for(int i = len-1; i >0; --i){
if(prices[i] > prices[i-1])
res += prices[i] - prices[i-1];
}
return res;
}
};
Problem 125. Valid Palindrome
-
题目描述
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
-
解题思路
本题要判断一个输入的字符串是否是回文串(忽略字符串中的非字母和数字,同时不考虑字母的大小写)。思路是定义下标 left 和 right,分别指向字符串 s 的左边和右边的有效元素(字母或数字),并判断 s[left] 和 s[right] 是否相等,若不相等直接返回 false,相等则 left 和 right 下标继续向里移动。需要注意的是,在对字符串s进行处理的时候,不能申请额外的空间储存新的字符串,否则会造成 Memory Limit Exceeded。
-
代码实现
class Solution {
public:
bool isPalindrome(string s) {
int len = s.length();
if(len == 0 || len == 1)
return true;
int left = 0, right = len-1;
while(left < right){
while(!(isalnum(s[left])) && left < right)
left++;
while(!(isalnum(s[right])) && left < right)
right--;
if(tolower(s[left++]) != tolower(s[right--]))
return false;
}
return true;
}
};