75. 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 blue respectively.

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

 1 class Solution {
 2     public void sortColors(int[] nums) {
 3         int begin  = 0;
 4         int cur = 0;
 5         int end = nums.length-1;
 6         while(cur<=end){
 7             if(nums[cur]==2){
 8                 swap(nums,cur,end);
 9                 end--;
10             }
11             else if(nums[cur]==1){
12                 cur++;
13             }
14             else //(nums[cur]==0)
15             {
16                 if(nums[cur]!=nums[begin])
17                     swap(nums,cur,begin);
18                 begin++;
19                 cur++;
20             }
21         }
22     }
23     private void swap(int[] nums,int i ,int j){
24         int temp;
25         temp = nums[i];
26         nums[i] = nums[j];
27         nums[j] = temp;
28     }
29 }

原文地址:https://www.cnblogs.com/zle1992/p/8455978.html

时间: 2024-10-13 16:19:59

75. Sort Colors(荷兰国旗问题 三指针)的相关文章

刷题75. Sort Colors

一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是简单的,代码如下: class Solution{ public: void sortColors(vector<int>& nums){ int num0=0,num1=0,num2=0; for(int i=0;i<nums.size();i++){ if(nums[i]==0)

No.75 Sort Colors

No.75 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

【一天一道LeetCode】#75. Sort Colors

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

75. 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

75. Sort Colors (Array)

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 sort]75. 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 75 Sort Colors(颜色排序)

翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字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, whit

LeetCode 75. 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 75. 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