# 625.Partition Array II

## 1.Description(Medium)

Partition an unsorted integer array into three parts:

1. The front part < *low*
2. 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)

[**Challenge**](https://www.lintcode.com/en/problem/partition-array-ii/#challenge)

1. Do it in place.
2. 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的变化。

```
 public void partition2(int[] nums, int low, int high) {
        if(nums==null || nums.length==0){
            return;
        }
        int left=0;
        int right=nums.length-1;
        int i=0;
        while(i<=right){
            if(nums[i]<low){
                swap(nums,i,left);
                i++;
                left++;               
            }
            else if(nums[i]>high){
                swap(nums,i,right);
                right--;
            }else{
                i++;
            }
        }
    }

    public void swap(int[] colors,int i,int j){
        int temp=colors[i];
        colors[i]=colors[j];
        colors[j]=temp;
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://junnie.gitbook.io/nine-chapter/7.two-pointers/625partition-array-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
