625.Partition Array II
1.Description(Medium)
Partition an unsorted integer array into three parts:
The front part < low
The middle part >= low & <= high3.
3.The tail part > high
Return any of the possible solutions.
Notice
low <= high in all test cases.
Example
Given[4,3,4,1,2,3,1,2]
, and low =2
and high =3
.
Change to[1,1,2,3,2,3,4,4]
.
([1,1,2,2,3,3,4,4] is also a correct answer, but [1,2,1,2,3,3,4,4] is not)
Do it in place.
Do it in one pass (one loop).
2.Code
跟sort colors II 非常相似,只不过sort color II 里面的上下限需要不断变化,这个题目是固定的low high
在代码里的变化就是sort colors II 在while(i<=right)外层还有一个while(min<max)的一层,在这一层里,控制最后结束时i要变回当前left的位置,以及min 和max的变化。
Last updated