随笔分类 -  leetcode-DP

摘要: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-22 13:13 krunning
摘要: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). 1 public class Solution { 2 ... 阅读全文
posted @ 2014-02-22 13:10 krunning
摘要: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-17 02:18 krunning
摘要: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-13 04:51 krunning
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path. 1 public class Solution { 2 public int minPathSum(int[][] grid) { 3 int len1 = grid.length; 4 if(len1==0) return 0; 5 int len2 =... 阅读全文
posted @ 2014-02-07 03:52 krunning
摘要:There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1,0], [0,0,0]] 1 public class Solution { 2 public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 int len = obstacleGrid.length; 4 if(len==0) return 0; 5 int len2 = obstacleGrid... 阅读全文
posted @ 2014-02-07 03:48 krunning
摘要: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. 1 public class Solution { 2 public boolean isInterleave(String s1, String s2, Strin 阅读全文
posted @ 2014-02-06 15:18 krunning
摘要: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-06 15:14 krunning
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ] 1 public class Solution { 2 public ArrayLis 阅读全文
posted @ 2014-02-06 15:01 krunning
摘要: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. 1 public cla 阅读全文
posted @ 2014-02-06 15:00 krunning
摘要:1 public class Solution { 2 public int climbStairs(int n) { 3 int f1 = 2; 4 int f2 = 1; 5 if(n<=0) return 0; 6 if(n==1) return f2; 7 if(n==2) return f1; 8 int fn=0; 9 for(int i=2;i<n;i++){10 fn=f1+f2;11 f2=f1;12 f1=fn;13 }14 return fn;15 }16 }View Code 阅读全文
posted @ 2014-02-06 14:59 krunning
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 1 public class Solution { 2 public int maximalRectangle(char[][] matrix) { 3 int row = matrix.length; 4 if(row=0;j--){28 if(matrix[i][j]=='1'){29 ... 阅读全文
posted @ 2014-02-06 14:47 krunning
摘要: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 1 public class Solution { 2 public int mi... 阅读全文
posted @ 2014-02-06 14:33 krunning
摘要: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-06 14:29 krunning
摘要: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. 1 public class Solution { 2 public String longestPalindrome(String s) { 3 if(s.length()=0;i--){10 for(int... 阅读全文
posted @ 2014-02-06 04:55 krunning