2013年9月11日

Chp3: Stacks and Queue

摘要: 1.说明如何用两个队列来实现一个栈,并分析有关栈操作的运行时间。解法:1.有两个队列q1和q2,先往q1内插入a,b,c,这做的都是栈的push操作。2.现在要做pop操作,即要得到c,这时可以将q1中的a,b两个元素全部dequeue并存入q2中,这时q2中元素为a,b,对q1再做一次dequeue操作即可得到c。3.如果继续做push操作,比如插入d,f,则把d,f插入到q2中,4.此时若要做pop操作,则做步骤25.以此类推,就实现了用两个队列来实现一个栈的目的。注意在此过程中,新push进来的元素总是插入到非空队列中,空队列则用来保存pop操作之后的那些元素,那么此时空队列不为空了,原 阅读全文

posted @ 2013-09-11 12:23 Step-BY-Step 阅读(148) 评论(0) 推荐(0) 编辑

Chp2: Linked List

摘要: 2.2 Implement an algorithm to find the kth to last element of a singly linked list.Just using "runner" tech,let the first pointer to run for k step, then start the second pointer, at the same speed.When the first one hit the end, the second one is just on the kth element.2.5(1) input: (7 - 阅读全文

posted @ 2013-09-11 11:14 Step-BY-Step 阅读(271) 评论(0) 推荐(0) 编辑

Populating Next Right Pointers in Each Node

摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文

posted @ 2013-09-11 07:45 Step-BY-Step 阅读(206) 评论(0) 推荐(0) 编辑

Valid Palindrome

摘要: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Pana... 阅读全文

posted @ 2013-09-11 07:36 Step-BY-Step 阅读(168) 评论(0) 推荐(0) 编辑

Binary Tree Maximum Path Sum

摘要: Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 ... 阅读全文

posted @ 2013-09-11 06:34 Step-BY-Step 阅读(201) 评论(0) 推荐(0) 编辑

Best Time to Buy and Sell Stock II

摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文

posted @ 2013-09-11 05:27 Step-BY-Step 阅读(128) 评论(0) 推荐(0) 编辑

Best Time to Buy and Sell Stock

摘要: Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.最简单的方法就是穷举,O(n2)。 1 public class Solution { 2 public in... 阅读全文

posted @ 2013-09-11 05:16 Step-BY-Step 阅读(192) 评论(0) 推荐(0) 编辑

Longest Consecutive Sequence

摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest ... 阅读全文

posted @ 2013-09-11 02:51 Step-BY-Step 阅读(217) 评论(0) 推荐(0) 编辑

Next Permutation

摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl... 阅读全文

posted @ 2013-09-11 01:41 Step-BY-Step 阅读(395) 评论(0) 推荐(0) 编辑

导航