0
点赞
收藏
分享

微信扫一扫

力扣第十四天

晴儿成长记 2022-01-31 阅读 31

文章目录

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:

Example 2:

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:

  1. 1 step + 1 step
  2. 2 steps

Example 2:

  1. 1 step + 1 step + 1 step
  2. 1 step + 2 steps
  3. 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:

Example 2:

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:

Example 2:

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];
    }
};

在这里插入图片描述

举报

相关推荐

HCIP第十四天

第十四天bj

第十四天xss漏洞

HCIP第十四天笔记

学习java 第十四天

第十四天155. 最小栈

0 条评论