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) 编辑

导航

点击右上角即可分享
微信分享提示