[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 ...
随机推荐
- hibernate中几个接口作用
1.Configuration 类 Configuration 类负责管理 Hibernate 的配置信息,包括数据库的URL.用户名.密码.JDBC驱动类,数据库Dialect,数据库连接池等,其加 ...
- Linux questions
1.can not use ifconfig http://blog.csdn.net/zjt289198457/article/details/6918644 add this : export P ...
- 5款Linux最佳照片管理软件
在谈到 Linux 中的应用程序时,对于不同的用户.不同的使用场景以及不同的使用习惯,在同一类应用当中,总会有多种软件可供选择和备选.就 Linux 中的照片管理软件来说,相信此前的 Google P ...
- wordpress工作原理
WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...
- MVC ViewBag和ViewData的区别
在MVC3开始,视图数据可以通过ViewBag属性访问,在MVC2中则是使用ViewData.MVC3中保留了ViewData的使用.ViewBag 是动态类型(dynamic),ViewData 是 ...
- Swap Nodes & Reverse Nodes in k-Group
Swap Nodes | Given a linked list, swap every two adjacent nodes and return its head. Example Given 1 ...
- nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 56; cvc-complex-type.2.4.c通配符的匹配很全面, 但无法找到元素 'dubbo:application' 的声明
严重: Exception sending context initialized event to listener instance of class org.springframework.we ...
- python 列表转为字典的两个小方法
1.现在有两个列表,list1 = ['key1','key2','key3']和list2 = ['1','2','3'],把他们转为这样的字典:{'key1':'1','key2':'2','ke ...
- 2.saltstack笔记之目标,模块,返回写入数据库
作者:刘耀 QQ:22102107 一.目标(targeting Minions) 1.匹配Minions Id 匹配所有 (*) [root@node1 salt]# salt '*' test.p ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...