[Swift]LeetCode75. 顏色分類 | Sort Colors

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

Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.git

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.github

Note: You are not suppose to use the library's sort function for this problem.算法

Example:數組

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Follow up:微信

    • A rather straight forward solution is a two-pass algorithm using counting sort.
      First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
    • Could you come up with a one-pass algorithm using only constant space?

給定一個包含紅色、白色和藍色,一共 個元素的數組,原地對它們進行排序,使得相同顏色的元素相鄰,並按照紅色、白色、藍色順序排列。函數

此題中,咱們使用整數 0、 1 和 2 分別表示紅色、白色和藍色。this

注意:
不能使用代碼庫中的排序函數來解決這道題。spa

示例:code

輸入: [2,0,2,1,1,0]
輸出: [0,0,1,1,2,2]

進階:

    • 一個直觀的解決方案是使用計數排序的兩趟掃描算法。
      首先,迭代計算出0、1 和 2 元素的個數,而後按照0、一、2的排序,重寫當前數組。
    • 你能想出一個僅使用常數空間的一趟掃描算法嗎?

8ms

 1 class Solution {
 2     func sortColors(_ nums: inout [Int]) {
 3         
 4         var i = 0
 5         var left = 0
 6         var right = nums.count - 1
 7         
 8         while i <= right {
 9             if nums[i] == 0 {
10                 nums.swapAt(i, left)
11                 i += 1
12                 left += 1
13             } else if nums[i] == 1 {
14                 i += 1
15             } else {
16                 nums.swapAt(i, right)
17                 right -= 1
18             }
19         }
20     }
21 }

12ms

 1 class Solution {
 2     /// 桶排序法, 0...red 都是 0,blue...nums.count-1 都是2
 3     func sortColors(_ nums: inout [Int]) {
 4         var red = -1
 5         var blue = nums.count
 6         var i = 0
 7         
 8         while i < blue {
 9             if nums[i] == 0 {
10                 red += 1
11                 (nums[i], nums[red]) = (nums[red], nums[i])
12                 i += 1
13             } else if nums[i] == 2 {
14                 blue -= 1
15                 (nums[i], nums[blue]) = (nums[blue], nums[i])
16             } else {
17                 i += 1
18             }
19         }
20     }
21 }

16ms

 1 class Solution {
 2     func sortColors(_ nums: inout [Int]) {
 3         var zeroCounter = 0
 4         var oneCounter = 0
 5         var twoCounter = 0
 6         
 7         for each in nums{
 8             if each == 0{
 9                 zeroCounter += 1
10             }else if each == 1{
11                 oneCounter += 1
12             }else if each == 2{
13                 twoCounter += 1
14             }
15         }
16         
17         for each in 0..<zeroCounter{
18             nums[each] = 0
19         }
20         
21         for each in 0..<oneCounter{
22             nums[zeroCounter+each] = 1
23         }
24         
25         for each in 0..<twoCounter{
26             nums[zeroCounter+oneCounter+each] = 2
27         }
28         
29     }
30 }

20ms

 1 class Solution {
 2     func sortColors(_ nums: inout [Int]) {
 3             var redCount = 0, whiteCount = 0, blueCount = 0
 4     for i in 0..<nums.count {
 5         if nums[i] == 0 {
 6             redCount = redCount + 1
 7             nums[redCount - 1] = 0
 8         } else if nums[i] == 1 {
 9             whiteCount = whiteCount + 1
10         } else {
11             blueCount = blueCount + 1
12         }
13     }
14     for i in redCount..<redCount+whiteCount {
15         nums[i] = 1
16     }
17     for i in redCount+whiteCount..<nums.count {
18         nums[i] = 2
19     }
20     }
21 }
相關文章
相關標籤/搜索