随笔分类 -  Dynamic Programming

摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" 阅读全文
posted @ 2014-02-20 16:06 Razer.Lu 阅读(198) 评论(0) 推荐(0)
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo 阅读全文
posted @ 2014-02-19 02:18 Razer.Lu 阅读(344) 评论(0) 推荐(0)
摘要:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文
posted @ 2014-02-17 14:46 Razer.Lu 阅读(277) 评论(0) 推荐(0)
摘要:Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring.参考:水中的鱼: [LeetCode]Longest Palindromic Substring解题报告定义函数P[i,j] = 字符串区间[i,j]是否为palindrome.首先找个例子,比如S="abccb", S= a b c c 阅读全文
posted @ 2014-02-08 03:32 Razer.Lu 阅读(297) 评论(0) 推荐(0)
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.二维DP。设数组A[row][col],Min[i][j] = min(Min[i-1][j], Min[i][j-1]) +A[i][j];注意初始条件即可。public class 阅读全文
posted @ 2014-02-07 03:33 Razer.Lu 阅读(164) 评论(0) 推荐(0)
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文
posted @ 2014-02-07 02:49 Razer.Lu 阅读(162) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse. 1 阅读全文
posted @ 2014-02-07 01:20 Razer.Lu 阅读(275) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.click to show more practice.More practice:If you have figured out the O(n) solution, try 阅读全文
posted @ 2014-02-06 03:40 Razer.Lu 阅读(535) 评论(0) 推荐(0)
摘要:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
posted @ 2014-02-05 02:29 Razer.Lu 阅读(293) 评论(0) 推荐(0)
摘要:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文
posted @ 2014-02-04 16:32 Razer.Lu 阅读(254) 评论(0) 推荐(0)
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs.For example, givens="aab",Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.[Thoughts]凡是求 阅读全文
posted @ 2014-02-04 15:06 Razer.Lu 阅读(232) 评论(0) 推荐(0)
摘要:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character正确的解出来方法是用二维的dp。假如我们要将字符串str1变成str2,sstr1(i)是s 阅读全文
posted @ 2014-01-31 02:56 Razer.Lu 阅读(282) 评论(0) 推荐(0)
摘要:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.[解题思路]分析题目给定3个字符串(s1,s2,s3),看s3是否是有s1和s2通过交织可以得到。可以这么来看这个问题,每次从s3开头拿出来一个字符,如果s1的开头或者 阅读全文
posted @ 2014-01-28 03:28 Razer.Lu 阅读(326) 评论(0) 推荐(0)
摘要:You are climbing a stair case. It takesnsteps 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?public class Solution { public int climbStairs(int n) { int[] level = new int[n+1]; level[0] = 0; level[1] = 1; ... 阅读全文
posted @ 2014-01-24 16:56 Razer.Lu 阅读(119) 评论(0) 推荐(0)