75. Sort Colors - LeetCode Solution


Two Pointers Sort

Python Code:

class Solution:
    def sortColors(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """

        third = len(nums) -1
        one = 0
        second = 0

        while second<third +1:


            if nums[second] == 2:
                nums[second], nums[third] = nums[third], nums[second]
                third-=1

            elif nums[second] ==0:
                nums[second], nums[one] = nums[one], nums[second]
                one+=1

            second+=1


            
            
                
            
        


Comments

Submit
0 Comments
More Questions

1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest