Interleaving Positive and Negative Numbers

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

Note: You are not necessary to keep the original order of positive integers or negative integers.

Example

Given [-1, -2, -3, 4, 5, 6], after re-range, it will be[-1, 5, -2, 4, -3, 6] or any other reasonable answer.

分析:

其实这题思路还是很简单的,先用partition方法把数组分成两队,左边的数是小于0的数,右边的数是大于0的数。

然后,我们需要把正数插入到负数里,但是之前我们要确认正数负数哪个更多。多的要放在第一个位置。

 1 class Solution {
 2     /**
 3      * @param A: An integer array.
 4      * @return: void
 5      * cnblogs.com/beiyeqingteng/
 6      */
 7     public void rerange(int[] A) {
 8         if (A == null || A.length <= 2) return;
 9         int pp = partition(A);  //positive number starting posisition
10         int np = 0; // negatie number starting position
11         if (A.length / 2 < pp) {
12             np++;
13         }
14         // put positive numbers into negative numbers
15         while (np <= A.length - 1 && pp <= A.length - 1) {
16             swap(A, np, pp);
17             np = np + 2;
18             pp++;
19         }
20    }
21
22    // move negative to left
23    private int partition(int[] A) {
24        int p = 0;
25        for (int i = 0; i < A.length; i++) {
26            if (A[i] < 0) {
27                swap(A, i, p);
28                p++;
29            }
30        }
31        return p;
32    }
33
34    private void swap(int[] A, int i, int j) {
35        int temp = A[i];
36        A[i] = A[j];
37        A[j] = temp;
38    }
39 }

转载请注明出处: cnblogs.com/beiyeqingteng/

时间: 2024-07-30 21:47:48

Interleaving Positive and Negative Numbers的相关文章

Lintcode: Interleaving Positive and Negative Numbers 解题报告

Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-positive-and-negative-numbers/ Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. 注意 Yo

[leetcode]Interleaving Positive and Negative Numbers

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note You are not necessary to keep the original order or positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6],

LintCode-Interleaving Positive and Negative Numbers.

Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers. Note You are not necessary to keep the original order or positive integers or negative integers. Example Given [-1, -2, -3, 4, 5, 6],

True Positive|True Negative|False Positive|False Negative

True Positive|True Negative|False Positive|False Negative 表示分类正确: True Positive:本来是正样例,分类成正样例. True Negative:本来是负样例,分类成负样例. 表示分类错误: False Positive :本来是负样例,分类成正样例,通常叫误报. False Negative:本来是正样例,分类成负样例,通常叫漏报. true positive rate = true positive / (true po

计算机中负数的表示法 Twos complement: Negative numbers in binary

Twos complement: Negative numbers in binary 二进制的负数表示法 负数的表示法: 我们将第一位定义为符号位 ,1代表负数 0代表正数 计算5+(-5) 结果是2 it doesn’t work, 下面我们来介绍 one’s complement 在one’s complement中进行相加: 结果都差了个1, two’s complement (将 -0 去掉了) 相加的结果也是正确的 two’s complement中,位对应的值也是make sens

1351. Count Negative Numbers in a Sorted Matrix

Given a m * n matrix grid which is sorted in non-increasing order both row-wise and column-wise. Return the number of negative numbers in grid. Example 1: Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] Output: 8 Explanation: There ar

bc#32-2 Positive and Negative

http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=570 给定一个数组(a0,a1,a2,?an−1)和一个整数K, 请来判断一下是否存在二元组(i,j)(0≤i≤j<n)使得 NP−sum(i,j) 刚好为K.这里NP−sum(i,j)=ai−ai+1+ai+2+?+(−1)^(j−i)*aj. 分基数和偶数讨论 开始用set做tle 后来改成哈希表+输入优化就好多了 线性探测和二次探测都大约

Flooded!

Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5955   Accepted: 1800   Special Judge Description To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients with the elevation of each 10-meter by 10

Creating fields using CSOM

? When creating a field, whether you are using CAML, server-side object mode, or one of the client-side object models, you typically specify the following attributes: The?ID?of the fied, which is a GUID The?Name, which is the internal name of the fie