LeetCode_70、300、303三题(动态规划)

LeetCode_70. Climbing Stairs(基础题)
原题:You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

解析:很简单的一道动态规划题,练练手,a[i] = a[i-1] + a[i-2],因为可以从下面二楼一步跳上来,或者从下面一楼一步跳上来。但是在第一次提交的时候出错了,原因是a[2]=2,我以为a[2]=1,因为我觉得只能从1楼一步跳上来,没想到还有第0层,需要注意这方面陷阱。
具体代码如下:

class Solution {
public:
    int climbStairs(int n) {
        if(n == 0)return 0;
        if(n == 1)return 1;
        int a[100000];
        a[0] = 1;
        a[1] = 1;
        for(int i = 2;i <= n;i ++){
            a[i] = a[i-1] + a[i-2];
        }
        return a[n];
    }
};

LeetCode_300. Longest Increasing Subsequence
原题:Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

解析:最长字串问题,但与之前遇到的最长字串不同,不同在于它可以不连续,这表明时间复杂度一定大于O(n),因为动态规划来做的话,对每个元素都应该至少循环一遍它之前的元素(还没有找到O(n log n)的好方法,只能用最基本的方法做了),找出前后关系式:len[i] = max(len[i],len[j] + 1),若以第i个元素结尾的最长字串长度=它已知的最长字串长度或者是前一个比它小的元素长度+1。此外还有一个疑惑的地方,我一开始将递增理解为1、2、2、2、3这样也可以,事实上不能有相等元素,但数学上对这两者的区别有明显定义:递增和严格递增,在这里应该是严格递增。具体代码如下:

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        vector<int> len(nums.size());
        int max1 = 0;
        for(int i = 0;i < nums.size();i++){
            len[i] = 1;
            for(int j = 0; j < i;j++){
                if(nums[j] < nums[i])len[i] = max(len[i],len[j] + 1);
            }
            max1 = max(max1,len[i]);
        }
        return max1;
    }
};

LeetCode_303. Range Sum Query - Immutable
原题:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3

解析:乍一看是一题很简单的题,但通过率23W次只有6W6通过,明显有坑,因此将所有可能发生的错误情况都详细列出来,比如,1、超时(每次都对i,j之间循环加一次),为了克服这点,一次性在加载数组的时候将所有的到第i个元素的和统统求出来,到时候用sum[j]-sum[i-1]即可。2、i比j大,则不存在中间元素,理论上值也不该等于0,所以排除这种情况。3、i<0,j>0,这两种情况应该将i、j改成边界。4、i-1<0

具体代码如下:

class NumArray {
public:
    NumArray(vector<int> nums) {
        if(nums.empty())return;
        size = nums.size();
        sum = new int[size];
        sum[0] = nums[0];
        for(int i = 1;i < size;i++){
            sum[i] = sum[i-1] + nums[i];
        }
    }

    int sumRange(int i, int j) {
        if(i < 0)i = 0;
        if(j > size - 1)j = size - 1;
        if(i == 0)return sum[j];
        return sum[j]-sum[i-1];
    }

private:
    int *sum;
    int size;
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值