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的数。

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

 class Solution {
/**
* @param A: An integer array.
* @return: void
* cnblogs.com/beiyeqingteng/
*/
public void rerange(int[] A) {
if (A == null || A.length <= ) return;
int pp = partition(A); //positive number starting posisition
int np = ; // negatie number starting position
if (A.length / < pp) {
np++;
}
// put positive numbers into negative numbers
while (np <= A.length - && pp <= A.length - ) {
swap(A, np, pp);
np = np + ;
pp++;
}
} // move negative to left
private int partition(int[] A) {
int p = ;
for (int i = ; i < A.length; i++) {
if (A[i] < ) {
swap(A, i, p);
p++;
}
}
return p;
} private void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}

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

Interleaving Positive and Negative Numbers的更多相关文章

  1. Lintcode: Interleaving Positive and Negative Numbers 解题报告

    Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-pos ...

  2. [LintCode] Interleaving Positive and Negative Numbers

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

  3. Facebook Gradient boosting 梯度提升 separate the positive and negative labeled points using a single line 梯度提升决策树 Gradient Boosted Decision Trees (GBDT)

    https://www.quora.com/Why-do-people-use-gradient-boosted-decision-trees-to-do-feature-transform Why ...

  4. plink修改正负链(--flip, change the positive and negative stand)

    修改正负链用到的参数为--flip 假定trial.bim的内容如下: trial.bim 1 rs142578063 0 732746 G A 1 rs144022023 0 732801 G A ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  7. Flooded!

    Flooded! Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5955   Accepted: 1800   Specia ...

  8. MOOCULUS微积分-2: 数列与级数学习笔记 Review and Final

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

  9. Hadoop学习笔记(2)

    Hadoop序列化:Long 和Int---变长编码的方法: 如果整数在[ -112, 127] ,所需字节数为1,即第一个字节数就表示该值. 如果大于127,则第一个字节数在[-120,-113]之 ...

随机推荐

  1. python递归理解图

    递归:下一级只能return给自己的上一级. import re val="9-2*5/3+7/3*99/4*2998+10*568/14" val="9-2*5/3+7 ...

  2. 单选框的回显c:if

    <input type="radio" name="sex" value="boy" <c:if test="${te ...

  3. STM32堆栈溢出

    在使用STM32读取SD Card的文件时,总是会卡死在读函数那里 res = f_read(&fsrc, gbuffer, sizeof(gbuffer)-1, &br); 而且出现 ...

  4. Ajax 局部刷新

    方式一:function hits1(troops) {    var troops = troops;    var ajax=Ajax();    var url = 'xxx.php';    ...

  5. ASP.NET MVC5 Filter重定向问题

    ASP.NET MVC5 Filter重定向问题 一.问题描述 1.在Filter中使用直接filterContext.RequestContext.HttpContext.Response.Redi ...

  6. gradle 默认属性

    Properties(未翻译) Property Description allprojects 包含该项目及其子项目的属性 ant The AntBuilder for this project. ...

  7. firefox如何卸载插件plugins和临时文件夹

    下载原版的 英文版的 firefox 会看到 openH264 video codec Plugin和microsoft DRM (digit rightcopy manager 数字版权管理)等等插 ...

  8. jquery客户端验证插件

    http://www.cnblogs.com/masing/articles/2157420.html http://www.oschina.net/p/jquery+formvalidator ht ...

  9. 10条PHP编程习惯助你找工作

    过去的几周对我来说是一段相当复杂的经历.我们公司进行了大裁员,我是其中之一,但却体验到了其中的乐 趣.我从来没有被开除过,所以很难不去想得太多.我开始浏览招聘板块,一个全职PHP程序员的职位很吸引人, ...

  10. 【翻译】Tomcat 6.0 部署与发布

    本篇参考Tomcat官方文档:<First Webapp>翻译,并结合自己的开发经验介绍关于tomcat部署以及发布的相关内容. 1 目录结构 在tomcat中所有的应用都是放置在CATA ...