文章目录
session Ⅰ Algorithm
problem Ⅰ
784. Letter Case Permutation
Given a string s, you can transform every letter individually to be lowercase or uppercase to create another string.
Return a list of all possible strings we could create. Return the output in any order.
Example 1:
Input: s = “a1b2”
Output: [“a1b2”,“a1B2”,“A1b2”,“A1B2”]
Example 2:
Input: s = “3z4”
Output: [“3z4”,“3Z4”]
my solution 1
class Solution {
public:
vector<string> ans;
void Backtrack(string curr, string S, int i){
if(i==S.size()){
ans.push_back(curr);
return;
}
if(!isalpha(S[i])){
curr.push_back(S[i]);
Backtrack(curr, S, i+1);
}
else{
curr.push_back(tolower(S[i]));
Backtrack(curr, S, i+1);
curr.pop_back();
curr.push_back(toupper(S[i]));
Backtrack(curr, S, i+1);
curr.pop_back();
}
}
vector<string> letterCasePermutation(string S){
Backtrack("", S, 0);
return ans;
}
};
NOTE:
problem Ⅱ
70. Climbing Stairs
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
- 1 step + 1 step
- 2 steps
Example 2:
Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
- 1 step + 1 step + 1 step
- 1 step + 2 steps
- 2 steps + 1 step
my solution ⅠTime Limit Exceeded
class Solution {
public:
int climbStairs(int n) {
if(n==1)return 1;
if(n==2)return 2;
return climbStairs(n-1)+climbStairs(n-2);
}
};
my solution Ⅱ Top down DP
class Solution {
public:
vector<int> t = vector<int>(46,0);
int climbStairs(int n) {
if(n==1)return t[1]=1;
if(n==2)return t[2]=2;
if(t[n] != 0)return t[n];
t[n] = climbStairs(n-1) + climbStairs(n-2);
return t[n];
}
};
my solution Ⅲ Bottom up DP
class Solution {
public:
vector<int> t = vector<int>(46,0);
int climbStairs(int n) {
if(n==1)return 1;
if(n==2)return 2;
t[1]=1, t[2]=2;
for(int i=3; i<=n; i++){
t[i] = t[i-1] + t[i-2];
}
return t[n];
}
};
problem Ⅲ
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.
my solution Ⅰ with DP-array
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==1)return nums[0];
vector<int> sums(nums.size(), 0);
sums[0]=nums[0];
sums[1]=max(nums[0], nums[1]);
for(int i=2; i<nums.size(); i++)
sums[i] = max(nums[i]+sums[i-2], sums[i-1]);
return sums[nums.size()-1];
}
};
my solution Ⅱwithout DP-array
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==1)return nums[0];
int tmp0=nums[0], tmp1=max(nums[0], nums[1]), t;
for(int i=2; i<nums.size(); i++){
t = tmp0;
tmp0 = tmp1;
tmp1 = max(nums[i]+t, tmp1);
}
return tmp1;
}
};
problem Ⅳ
120. Triangle
Given a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
Example 1:
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:
Input: triangle = [[-10]]
Output: -10
my solution Bottom up DP
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];
}
};