页面顶部 Top
文件:  root - text - article - 2019 - 12 - sort-colors.txt.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 520 次阅读, 14070 次搜索 | 118 个单词

定阅此目录的博客 | 浏览 | 博客存档
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:


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]
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 520 次阅读, 14070 次搜索 | 118 个单词 定阅此目录的博客

猜您喜欢...

  1. Palindrome Integers
  2. Maximum In A Stack
  3. [Daily Problem] Validate Balanced Parentheses
  4. First and Last Indices of an Element in a Sorted Array
  5. Daily Interview Question: Find Cycles in a Graph
  6. [Daily Problem] Longest Palindromic Substring
  7. Spreadsheet Columns
  8. Longest Substring Without Repeating Characters
  9. Reverse a Linked List
  10. Most Frequent Subtree Sum

评论 (0)

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="索引, 跟踪">