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]之 ...
随机推荐
- Python之路【第四篇补充】:面向对象初识和总结回顾
面向过程的编程 面向过程:根据业务逻辑从上到下写垒代码! 例子: 需求一.有一个程序需要做身份认证: 用户名有个字典: #定义一个用户名信息字典 user_info = { "zhangsa ...
- Jquery中$.load(),$.get(),$.post(),$.ajax(),$.getJSON()的作用与不同
这个五个都是获取页面或者数据的方法.. 都是基于Ajax协议的.. $.get(url,[data],[callback]) //描述: 从服务器加载数据,请求方式为GET. url ...
- 网页JQ基础之jq-隐藏以及显示特效
简单的 隐藏以及显示的 JQ 的代码如下: <!DOCTYPE html> <html> <head> <script src="/jquery/j ...
- C#创建windows服务列表
转载自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html Windows Service这一块并不复杂,但是注意事项太多了,网上 ...
- 关于外部引用JS,中文乱码的问题
asp.net 页面默认编码为UTF-8, 如果js嵌套写在asp.net中,不会导致中文乱码,因为他们具有相同的编码 外部引用js由于编码格式与asp.net的编码不同,javascript编码默认 ...
- nginx专题
1.Nginx和php性能优化相关 专家向磊http://slaytanic.blog.51cto.com/2057708/1173021 2.Puppet利用Nginx多端口实现负载均衡http:/ ...
- Ubuntu 14 修改默认打开方式
通过研究,有三种修改方式. 方式一: 修改路径:右上角“系统设置” -> 详细信息 -> 默认应用程序 但是,有个缺陷,可修改的项比较少. 方式二: 例如,修改pdf的打开方式,只要查看任 ...
- C#之类与对象
这段代码告诉我们要把#define DEBUG放在文件的开头位置,不然会导致编译错误,最后还要#endif 以上代码告诉我们可以对自己创建的类设计自己的构造方法,然后可以通过具体的Main()函数来通 ...
- 关于css3的动画总结
旋转:transform:rotate(xxdeg)扭曲:transform:skey(x,y)缩放:transform:scale(x,y)变形位移:transform:translate(x,y) ...
- JQuery在asp.net中三种ajax传值
1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释 2)通过aspx.cs文件中的静态方法 3)通过aspx文件 ...