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], after re-range, it will be [-1, 5, -2, 4, -3, 6] or any other legal answer.

Challenge

Do it in-place and without extra memory.

Analysis:

We need figure how to address the cases that one kind of numbers is more than the other kind of numbers.

In LintCode, if postive numbers are more than negtive numbers, the array then start with positive numbers.

Solution:

 1 class Solution {
 2     /**
 3      * @param A: An integer array.
 4      * @return an integer array
 5      */
 6     public int[] rerange(int[] A) {
 7         if (A.length==0 || A.length==1) return A;
 8
 9         int posNum = 0;
10         for (int i : A) if (i>=0) posNum++;
11         int negP = (posNum>(A.length-posNum)) ? 1 : 0;
12         int posP = (posNum>(A.length-posNum)) ? 0 : 1;
13
14     int p = A.length-1;
15     while (negP<A.length && A[negP]<0) negP += 2;
16     while (posP<A.length && A[posP]>=0) posP += 2;
17     while (p>=posP || p>=negP){
18         if (A[p]<0){
19             if (negP>=A.length) p--;
20             else {
21                 int temp = A[negP];
22                 A[negP] = A[p];
23                 A[p] = temp;
24                 negP += 2;
25             }
26         } else {
27             if (posP >= A.length) p--;
28             else {
29                 int temp = A[posP];
30                 A[posP] = A[p];
31                 A[p] = temp;
32                 posP += 2;
33             }  
34         }
35     }
36
37     return A;
38
39      }
40 }
时间: 2024-10-18 02:43:09

LintCode-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

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]

[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],

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

LintCode Interleaving String

Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Example For s1 = "aabcc", s2 = "dbbca" When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. For

lintcode 中等题:Ugly Numbers 丑数

题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */

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 后来改成哈希表+输入优化就好多了 线性探测和二次探测都大约