Given an array with positive and negative integers. Re-range it to interleaving with positive and 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.

Note

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

Challenge

Do it in-place and without extra memory.

class Solution {
public:
/**
* @param A: An integer array.
* @return: void
*/
void rerange(vector<int> &A) {
int n = A.size();
if(n < ) return; int posNum = , negNum = , posIdx = , negIdx = ;
for(size_t t = ;t < n;++t){
if(A[t] >= ) ++posNum;
else ++negNum;
} if(negNum > posNum){
negIdx = ;
posIdx = ;
} while(posIdx < n && negIdx < n){
while(posIdx < n && A[posIdx] >= ) posIdx += ;
while(negIdx < n && A[negIdx] < ) negIdx += ;
if(posIdx < n && negIdx < n) swap(A[posIdx],A[negIdx]);
}
} };

[LintCode] 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. 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. LintCode Interleaving String

    Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...

  5. lintcode: Check Sum of Square Numbers

    Check Sum of Square Numbers Given a integer c, your task is to decide whether there're two integers ...

  6. 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 ...

  7. [LintCode]——目录

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

  8. Java Algorithm Problems

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

  9. 二分难题 && deque

    141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...

随机推荐

  1. Java中的异常

    一.什么是异常 异常就是在程序的运行过程中所发生的不正常的事件,如所需文件找不到,网络连接不通或中断,算术运算出错(如被0除),数组下标越界,装载了一个不存在的类,对null的操作,类型转换异常等等. ...

  2. PowerDesigner15在win7-64位系统下对MySQL反向工程

    由于机器是win64位的,下载的64的connector安装测试成功,但是在powerdesigner中测试连不上,总算在下面这边博友中找到解决方案! http://blog.csdn.net/web ...

  3. HDU 4858 项目管理(邻接表 暴力模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的! 两个节点间可 ...

  4. IOS 页面之间的跳转

    1.UINavigationController popToViewController 对应popViewControllerAnimated: 也可以使用: [self.navigationCon ...

  5. iPhone取消软件更新上边的1

    去除设置的更新+1小红点提示主要分为越狱和非越狱设备两种方法. 越狱状态下方法: 首先将你的设备进行越狱: 越狱后安装ifile(这个自行搜索安装): 用ifile打开/System/Library/ ...

  6. SSDB 数据库如何换用 rocksdb 引擎?

     牧童遥指杏花村,一枝红杏出墙来… SSDB 数据库如何换用 rocksdb 引擎? idea's blog 2014-04-12 71 阅读 rocksdb NoSQL SSDB 数据库使用的是 G ...

  7. spring+quartz报错:Table 'BANKSTEELERP_OLD.QRTZ_TRIGGERS' doesn't exist

    spring3.2.8 + quartz2.2.1配置到application.xml中 org.springframework.beans.factory.BeanCreationException ...

  8. process vs thread

    process vs thread http://blog.csdn.net/mishifangxiangdefeng/article/details/7588727 6.进程与线程的区别:系统调度是 ...

  9. 利用 FFmpeg 和 ImageMagick, AVI 转 GIF(不失真)

    利用[TMPGEnc 4.0 XPress] 或 [TMPGEnc Video Mastering Works 5] 生成 AVI 这个视频编辑软件,可对每个帧进行操作 1.生成每个帧的 PNG ff ...

  10. hdu 1050 Moving Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1050 这道题目隔了很久才做出来的.一开始把判断走廊有重叠的算法都想错了.以为重叠只要满足,下一次mov ...