File: root - text - article - 2019 - 12 - sort-colors.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 731 Views, 17670 Search Bots | 118 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 731 Views, 17670 Search Bots | 118 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Apple:
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.
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.
Can you do this in a single pass?
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 731 Views, 17670 Search Bots | 118 Words 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.
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.
Can you do this in a single pass?
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Here's a starting point:
class Solution:
def sortColors(self, nums):
# Fill this in.
nums = [0, 1, 2, 2, 1, 1, 2, 2, 0, 0, 0, 0, 2, 1]
print("Before Sort: ")
print(nums)
# [0, 1, 2, 2, 1, 1, 2, 2, 0, 0, 0, 0, 2, 1]
Solution().sortColors(nums)
print("After Sort: ")
print(nums)
# [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2]
Related Articles
- Daily Interview Problem: Group Words that are Anagrams
- Daily Interview Puzzle: Intersection of Linked Lists
- Algorithm Interview: Permutations of numbers
- Find Missing Numbers in an Array
- Daily Interview Problem: Given two arrays, write a function to compute their intersection.
- Daily Interview Problem: Min Stack
- Autorun.inf Virus Protection
- Progess made
- YES!!
- Algorithm Interview: Determine If Linked List is Palindrome
©2006~2024 SteakOverCooked - 0.00719 Seconds(s) - 2801.246 KB/s - 96 Online Memory: 493.2 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !