上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 13 下一页
摘要: 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./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode ... 阅读全文
posted @ 2014-01-06 11:40 23lalala 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public static Tree... 阅读全文
posted @ 2014-01-06 11:40 23lalala 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 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?public class Solution { public static HashMap dp = new HashMap(); public int climbStairs(int n) { if (dp.containsKey(n)){ ... 阅读全文
posted @ 2014-01-06 11:40 23lalala 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } * * 麻烦的地方在于java里的array... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Implement pow(x,n).public class Solution { public double pow(double x, int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int absn = Math.abs(n); if (absn==0.0) { //0的多少次方都是1 ... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(158) 评论(0) 推荐(0) 编辑
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.public class Solution { public int removeElement(int[] A, int elem) { int n = A.length; for ... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(88) 评论(0) 推荐(0) 编辑
摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.public class Solution { public int maxSubArray(int[] A) { if (A.length==1) { ... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(90) 评论(0) 推荐(0) 编辑
摘要: Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5],[6,7],[8 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(109) 评论(0) 推荐(0) 编辑
摘要: Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]./** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { ... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(89) 评论(0) 推荐(0) 编辑
摘要: Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".public class Solution { public String addBinary(String a, String b) { int alen = a.length(); int blen = b.length(); //表示进位 int add=0; int i; StringBuild... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(87) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 13 下一页