1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_462.java 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 6年前 . refactor 462
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 462. Minimum Moves to Equal Array Elements II
*
* Given a non-empty integer array,
* find the minimum number of moves required to make all array elements equal,
* where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
*
* You may assume the array's length is at most 10,000.
Example:
Input:
[1,2,3]
Output:
2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]
*/
public class _462 {
public static class Solution1 {
public int minMoves2(int[] nums) {
/**sort this array, find the median of this array as the pivot*/
Arrays.sort(nums);
int result = 0;
int result1 = 0;
if (nums.length % 2 != 0) {
int median = nums[nums.length / 2];
for (int n : nums) {
result += Math.abs(n - median);
}
return result;
} else {
int median1 = nums[nums.length / 2];
for (int n : nums) {
result1 += Math.abs(n - median1);
}
int result2 = 0;
int median2 = nums[nums.length / 2 - 1];
for (int n : nums) {
result2 += Math.abs(n - median2);
}
result1 = Math.min(result1, result2);
return result1;
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助