303. Range Sum Query - Immutable

解题思路:

Note里说sumRange会被调用很多次。。所以简直强烈暗示要做cache啊。。。所以刚开始,虽然用每次都去遍历数组求和的方式可以

AC,但是真的耗时太长了。因此,考虑不存储数组nums,而是在array[i+1]处存前i项的和。这样的话,求i和j之间的和,只需要用

array[j+1]-array[i]即可以求得了。这样是直接访问数组,会快很多。

class NumArray {
public:
NumArray(vector<int> nums) {
// default is 0
array = new int[nums.length() + 1];
for (int i = 0; i < nums.length(); i++) {
array[i + 1] = array[i] + nums[i];
}
} int sumRange(int i, int j) {
return array[j + 1] - array[i];
}
private:
// Notice
int[] array;
};

70. Climbing Stairs

解题思路:

类似汉诺塔问题。。

int climbStairs(int n) {
if (n <= 2)
return n;
// from n-1 to n
int oneBefore = 2;
int twoBefore = 1;
int sum = 0;
for (int i = 2; i < n; i++) {
sum = oneBefore + twoBefore;
twoBefore = oneBefore;
oneBefore = sum;
}
return sum;
}

198. House Robber

解题思路:

由题目可知,不能抢劫相邻的两家,所以可以考虑单数线和双数线。

int Max(int a, int b) {
return a > b ? a : b;
}
int rob(vector<int>& nums) {
int even = 0;
int odd = 0;
for(int i = 0; i < nums.size(); i++) {
if (i % 2 == 0)
// if odd is bigger, nums[i-1] is chosen and nums[i] isn't chosen
// otherwise, nums[i] is chosen.
even = Max(even + nums[i], odd);
else
odd = Max(even, odd + nums[i]);
}
// return max of two lines
return Max(even, odd);
}

300. Longest Increasing Subsequence

解题思路:

arr[i]存的是以nums[i]结尾的最长递增子序列的长度,所以计算时:

arr[i] = max(arr[j]+1) 其中, nums[j] < nums[i], j = 0...i-1

写的时候,注意max在内层循环结束后要更新。。。最终只要得到arr[]中的最大值就是结果。

int lengthOfLIS(vector<int>& nums) {
if (nums.size() <= 1)
return nums.size();
int arr[nums.size()];
arr[0] = 1;
int i, j;
int max = 1;
for (i = 0; i < nums.size(); i++) {
for (j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
if (arr[j] + 1 > max)
max = arr[j] + 1;
}
}
arr[i] = max;
max = 1;
}
int result = arr[0];
for (i = 1; i < nums.size(); i++)
if (arr[i] > result)
result =arr[i];
return result;
}

72. Edit Distance

解题思路:

听老师讲完编辑距离那块,做这个就很容易了。思路是用一个数组edit[i][j]存储word1[1..i]变到word2[1..j]的编辑距离。数组的递推公式为:

解释:前两行是空串到非空串的情况;第三行是i,j > 0时,前两种是增加一个字母,最后一种是替换或不替换(word1[i]与word[j]相同,所以

此处要保持编辑距离)

实际写的时候,注意字符串的下标从0开始,所以diff(i-1, j-1)

int Min(int a, int b, int c) {
if (a <= b && a <= c)
return a;
if (b <= a && b <= c)
return b;
if (c <= a && c <= b)
return c;
}
int minDistance(string word1, string word2) {
int len1 = word1.length();
int len2 = word2.length();
if (len1 == 0)
return len2;
if (len2 == 0)
return len1;
int edit[len1+1][len2+1];
for (int i = 0; i <= len1; i++)
edit[i][0] = i;
for (int i = 0; i <= len2; i++)
edit[0][i] = i;
int diff;
int i, j;
for (i = 1; i <= len1; i++) {
for (j = 1; j <= len2; j++) {
if (word1[i-1] == word2[j-1])
diff = 0;
else
diff = 1;
edit[i][j] = Min(edit[i-1][j]+1, edit[i][j-1]+1, diff+edit[i-1][j-1]);
}
}
return edit[len1][len2];
}

leetcode-20-Dynamic Programming的更多相关文章

  1. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  4. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  8. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

  10. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

随机推荐

  1. ASPECTJ 注解。。。

    public interface ISomeService { public void doSome(); public String dade(); } public class SomeServi ...

  2. WebStorm快捷键(Mac版)

    编辑 Command+alt+T 用 (if..else, try..catch, for, etc.)包住 Command+/ 注释/取消注释的行注释 Command+alt+/ 注释/取消注释与块 ...

  3. sql常用操作(三)多表查询

    1 连接查询 1.1连接就是指两个或2个以上的表(数据源)“连接起来成为一个数据源”. 实际上,两个表的完全的连接是这样的一个过程: 左边的表的每一行,跟右边的表的每一行,两两互相“横向对接”后所得到 ...

  4. ajax在购物车中的应用

    代码如下: 购物车页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  5. JavaScript笔记3--标识符和保留字

    1.标识符 javaScript标识符必须以字母,下划线(_)或美元符($)开始.后续的字符可以是字母/数字/下划线/美元符.也可以使用非英语语言或数学符号来书写标识符; 2.保留字 break/de ...

  6. 评价PE基金绩效的常用指标

    作为信息系统,辅助管理层决策是重要的功能之一.前文介绍了PE基金管理系统的建设,对PE业务的运转有了一些了解,但没有介绍如何评价PE基金的绩效,而这是管理层作出重大决策的主要依据之一.PE基金本质也是 ...

  7. BUG数量和项目成本

    这篇文章,不是讨论怎么提升程序员的能力避免BUG,因为程序员的能力不足造成的BUG,短期是无法避免的.这里主要探讨的是因为程序员疏忽大意和不良的开发习惯,产生的低级BUG,对项目成本影响. 首先了解下 ...

  8. Python中的绝对路径和相对路径

    大牛们应该对路径都很了解了,这篇文章主要给像我这样的入门小白普及常识用的,啊哈 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中打开文件的时候经常见到下面其中路径的表达 ...

  9. 转载 - 浅析我曾遇到的几个便宜VPS服务器

    本文来自:http://www.jianshu.com/p/7d8cfa87fa32 有些时候可能并不是我们工作和业务的需要,但是网上就是这么的邪门,如果看到便宜的衣服不去购买深怕自己吃亏.所以每年的 ...

  10. BandwagonHost 5个数据中心/机房Ping速度测试亲自体验

    我们选择Bandwagonhost服务器的原因之一在于有5个数据中心,而且与众多其他VPS不同之处在于可以自己后台切换机房和IP,这样我们 在遇到不满意的速度时候,可以自己切换其他机房更换,而且对于有 ...