Leetcode練習(Python):數組類:第75題:給定一個包含紅色、白色和藍色,一共 n 個元素的數組,原地對它們進行排序,使得相同顏色的元素相鄰,並按照紅色、白色、藍色順序排列。 此題中,我們使用整數 0、 1 和 2 分別表示紅色、白色和藍色。 注意: 不能使用代碼庫中的排序函數來解決這道題。


題目:第75題:給定一個包含紅色、白色和藍色,一共 n 個元素的數組,原地對它們進行排序,使得相同顏色的元素相鄰,並按照紅色、白色、藍色順序排列。  此題中,我們使用整數 0、 1 和 2 分別表示紅色、白色和藍色。  注意: 不能使用代碼庫中的排序函數來解決這道題。  
思路:
思路較簡單,提示了進階思路

進階:

一個直觀的解決方案是使用計數排序的兩趟掃描算法。
首先,迭代計算出0、1 和 2 元素的個數,然后按照0、1、2的排序,重寫當前數組。
你能想出一個僅使用常數空間的一趟掃描算法嗎?

程序1:

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        length = len(nums)
        if length <= 0:
            return nums
        if length == 1:
            return nums
        num_0 = 0
        num_1 = 0
        num_2 = 0
        for index1 in range(length):
            if nums[index1] == 0:
                num_0 += 1
            elif nums[index1] == 1:
                num_1 += 1
            elif nums[index1] == 2:
                num_2 += 1
        for index2 in range(0, num_0):
            nums[index2] = 0
        for index3 in range(num_0, num_0 + num_1):
            nums[index3] = 1
        for index4 in range(num_0 + num_1, length):
            nums[index4] = 2
常數空間的方法:
這個超時了,之后的按照這個思想來改進
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        length = len(nums)
        if length <= 0:
            return nums
        if length == 1:
            return nums
        index1 = 0
        while index1 < length:
            if nums[index1] == 0:
                del nums[index1]
                nums = [0] + nums
                index1 += 1
            elif nums[index1] == 2:
                del nums[index1]
                nums = nums + [2]
                index1 = index1
            elif nums[index1] == 1:
                index1 += 1
        #return nums
改進的方法:
class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        length = len(nums)
        num_0 = 0
        num_2 = length - 1    
        index = 0
        while index <= num_2:
            if nums[index] == 0:
                nums[index], nums[num_0] = nums[num_0], nums[index]
                num_0 += 1
            elif nums[index] == 2:
                nums[index], nums[num_2] = nums[num_2], nums[index]
                index -= 1    
                num_2 -= 1
            index += 1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Leetcode練習(Python):數組類:第73題:給定一個 m x n 的矩陣,如果一個元素為 0,則將其所在行和列的所有元素都設為 0。請使用原地算法。 給定一個排序數組,你需要在原地刪除重復出現的元素 Leetcode練習(Python):數組類:第128題:給定一個未排序的整數數組,找出最長連續序列的長度。 要求算法的時間復雜度為 O(n)。 Leetcode練習(Python):數組類:第41題:給你一個未排序的整數數組,請你找出其中沒有出現的最小的正整數。你的算法的時間復雜度應為O(n),並且只能使用常數級別的額外空間。 JS對象 數組排序sort() sort()方法使數組中的元素按照一定的順序排列。 語法: arrayObject.sort(方法函數) Leetcode練習(Python):排序類:第179題:最大數:給定一組非負整數,重新排列它們的順序使之組成一個最大的整數。 Problem Description 有n(n<=100)個整數,已經按照從小到大順序排列好,現在另外給一個整數x,請將該數插入到序列中,並使新的序列仍然有序。 Input 輸入數據包含多個測試實例,每組數據由兩行組成,第一行是n和m,第二行是已經有序的n個數的數列。n和m同時為0標示輸入數 Leetcode練習(Python):數組類:第217題:給定一個整數數組,判斷是否存在重復元素。 如果任意一值在數組中出現至少兩次,函數返回 true 。如果數組中每個元素都不相同,則返回 false 。 Leetcode練習(Python):第448題:找到所有數組中消失的數字:給定一個范圍在 1 ≤ a[i] ≤ n ( n = 數組大小 ) 的 整型數組,數組中的元素一些出現了兩次,另一些只出現一次。 計算一個未排序數組中排序后相鄰元素的最大差值
 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM