算法总结

1.给定数组[1,1,1,2,2,2,4,5,6,4,2,1]

很多时候,需要针对该数组创建一个给不包含重复元素的新数组。

这时,为了避免占用额外的空间去统计新数组的长度素,我们可以通过求解旧数组中最大值的方法来求解长度。

例如该数组中最大值为6.那新数组的长度则最大为7.即0-6.

当然这个数组可能有多出来的空余元素。

该方法可以减少算法的复杂度。

 int maxNum = 0;
        // find the maximum number in input array 1 , 3 , 4 , 4  = 4
        for (int i =0;i< nums.length; i++)
            maxNum = Math.max(maxNum, nums[i]);

  

原文地址:https://www.cnblogs.com/lyr2015/p/7692581.html