在做这道题的时候,其实我一开始也是思维错了,要想使得数组中元素相同,那不就每次选中最大值,然后让剩下的值+1不就得了嘛,思维上是这么个情况,但是代码实现起来复杂度太高了
因此就换了一种思维,每次选中一个值 相对数组中的值来说,也就意味着每次有一个元素-1其它元素值不变效果也是一样的。
public class Array {
public int minMoves(int[] nums) {
int count = 0;
int i;
//定义最小值为数组中第一个元素,避免数组中元素全部大于你自定义的其它值
int min = nums[0];
for (int i = 0; i < nums.length; i++) {
if (min >= nums[i]) min = nums[i];
}
for (int i = 0; i < nums.length; i++) {
count =count+nums[i]-min;
}
return count;
}
}



