62. 不同路径
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > dp(m, vector<int>(n, 1));
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
}
}
return dp[m - 1][n - 1];
}
};
70. 爬楼梯
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1);
dp[0] = 1,dp[1] = 1;
for(int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
};
78. 子集
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int n = nums.size();
vector<int> se;
vector<vector<int>> ans;
for (int i = 0; i < (1 << n); ++i)
{
se.clear();
for (int j = 0; j < n; ++j)
{
if (((1 << j) & i) != 0)
{
se.push_back(nums[j]);
}
}
ans.push_back(se);
}
return ans;
}
};