上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 43 下一页
摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Top down 的解题方法:1. 将LinkedList的值保存到一个数组中,转化成Convert Sorted Array to Binary Search Tree 来解决时间复杂度为O(n), 空间复杂度为O(n) 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 *... 阅读全文
posted @ 2013-08-19 15:19 feiling 阅读(1424) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.[解题思路]递归确定每棵树的根节点的值,这里根节点的值是二分搜索的中值由于这里是有序数组,确定root的时间复杂度为O(1), 整个算法的时间复杂度为O(n),n为节点数目。 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * ... 阅读全文
posted @ 2013-08-19 11:21 feiling 阅读(229) 评论(0) 推荐(0) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak) must 阅读全文
posted @ 2013-08-18 21:06 feiling 阅读(518) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak 阅读全文
posted @ 2013-08-18 20:19 feiling 阅读(232) 评论(0) 推荐(0) 编辑
摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] 1 public class Solution { 2 public ArrayList> combine(int n, int k) { 3 // Start typing your Java solution below... 阅读全文
posted @ 2013-08-18 19:55 feiling 阅读(359) 评论(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 consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.[解题思路]数组,O(n)---->使用hash,空间换时间对于当前数N 阅读全文
posted @ 2013-08-17 21:44 feiling 阅读(362) 评论(0) 推荐(0) 编辑
摘要: 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 at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). 1 public c 阅读全文
posted @ 2013-08-17 20:46 feiling 阅读(297) 评论(0) 推荐(0) 编辑
摘要: 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-08-17 15:45 feiling 阅读(210) 评论(0) 推荐(0) 编辑
摘要: 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.[解题思路]记录一个最小值,每次拿当前值与最小值比较,如果两者差>profit,则更新profit 1 publ 阅读全文
posted @ 2013-08-17 15:12 feiling 阅读(253) 评论(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.[解题思路]检查每个node是否是balanced,大数据集挂掉了 1 /** 2 * Definition for binary tree 3 * public class.. 阅读全文
posted @ 2013-08-16 21:28 feiling 阅读(227) 评论(0) 推荐(0) 编辑
上一页 1 ··· 13 14 15 16 17 18 19 20 21 ··· 43 下一页