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(); } }

minimum-moves-to-equal-array-elements的更多相关文章

  1. [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二

    Given a non-empty integer array, find the minimum number of moves required to make all array element ...

  2. LeetCode Minimum Moves to Equal Array Elements II

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...

  3. LeetCode Minimum Moves to Equal Array Elements

    原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...

  4. Leetcode-462 Minimum Moves to Equal Array Elements II

    #462.   Minimum Moves to Equal Array Elements II Given a non-empty integer array, find the minimum n ...

  5. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  6. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  7. LeetCode_453. Minimum Moves to Equal Array Elements

    453. Minimum Moves to Equal Array Elements Easy Given a non-empty integer array of size n, find the ...

  8. LeetCode 453. 最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements) 47

    453. 最小移动次数使数组元素相等 453. Minimum Moves to Equal Array Elements 题目描述 给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移 ...

  9. [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等

    Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...

  10. LeetCode 453 Minimum Moves to Equal Array Elements

    Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...

随机推荐

  1. 在当前iframe中, 获取Iframe的id

    window.frameElement   返回嵌入当前window对象的元素(比如 <iframe> 或者 <object>),如果当前window对象已经是顶层窗口,则返回 ...

  2. Android App集成支付宝

    原地址:http://blog.csdn.net/wenbingoon/article/details/7933078 手机的在线支付,被认为是2012年最看好的功能,我个人认为这也是移动互联网较传统 ...

  3. POJ 1491

    #include<iostream> #include<cmath> #include<iomanip> #define MAXN 50 using namespa ...

  4. mvc5 错误页如何定义

    项目根目录下的Web.config <system.web> <customErrors mode="On" defaultRedirect="~/Er ...

  5. kafka配置

    官网:http://kafka.apache.org/ 主要有3种安装方式: 1. 单机单broker 2. 单机多broker 3. 多机多broker 1. wget http://mirror. ...

  6. C 和C++ 名称修饰规则

    C名称修饰规则 1.对于使用__cdecl调用约定的函数,在函数名称前加一下划线,不考虑参数和返回值. 2.对于使用__fastcall调用约定的函数,在函数名称前后各加一@符号,后跟参数的长度,不考 ...

  7. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  8. Hbase Shell命令

    1 启动HBase shell 2 HBase shell 命令 3 我们将以“一个学生成绩表”的例子来详细介绍常用的 HBase 命令及其使用方法. 这里 grad 对于表来说是一个列,course ...

  9. MVC新手指南

    MVC新手指南 2010-04-06 09:54:23 18839 次阅读 0 条评论   本文感谢东西提供 模型-视图-控制器(MVC)可能是近年来网络编程圈子里最常被提及的模式之一.目前与网络应用 ...

  10. Android中级之网络数据解析一之xml解析

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! --Comic Sans MS Xml解析具有跨平台性,语言无关性,易操作性,因此广受开发者的欢迎. ...