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. MAC下搭建web开发环境

    具体做法,参照此链接:http://mallinson.ca/osx-web-development/ Mac系统本身自带apache和PHP,MySQL可以安装也可以不安装 web开发的IDE可以是 ...

  2. 亚马逊 在线测试题目 amazon (变种的)三叉树的最近公共祖先问题

    题目意思就是找一棵按上面链接所示的树对应的上面的两个点的最小公共祖先(LCP,Least Common Father),按照比较大小来依次返回自己的父亲节点就行了.具体看代码:getfather(a) ...

  3. <context:annotation-config> 跟 <context:component-scan>诠释及区别

    <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注 ...

  4. ASP.NET运行机制之一般处理程序(ashx)

    一. 概述 新建一个ashx文件  代码如下 <%@ WebHandler Language="C#" Class="TestHandler" %> ...

  5. JS中showModalDialog 详细使用(转)

    基本介绍: showModalDialog()         (IE 4+ 支持) showModelessDialog()      (IE 5+ 支持) window.showModalDial ...

  6. MySQL 操作表命令

    新建表: create table Itemcats (id int(11) not null auto_increment, primary key (id)) engine=MyISAM auto ...

  7. codeforces 430A Points and Segments (easy)(理解能力有待提高……)

    题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include ...

  8. Unix安装BerkeleyDB

    下载安装包Berkeley DB 5.3.21.tar.gz http://www.oracle.com/technetwork/products/berkeleydb/downloads/index ...

  9. zoj 3529 A Game Between Alice and Bob 博弈论

    思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...

  10. UVA 11361 - Investigating Div-Sum Property 数位DP

    An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...