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. MVC中的@section

    在前文<MVC中 _ViewStart _Layout Index三个页面中的加载顺序> 中另外指定母版页 Layout.cshtml时...遇到了这个问题.. 报错: 以下各节已定义,但 ...

  2. Struts2验证框架实例

    今天写了个Struts验证框架的实例,总算把验证框架弄清楚了. 上一篇Struts实例的action没有继承ActionSupport类,虽然也可以实现action的功能,但是却不能应用Struts提 ...

  3. 发布自己的包到Nuget上

    1.首先下载Nuget.exe https://dist.nuget.org/index.html 2.设置环境变量  设置apikey nuget setApiKey <my_api_key& ...

  4. regsvr32的使用

    注册器是: DllRegisterServer 命令就是: regsvr32 不是regsrv32.

  5. Hadoop集群搭建

    配置IP,修改主机名,关闭防火墙,配置SSH免密码登录

  6. Linux下的一个图形管理工具webmin

    这个工具其实我在两年前的小白时期还是经常用的,因为那时候对Linux比较陌生在为数server的时候帮了我很多工作,现在周末外面下雨,闲来无事莫名其妙的想起他来. 工具优点:最大特点是他是脚本安装 不 ...

  7. ExtJS -- Grid 文本居中显示

    Reference: http://stackoverflow.com/questions/6561120/extjs-4-apply-defaults-to-all-columns-in-a-gri ...

  8. 利用CSS实现带相同间隔地无缝滚动动画

    说明:因为在移动上主要利用CSS来做动画,所以没有考虑其他浏览器的兼容性,只有-webkit这个前缀,如果需要其他浏览器,请自行补齐. 首先解释一下什么是无缝滚动动画, 例如下面的例子 See the ...

  9. 关于IE7 默认有边框的解决方案

    这个问题出现在IE7中,因为body有默认的border.这个原因是由于声明引起的. 加了这个头就可以了 * {border:0;} 以上的 CSS 在 XHTML 下是无效果的,将 DOCTYPE ...

  10. Tomcat 6 —— Realm域管理

    本篇来源于官方文档,但不仅仅是翻译,其中不乏网上搜索的资料与自己的理解. 如有错误,请予指正. 什么是Realm 首先说一下什么是Realm,可以把它理解成“域”,也可以理解成“组”,因为它类似 类U ...