【leetcode】75. Sort Colors
生活随笔
收集整理的這篇文章主要介紹了
【leetcode】75. Sort Colors
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目如下:
解題思路:我的解題思路是遍歷數組,遇到0刪除該元素并插入到數組頭部,遇到1則不處理,遇到2刪除該元素并插入到數組尾部。
代碼如下:
class Solution(object):def sortColors(self, nums):""":type nums: List[int]:rtype: void Do not return anything, modify nums in-place instead."""inx = 0nums += ['#']while inx < len(nums):if nums[inx] == 0:del nums[inx]nums.insert(0,0)inx += 1elif nums[inx] == 2:del nums[inx]nums.append(2)elif nums[inx] == '#':del nums[inx]breakelse:inx += 1?
轉載于:https://www.cnblogs.com/seyjs/p/9617370.html
總結
以上是生活随笔為你收集整理的【leetcode】75. Sort Colors的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 环境下 jdk1.8 mav
- 下一篇: Sysbench 1.0.15安装及使用