Interleaving Positive and Negative Numbers
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的更多相关文章
- Lintcode: Interleaving Positive and Negative Numbers 解题报告
Interleaving Positive and Negative Numbers 原题链接 : http://lintcode.com/zh-cn/problem/interleaving-pos ...
- [LintCode] 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 ...
- 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可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Flooded!
Flooded! Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5955 Accepted: 1800 Specia ...
- MOOCULUS微积分-2: 数列与级数学习笔记 Review and Final
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- Hadoop学习笔记(2)
Hadoop序列化:Long 和Int---变长编码的方法: 如果整数在[ -112, 127] ,所需字节数为1,即第一个字节数就表示该值. 如果大于127,则第一个字节数在[-120,-113]之 ...
随机推荐
- Autolayout学习(1)-了解Autoreszing
1. 为什么要有Autoreszing? 在Xcode6之前,如果定义了下面的一个布局,同时运行在不同尺寸设备下会显示不同的效果. (iPhone6-4.7inch) (iPhone5s-4inch) ...
- 如何使用jquery - ui 的图标icons 及图标的相对位置 +jquerui是如何来显示图标的?
1. 只需要引入 jquery-ui 的主css文件: jquery-ui.css 文件?? 不需要 引入 jquery-ui-structure/theme.css文件??? ,,,,, 2. 一定 ...
- linux 打造man中文帮助手册
博客转自:http://my.oschina.net/hbzhangmao/blog/354533 学IT的同学都知道, Linux是一个好东西, 但初学者往往会因为太多的命令觉得头疼, 更头疼的是所 ...
- 配置lamp中的apache
root@komiles-VirtualBox:/etc/apache2/sites-available# service apache2 restart * Restarting web serve ...
- getStyle(),修改样式属性
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ASO优化总结(基于网络分享的知识总结归纳)
如何优化应用标题? 注意关键字的长度,尽量保证每一个关键字小于10个字符.保持快速更新,因为每次更新,你都将有机会删除表现不佳的关键字以 及增添新的关键字.在ASO中使用关键字的正确做法 标题,并非越 ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- iOS设备 屏幕尺寸、操作系统、摄像头像素、发行时间 汇总
设备 硬件尺寸 软件尺寸 精密程度 操作系统 摄像头像素 发行时间 iPhone 4s 640 x 960 320 x 480 2x iOS 5 800万 2011.10.04 iPhone 5 64 ...
- Web服务精讲–搭个 Web 服务器(二)
导读 曾几何时,你所选择的 Python Web 框架会限制你所可选择的 Web 服务器,反之亦然.如果某个框架及服务器设计用来协同工作的,那么一切正常. 在第一部分中,我提出了一个问题:“如何在你刚 ...
- Unity手游之路<四>3d旋转-四元数,欧拉角和变幻矩阵
http://blog.csdn.net/janeky/article/details/17272625 今天我们来谈谈关于Unity中的旋转.主要有三种方式.变换矩阵,四元数和欧拉角. 定义 变换矩 ...