https://leetcode.com/problems/minimum-moves-to-equal-array-elements/

package com.company;

import java.util.*;

class Solution {
public int minMoves(int[] nums) {
// 关键是要想到方法
// 先把第一个和第二个抹平,再把第二个第三个抹平,依次类推
Arrays.sort(nums);
int ret = 0;
int acc = 0;
for (int i=1; i<nums.length; i++) {
acc += nums[i] - nums[i-1];
ret += acc;
}
return ret;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] nums = {1,2,3};
int ret = solution.minMoves(nums);
System.out.printf("ret:%d\n", ret); System.out.println(); } }
05-11 22:01