将正数放在负数之前 [英] Place positive numbers before negative
问题描述
所以我有这段代码,我在互联网上找到了该代码,该代码接受一个负数和正数的数组并重新排列该数组,以便所有负数都在正数之前.但是每个数字的出现位置必须保持不变.因此,例如,如果我有 -2 5 -9 .....
,则在组织数组中 -2
仍必须是 first 负的数量和 -9
的数量必须是负的数量的 second ,也是5
必须是阳性的第一个号.
So I have this code which I found on the internet which takes an array of negative and positive numbers and rearranges the array so all negative numbers are before the positive ones. But the position of appearance of each number has to remain the same. So for example if i have -2 5 -9 .....
, in the organized array -2
still has to be the first number of the negative ones and -9
has to be the second of the negative ones, also 5
has to be the first number of the positive ones.
示例:
Input: {1,7,-5,9,-12,15,-6,-8}
Output:{-5,-12,-6,-8,1,7,9,15}
所以代码可以正常工作,但是我要做的是更改它,以便在负数之前出现正数.
So the code is working correctly but what I want to do is to change it so positive numbers appear before the negative ones.
示例:
Input: {1,7,-5,9,-12,15,-6,-8}
Output:{1,7,9,15,-5,-12,-6,-8}
如果您可以提供帮助,请提供以下代码,我知道它不是很复杂,但是我无法弄清楚.
Here's the code please if you can help me, I know that it's not very complicated but I can't figure it out.
// Java program to Rearrange positive and negative
// numbers in a array
public class Rearrangement {
/* Function to print an array */
static void printArray(int[] A, int size) {
for (int i = 0; i < size; i++)
System.out.print(A[i] + " ");
System.out.println("");
}
/* Function to reverse an array. An array can be
reversed in O(n) time and O(1) space. */
static void reverse(int[] arr, int l, int r) {
if (l < r) {
arr = swap(arr, l, r);
reverse(arr, ++l, --r);
}
}
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
static void merge(int[] arr, int l, int m, int r) {
int i = l; // Initial index of 1st subarray
int j = m + 1; // Initial index of IInd
while (i <= m && arr[i] < 0)
i++;
// arr[i..m] is positive
while (j <= r && arr[j] < 0)
j++;
// arr[j..r] is positive
// reverse positive part of
// left sub-array (arr[i..m])
reverse(arr, i, m);
// reverse negative part of
// right sub-array (arr[m+1..j-1])
reverse(arr, m + 1, j - 1);
// reverse arr[i..j-1]
reverse(arr, i, j - 1);
}
// Function to Rearrange positive and negative
// numbers in a array
static void RearrangePosNeg(int[] arr, int l, int r) {
if (l < r) {
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = (l + r) / 2;
// Sort first and second halves
RearrangePosNeg(arr, l, m);
RearrangePosNeg(arr, m + 1, r);
merge(arr, l, m, r);
}
}
static int[] swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args) {
int[] arr = {1, 7, -5, 9, -12, 15, -6, -8};
int arr_size = arr.length;
RearrangePosNeg(arr, 0, arr_size - 1);
printArray(arr, arr_size);
}
}
推荐答案
您可以 concat
.这大大简化了您的代码:
You can filter
two substreams from this array: positive and negative, and then concat
them. This greatly simplifies your code:
public static void main(String[] args) {
int[] arr = {1, 7, -5, 9, -12, 15, -6, -8};
System.out.println(Arrays.toString(rearrangePosNeg(arr)));
// [-5, -12, -6, -8, 1, 7, 9, 15]
}
public static int[] rearrangePosNeg(int[] arr) {
return IntStream.concat(
Arrays.stream(arr) // negative
.filter(i -> i < 0),
Arrays.stream(arr) // positive
.filter(i -> i >= 0))
.toArray();
}
If you want to swap the positive and negative substreams, you can swap the parts of the filter
s: i < 0
and i >= 0
.
另请参见:仅使用一个分号在Java中旋转int数组
这篇关于将正数放在负数之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!