# Interleaving Positive And Negative Numbers

## Interleaving Positive And Negative Numbers

## 题目地址

<https://www.lintcode.com/problem/interleaving-positive-and-negative-numbers>

## 题目描述

```
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
```

## 代码

```java
public class Solution {
    public void rearrange(int arr[], int n) {
    int i = -1, temp = 0;
      // 1. 先把负数放前
    for (int j = 0; j < n; j++) {
      if (arr[j] < 0) {
        i++;
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }
    }

    int pos = i + 1, neg = 0;
        // 2. 把正数往前面负数群中交换
    while (pos < n && neg < pos && arr[neg] < 0) {
      temp = arr[neg];
      arr[neg] = arr[pos];
      arr[pos] = temp;
      pos++;
      neg += 2;
    }
  }

}
```


---

# 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://wentao-shao.gitbook.io/leetcode/array/interleaving-positive-and-negative-numbers.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.
