上一页 1 ··· 4 5 6 7 8
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space. 1 public class Solution{ 2 public int firstMissingPositive(int[] A){ 3 int result = 1; //if the... 阅读全文
posted @ 2014-01-30 08:16 Averill Zheng 阅读(139) 评论(0) 推荐(0) 编辑
摘要: Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13&qu 阅读全文
posted @ 2014-01-30 06:57 Averill Zheng 阅读(118) 评论(0) 推荐(0) 编辑
摘要: Implement pow(x,n).divide and conquer method: the running time is O(ln n) 1 public class Solution{ 2 public double pow(double x, int n){ 3 double result = 0; 4 long temp = n; 5 if(temp >= 0) 6 result = power(x, temp); 7 else 8 result =... 阅读全文
posted @ 2014-01-29 23:38 Averill Zheng 阅读(136) 评论(0) 推荐(0) 编辑
摘要: Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?solution 1 using O(n) space: 1 /** 2 * Definition for binary tree 3 * public cla.. 阅读全文
posted @ 2014-01-29 13:44 Averill Zheng 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val... 阅读全文
posted @ 2014-01-29 03:31 Averill Zheng 阅读(142) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { va... 阅读全文
posted @ 2014-01-29 03:15 Averill Zheng 阅读(135) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Fo... 阅读全文
posted @ 2014-01-28 23:38 Averill Zheng 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 ... 阅读全文
posted @ 2014-01-28 22:35 Averill Zheng 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors 1 /** 2 * Definition for undirected graph. 3 * class Und... 阅读全文
posted @ 2014-01-28 10:52 Averill Zheng 阅读(166) 评论(0) 推荐(0) 编辑
摘要: 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 @ 2014-01-28 10:48 Averill Zheng 阅读(136) 评论(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 maximu... 阅读全文
posted @ 2014-01-17 12:44 Averill Zheng 阅读(140) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8