上一页 1 ··· 4 5 6 7 8 9 10 11 下一页

2013年5月24日

摘要: Problem:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Analysis:There two ways to solve the problem, either compute each row element separately or use inductive method.The latter cost much more le... 阅读全文
posted @ 2013-05-24 05:39 freeneng 阅读(200) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5... 阅读全文
posted @ 2013-05-24 05:26 freeneng 阅读(176) 评论(0) 推荐(0) 编辑
摘要: Problem:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence 阅读全文
posted @ 2013-05-24 05:03 freeneng 阅读(304) 评论(0) 推荐(0) 编辑

2013年5月20日

摘要: Problem:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Analysis:Remember a binary search tree's inorder treversal is a sorted array.To convert in the contrary direction, we need to create a node with the middle value of the given array and then c 阅读全文
posted @ 2013-05-20 08:13 freeneng 阅读(192) 评论(0) 推荐(0) 编辑
摘要: Problem:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.Analysis:To finish the task efficiently, merge should started from th 阅读全文
posted @ 2013-05-20 07:57 freeneng 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Probelm:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].Analysis:One pass algorithm, with the help of an extra counter which keeps recoreds of the nu 阅读全文
posted @ 2013-05-20 07:44 freeneng 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Probelm: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?Analysis:Simplest DP problem,Sn = Sn-1 + Sn-2 with S1 = 1 and S2 = 2;Code: 1 class Solution { 2 public: 3 int climbStairs(int ... 阅读全文
posted @ 2013-05-20 07:31 freeneng 阅读(154) 评论(0) 推荐(0) 编辑
摘要: Problem:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".Analysis:Three main steps: 1. add a and b until meet one's end; 2. process the remaining part of the the other string; 3. process the carry bit.Simulation 阅读全文
posted @ 2013-05-20 07:24 freeneng 阅读(146) 评论(0) 推荐(0) 编辑

2013年5月19日

摘要: Problem:Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem statement to be ambiguous. You should gather all requirements up front before imp 阅读全文
posted @ 2013-05-19 13:41 freeneng 阅读(186) 评论(0) 推荐(0) 编辑
摘要: Problem: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 poss 阅读全文
posted @ 2013-05-19 11:59 freeneng 阅读(185) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 下一页

导航