题目:
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

Example:
Input:
[1,2,3]

Output:
3

Explanation:
Only three moves are needed (remember each move increments two elements):
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]

思路:
因为sum+m*(len-1)=x*len,min+m=x,sum-num*len=m,所以就是需要求出最小值,结果就是结果 返回sum-num*len

代码:

class Solution {
public:int minMoves(vector<int>& nums) {int sum=0;int min=nums[0];//min的初始值为数组第一个元素int len=nums.size();for(int i=0;i<len;++i){min=nums[i]<min?nums[i]:min;//求数组中最小的元素sum+=nums[i];//求和}return sum-min*len;//根据思路说明返回结果}
};

**输出结果:**53ms

LeetCode 453. Minimum Moves to Equal Array Elements相关推荐

  1. leetcode 462. Minimum Moves to Equal Array Elements II

    这道题目和leetcode453是有联系的,虽然这道题难度为中等,但是我感觉初等难度的453绕的弯子更大一些. 题目:Given a non-empty integer array, find the ...

  2. LeetCode 462 Minimum Moves to Equal Array Elements II

    问题:给出一个数组,每次可以从中选择一个数+1或者-1,使得数组中的所有元素相等,要求操作次数最少. 思路:假设元素分别为x1,x2,...,xn,元素最终等于x,则要求|x1-x|+|x2-x|+. ...

  3. 453. Minimum Moves to Equal Array Elements (python)

  4. leetcode 453,462. Minimum Moves to Equal Array Elements I, II | 453, 462. 最少移动次数使数组元素相等(图解)

    453. Minimum Moves to Equal Array Elements https://leetcode.com/problems/minimum-moves-to-equal-arra ...

  5. LeetCode Minimum Moves to Equal Array Elements II

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

  6. C#LeetCode刷题之#453-最小移动次数使数组元素相等(Minimum Moves to Equal Array Elements)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3877 访问. 给定一个长度为 n 的非空整数数组,找到让数组所有 ...

  7. java求最小步数,使数组值相等的最小步数 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 ...

  8. leetcode-453-Minimum Moves to Equal Array Elements

    题目描述: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  9. LeetCode Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Woul ...

最新文章

  1. 提升网络安全 十大策略全面巩固企业内网
  2. Linux下的阻塞(Block)
  3. 004-cpu的区分
  4. java中如何获得屏幕的宽度和高度_在Android中获取屏幕宽度和高度
  5. uml 继承_UML 完全解析和总结
  6. php面向对象魔术方法,PHP面向对象中的魔术方法
  7. SQLite 3 中的数据类型
  8. al00华为手机_jmm-al00是什么型号
  9. 基于tcp协议的客户机与服务器通信程序的设计 c++,TCP服务器端和客户端程序设计...
  10. HTIML5 真的打败了Flash?新测试结果出人意料
  11. java判断天数_Java判断两个日期相差天数的方法
  12. Android网络图片加载三级缓存
  13. excel 置信区间 计算_用Excel求置信区间.ppt
  14. Word中相邻的两页无法没有对齐的解决方法
  15. CocosCreator3.x屏幕适配
  16. css定义变量(定义:--aa;使用:var(--aa)、calc()计算样式函数
  17. 小白入门Python Web之web开发前的准备(持续更新~)
  18. 免费抠图,一键抠图,ai智能艺术设计平台
  19. C++PrimerPlus 第三章 处理数据
  20. 【已解决】Discuz论坛头像无法上传,出现access……

热门文章

  1. 编译C/C++为dll供Python调用
  2. linux那些事之中断与异常(AMD64架构)_2
  3. IntelAI开发平台OpenVINO
  4. IE6的重定向页面无法跳转解决
  5. [Unity] GameFramework 学习记录 6:计时器
  6. (自定义组件)通用- X轴横向:溢出滚动 (含代码)- 案例篇
  7. html5软件下载页面源码
  8. 客户关系管理OACRM商品进销存销售管理合同订单库存财务管理跟单PHP源码
  9. client mysql 逗号_mysql comment line client(MySQL命令行常用操作)
  10. 人脉社群系统源码人脉推广裂变源码群发布源码