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

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

解法1:

统计每个颜色出现的次数,这个方法比较简单易懂

 1 class Solution(object):
 2     def sortColors(self, nums):
 3         flag = [0,0,0]
 4         for i in nums:
 5             flag[i] += 1
 6         for n in range(flag[0]):
 7             nums[n] = 0
 8         for n in range(flag[1]):
 9             nums[flag[0]+n]=1
10         for n in range(flag[2]):
11             nums[flag[0]+flag[1]+n] = 2

解法2:

在评论区总是能发现一些很漂亮的方法,方法和快排比较相似

 1 class Solution(object):
 2     def sortColors(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: void Do not return anything, modify nums in-place instead.
 6         """
 7         i,start,end = 0,0,len(nums)-1
 8         while i<=end:
 9             if nums[i]==0:
10                 nums[i]=nums[start]
11                 nums[start]=0
12                 start += 1
13             elif nums[i]==2:
14                 nums[i]=nums[end]
15                 nums[end]=2
16                 end -= 1
17                 i -= 1 #交换后此位置的数未考虑,要重新考虑
18             i += 1

解法3:

最帅的是这种方法,类似于快排,指针i,j分别处理以类数据

 1 class Solution(object):
 2     def sortColors(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: void Do not return anything, modify nums in-place instead.
 6         """
 7         i,j = 0,0
 8         for k in range(len(nums)):
 9             v = nums[k]
10             nums[k] = 2
11             if v<2:
12                 nums[j]=1
13                 j += 1
14             if v == 0:
15                 nums[i]=0
16                 i += 1
时间: 2024-10-06 07:46:11

[leetcode sort]75. Sort Colors的相关文章

【一天一道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

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

【leetcode】75.Sort Colors

题目说明 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. https://leetcode-cn.com/problems/sort-colors/ 解法1 时间复杂度:O(n) 空间复杂度:O(1) 思路:使用计数排序法,先遍历一遍统计出各个颜色的个数,然后再遍历第二遍,按指定顺序赋值. void sortColors(vector<int>

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

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

刷题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)

【LeetCode】Insertion Sort List

题目 Sort a linked list using insertion sort. 解答 链表无法像数组那样从后往前依次比较插入,只能从前往后:在链表首部添加一个哨兵可以稍微简化下代码,代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }

[LeetCode 题解]: Insertion Sort List

Sort a linked list using insertion sort. 题目要求:链表的插入排序,由于没有时间复杂度的要求,可以直接循环操作. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

LeetCode OJ - Insertion Sort List

题目: Sort a linked list using insertion sort. 解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class S