上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 13 下一页
摘要: Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?confused what"{1,#,2,3}"means?> read more on how binary tree is serialized on OJ./** * 阅读全文
posted @ 2014-01-06 11:40 23lalala 阅读(159) 评论(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 阅读(109) 评论(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 阅读(143) 评论(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 阅读(164) 评论(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 阅读(93) 评论(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 阅读(97) 评论(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 阅读(131) 评论(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 阅读(98) 评论(0) 推荐(0)
摘要: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * nex... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(115) 评论(0) 推荐(0)
摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place./** *O(1)空间解法,把每行的第一个元素和每列的第一个元素作为改行列是否清零的标志 **/public class Solution { public void setZeroes(int[][] matrix) { if (matrix==null) { return; } int row = matrix.length; in... 阅读全文
posted @ 2014-01-06 11:39 23lalala 阅读(95) 评论(0) 推荐(0)
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 13 下一页