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

猜您喜欢...

  1. Daily Interview Problem: Maximum Profit From Stocks
  2. Generate All IP Addresses
  3. Spectrum Master
  4. Windows Scripting
  5. Patterns for breaking down questions you haven
  6. Algorithm Interview: Level Order Traversal of Binary Tree
  7. Staying on a Chess Board
  8. Daily Interview Problem: Running Median
  9. [Daily Problem] Longest Palindromic Substring
  10. Design Tic-Tac-Toe

评论 (0)

    当前页暂时没有评论。


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