新博文地址:[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 blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
click to show follow up.
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 an one-pass algorithm using only constant space?
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.
click to show follow up.
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 an one-pass algorithm using only constant space?
暂未想到one-pass的方法
2-pass的比较简单,借助一个hash表即可。
public void sortColors(int[] A) { int[] hash = new int[3]; for(int tem : A){ hash[tem]++; } int index = 0; for(int i = 0; i < 3;i++){ while(hash[i] != 0){ A[index++] = i; hash[i]--; } } }
相关推荐
python python_leetcode题解之075_Sort_Colors
c c语言_leetcode题解之0075_sort_colors.zip
javascript js_leetcode题解之75-sort-colors.js
Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 LeetCode 3 Longest Substring...
* [Sort](https://github.com/kamyu104/LeetCode#sort) * [Recursion](https://github.com/kamyu104/LeetCode#recursion) * [Binary Search](https://github.com/kamyu104/LeetCode#binary-search) * [Binary Search...
- Sort Colors: 给定一个包含红色、白色和蓝色,一共n个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 - Combinations: 从n个不同元素中取出k个元素的组合。 - ...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone ...Sort Colors M
_75_SortColors_twopointer _142_LinkedListCycle2_twopointer //////// 弗洛伊德循环检测 _287_FindtheDuplicateNumber_TwoPointer_Floyd _340_LongestSubstringwithAtMostKDistinctCharacters_TwoPointer _424_...
Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表类(Linked List): 基础知识:链表如何...
6. **Sort Colors**:将一个包含0、1和2的数组原地排序。可以采用双指针策略,让较小的元素向左移动。 7. **Sort List**:对链表进行排序。可以先将链表转换为数组,然后使用快速排序或归并排序,最后将排序后的...
def sortColors(nums): left, right = 0, len(nums) - 1 while left if nums[left] == 0: left += 1 elif nums[left] == 2: nums[left], nums[right] = nums[right], nums[left] right -= 1 else: left +=...
public void sortColors(int[] nums) { int left = 0, right = nums.length - 1; while (left ) { while (left [left] == 0) { // 遇到红色球,向右移动 left++; } while (left [right] == 2) { // 遇到蓝色...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆...Colors 计数排序 | | | 88 Merge So
扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...sortColors 179.最大数 largestNumber 324.摆
SortColors 167 - TwoSum2 DCP 75 是一个双指针问题,如果当前项为 0,则使用 p1 p2 指向开始和结束,然后与开始交换,如果当前项目为 2,则与结束交换。 167是同一个想法 02/01/2020 16 -3SumClosest 344 - Reverse...
例如,"Sort Colors" (题号 25) 要求原地对数组进行颜色排序。 10. **哈希表**:利用哈希表快速查找和存储数据,解决如两数之和、唯一元素等问题。例如,"First Unique Number" (题号 347) 找出数组中的第一个不...
leetcode中国 每周小测 每周题目 week 1 adjust : 将数组中指定索引处的值替换为经函数变换的值 实现版本: ramda版本参考: groupAnagrams ...给定一个字符串数组,将字母异位词组合在一起。...sortColors
排序颜色: ://leetcode.com/problems/sort-colors/ 3index,nums [0-zero] = 0,nums [zero + 1,two-1] = 1,nums [two-n] = 2,用3个索引,nums [0-zero] = 0,nums [零+1,two-1] = 1,nums [two-n] = 2,初始...
sortcolors: 对三种颜色的方块排序 water: 不同高度的台阶,能够蓄水多少 quicksort: 面试必考之一,快速排序 heapsort: 堆排序算法 B+tree: B+树 RedBlackTree:红黑树 AVLTree: 自平衡的二叉查找数 Dijkstra:最短...