-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenzhengwei
committed
Feb 27, 2017
1 parent
94f2d9c
commit 9796f76
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#453. Minimum Moves to Equal Array Elements | ||
[题目链接](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/?tab=Description) | ||
```java | ||
public class Solution { | ||
/* | ||
一共有n个数字,每次有n-1个数字加一, | ||
可以转换为每次有一个数字减一, | ||
这样就可以转换为每个数见到最小数字的总和 | ||
*/ | ||
public int minMoves(int[] nums) { | ||
int min = Integer.MAX_VALUE; | ||
for (int num : nums) { | ||
if (num < min) { | ||
min = num; | ||
} | ||
} | ||
int moves = 0; | ||
for (int num : nums) { | ||
moves += (num - min); | ||
} | ||
return moves; | ||
} | ||
} | ||
``` |