[Swift]LeetCode462. 最少移動次數使數組元素相等 II | Minimum Moves to Equal Array Elements II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-fnuizaeg-ko.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.git

You may assume the array's length is at most 10,000.github

Example:數組

Input:
[1,2,3]

Output:
2

Explanation:
Only two moves are needed (remember each move increments or decrements one element):

[1,2,3]  =>  [2,2,3]  =>  [2,2,2]

給定一個非空整數數組,找到使全部數組元素相等所需的最小移動數,其中每次移動可將選定的一個元素加1或減1。 您能夠假設數組的長度最多爲10000。微信

例如:ui

輸入:
[1,2,3]

輸出:
2

說明:
只有兩個動做是必要的(記得每一步僅可以使其中一個元素加1或減1): 

[1,2,3]  =>  [2,2,3]  =>  [2,2,2]

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {
 3         //轉換爲變量
 4         var arr:[Int] = nums
 5         //對數組進行升序排序
 6         //sorted只返回排序數組, sort纔會修改原數組
 7         //arr.sort(by: {$1 < $2})
 8         arr = arr.sorted(by: <)
 9         let len = nums.count
10         var res:Int = 0, mid:Int = arr[len / 2]
11         for num in arr
12         {
13             res += abs(num - mid)
14         }
15         return res
16     }
17 }

24msspa

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {
 3         let a = nums.sorted(by:{ $0 < $1 })
 4         var ret = 0 ,i = 0 ,j = nums.count - 1
 5         while i < j {
 6             ret += a[j] - a[i]
 7             j -= 1 ; i += 1
 8         }
 9         return ret
10     }
11 }

28mscode

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {        
 3         var i = 0
 4         var j = nums.count - 1
 5         var result = 0
 6         
 7         let sortedNums = nums.sorted(by:{ $0 < $1 })
 8         
 9         while i < j {
10             result += sortedNums[j] - sortedNums[i]
11             i += 1
12             j -= 1
13         }
14         
15         return result
16     }
17 }

32mshtm

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {
 3         
 4         var _num = nums.sorted(by:<)
 5         
 6         var middleCount = _num.count/2
 7         
 8         var value = _num[middleCount]
 9         var steps = 0
10         for n in _num
11         {
12             if n != value
13             {
14                steps += abs(value - n)
15             }
16             
17         }
18         
19         return steps
20     }
21 }

40msblog

 1 class Solution 
 2 {
 3     func minMoves2(_ nums: [Int]) -> Int 
 4     {
 5       
 6         // option 2
 7         let arr = nums.sorted()
 8         var i = 0, j = arr.count - 1
 9         var count = 0
10         while i < j
11         {
12             count += (arr[j] - arr[i])
13             i += 1
14             j -= 1
15         }
16         return count
17     }
18 }

44ms

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {
 3         if (nums.count <= 1) {
 4             return 0
 5         }
 6         
 7         var nums = nums
 8         nums.sort()
 9         
10         var count = 0
11         for i in 0..<nums.count {
12             count += abs(nums[nums.count/2] - nums[i])
13         }
14         return count
15         
16     }
17 }

48ms

 1 class Solution {
 2     func minMoves2(_ nums: [Int]) -> Int {
 3         var raw = nums
 4         raw.sort { (num1, num2) -> Bool in
 5             return num1 < num2
 6         }
 7         var sum: Int = 0
 8         let mean = raw[raw.count / 2]
 9         for i in 0 ..< nums.count {
10             if i < raw.count / 2 {
11                 sum += (mean - raw[i])
12             }else {
13                 sum += (raw[i] - mean)
14             }
15         }
16         return sum
17     }
18 }
相關文章
相關標籤/搜索