[LintCode] Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Given [-1, -2, -3, 4, 5, 6]
, after re-range, it will be[-1, 5, -2, 4, -3, 6]
or any other reasonable answer.
You are not necessary to keep the original order of positive integers or negative integers.
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的更多相关文章
- Lintcode: Interleaving Positive and Negative Numbers 解题报告
Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-pos ...
- Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
- 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 ...
- LintCode Interleaving String
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2. Ex ...
- 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 ...
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
随机推荐
- IOS开发中滑动页面时NSTimer停止的问题
我们在做倒计时的时候,发现当你手指按着屏幕不放,拖动tableView滑动的时候,写在cell上得倒计时停止倒计时,松开继续倒计时.研究发现就是拖动tableView滑动时,NSTimer停止了. 这 ...
- 初学require.js
引入require.js,可以解决的问题: (1)实现js文件的异步加载,避免网页失去响应: (2)管理模板之间的依赖性,便于代码的编写和维护. 它的模块管理遵循AMD规范(Asynchronous ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(三)
主题:Service与Activity交互通信 问题的引出:现在有个需求,如果我们有一个下载任务,下载时间耗时比较长,并且当下载完毕后,需要更新UI的内容,这时,service中的bindServic ...
- HLG2062(make,heap问题)
最小的n个和 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 129(37 users) Total Accepted: 35(29 u ...
- 大数据之ETL设计详解
ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...
- PHP 遍历数组的方法汇总
1. foreach() foreach()是一个用来遍历数组中数据的最简单有效的方法. #example1: <?php $colors= array('red','blue','green' ...
- x:Name标记特性与Name属性
本文转载自silvergingko的专栏 在Xaml中定义了一个元素后,如果后面要使用该元素,则必须为该元素定义一个元素名称,在随后的Xaml中,通过元素名称来使用该元素. 在Xaml中,元素的名称定 ...
- Android 中的Force Close
今天写程序时遇到一个问题,领导希望在点击了setting里的force close 后,程序依然能够响应以前用alarmManager注册的receiver. 在网上看到了一些文章,写的是如何建立一个 ...
- MYSQL 删除字段值为NULL的语法
2014年9月1日 15:11:05 delete form your_table where your_field is null and your_field1 = '123' ...
- mysqldump备份
备份工具1.mysqldump(数据量很大时不推荐使用) myisam 锁表 innodb 行锁 mysqldump --help | less #查看mysql所有的语法 mysqldu ...