-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort Colors Leetcode.py
More file actions
39 lines (31 loc) · 1.23 KB
/
Sort Colors Leetcode.py
File metadata and controls
39 lines (31 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# class Solution:
# def sortColors(self, nums: List[int]) -> None: # method 1 --> t=O(n2)
# ini_index=0
# for i in range(0,3):
# length=len(nums)
# for j in range(0,length):
# if(nums[j]==i):
# nums[j],nums[ini_index]=nums[ini_index],nums[j]
# ini_index=ini_index+1
# return nums
class Solution:
def sortColors(self, nums: List[int]) -> None: # method 2 -- O(n) [ single traversal]
"""
Do not return anything, modify nums in-place instead.
"""
l=len(nums)
low=0
mid=0
high=l-1
while(mid<=high):
if nums[mid]==0:
nums[low],nums[mid]=nums[mid],nums[low]
low=low+1
mid=mid+1
elif(nums[mid]==1):
mid=mid+1
else:
nums[mid],nums[high]=nums[high],nums[mid]
high=high-1
return nums
# another method will be traverse through array and count number of 0,1,2 and place them indexvise [ 2 traversal]