LintCode: Sort Colors

通过交换,对0,1,2排序

使用三个标记[循环不变式]

i从前向后,记录最后一个0的位置

j从后向前,记录第一个2的位置

k从前向后,是遍历用的游标

[0..i-1]是0

[i..k-1]是1

[k,j-1]是未探测

[j..n-1]是2

初始k=0时,0,1,2的区域都是空,所有区域都是未探测,循环k=0..n-1

如果a[k] = 0=>swap(a[i++], a[k])

如果a[k] = 1=>无操作

如果a[k] = 2=>swap(a[--j], a[k--])

复杂度O(n)

 1 class Solution{
 2 public:
 3     /**
 4      * @param nums: A list of integer which is 0, 1 or 2
 5      * @return: nothing
 6      */
 7     void sortColors(vector<int> &nums) {
 8         // write your code here
 9         int i = 0, j = nums.size();
10         for (int k = 0; k < j; ++k) {
11             if (nums[k] == 0) {
12                 swap(nums[i++], nums[k]);
13             } else if (nums[k] == 2) {
14                 swap(nums[--j], nums[k--]);
15             }
16         }
17     }
18 };
时间: 2024-10-17 06:23:52

LintCode: Sort Colors的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

Sort Colors II Lintcode

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. Notice You are not suppose to use the library's sort function for this pro

Sort colors系列

75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第二遍开始摆放数字. 代码如下: 1 void sortColors(vector<int>& nums) { 2 int i=0,j=0,k=0; 3 int n=nums.size(); 4 for(int p=0;p<n;p++) 5 { 6 if(nums[p]==0) 7 i

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列. Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integ

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer

LeetCode: Sort Colors 题解

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[LeetCode OJ] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl