标签:最大值 strong 性能 fine 运行时间 校验 ted nod his
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:
The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.
Example 1:
Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:
6
/ 3 5
\ /
2 0
1
Note:
The size of the given array will be in the range [1,1000].
题目不算难,可以很容易的想到解决思路:
于是我用最直接的代码写出了以下解决方法
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public static TreeNode constructMaximumBinaryTree(int[] nums) {
if(nums.length==0) return null;
int max=getMaxNumber(nums);
TreeNode root=new TreeNode(max);
root.left=constructMaximumBinaryTree(getArrayLeftPart(max, nums));
root.right=constructMaximumBinaryTree(getArrayRightPart(max, nums));
return root;
}
public static int getMaxNumber(int[] nums) {
int[] tempArray = nums.clone();
Arrays.sort(tempArray);
return tempArray[tempArray.length - 1];
}
public static int[] getArrayLeftPart(int spilt, int[] nums) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
if (nums[i] == spilt) {
break;
}
list.add(nums[i]);
}
int[] result = new int[list.size()];
for (int k = 0; k < list.size(); k++) {
result[k] = list.get(k);
}
return result;
}
public static int[] getArrayRightPart(int spilt, int[] nums) {
List<Integer> list = new ArrayList<Integer>();
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] == spilt) {
break;
}
list.add(nums[i]);
}
int[] result = new int[list.size()];
for (int k = list.size() - 1; k >= 0; k--) {
result[list.size() - k - 1] = list.get(k);
}
return result;
}
}
这个解法给了AC,但是效率很低,Leetcode给出的运行时间是433ms。对于性能差我能想到的原因有:
上面的方法使用在递归时传的是值(value),这样的递归需要传递的参数个数比较少,但对资源的利用率不高。所以我们改为传递索引(index)的方式,提高对数组的利用率。这样在数据传递的时候只需要传递数组的索引值,整个递归过程只需要对一个数组进行操作。
public static TreeNode constructMaximumBinaryTree(int[] nums){
return constructor(nums,0,nums.length-1);
}
//给出的method只能传递一个参数,所以用一个新的method
public static TreeNode constructor(int[] nums,int left,int right){
if (left>right)
return null;
int max=MaxArrayIndex(nums, left, right);
TreeNode root=new TreeNode(nums[max]);
root.left=constructor(nums,left,max-1);
root.right=constructor(nums, max+1, right);
return root;
}
//获取数组中最大值的索引
public static int MaxArrayIndex(int[] nums,int left,int right){
int point=nums[left];
int max_index=left;
for(int i=left+1;i<=right;i++){
if(nums[i]>point){
point=nums[i];
max_index=i;
}else continue;
}
return max_index;
}
改进后运行时间降到了13ms,超过了66.8%的提交。
边界问题似乎不是什么大问题,但它却会在一定程度上阻碍你解题的速度。
首先我们得先了解哪里会出现数组越界的情况。容易想到越界肯定容易在索引发生变化的地方出现,也就是在递归那一步的时候。由于在递归时我们改变了数组的索引,所以我们必须在函数的第一步判断索引是不是有效的。在本例中你需要保证:
第一点保证了递归内部获取的索引一定是合法的。第二点保证了,调用递归时索引的值是合法的。第三点保证了子递归出现索引问题时能够及时阻断这个子递归。
[LeetCode] 654. Maximum Binary Tree
标签:最大值 strong 性能 fine 运行时间 校验 ted nod his
原文地址:http://www.cnblogs.com/rever/p/8004106.html