453. Minimum Moves to Equal Arra

2018-04-11  本文已影响7人  安东可

453. Minimum Moves to Equal Array Elements
[思路]

 sum + m* (n - 1) = x * n
变为
  x = minNum + m
计算为:
  sum - minNum * n = m
    int minMoves(vector<int>& nums) {
        int sum =0;
        int min =0;
        int len = nums.size();
        if(len <=1)return 0;
        min = nums[0];
        
        for(int i=0;i<nums.size();i++)
        {
            sum += nums[i];
            if(nums[i] < min)
                min = nums[i];
        }

    
        return sum - min*len;
    }
上一篇下一篇

猜你喜欢

热点阅读