Lintcode144 Interleaving Positive And Negative Numbers solution 题解
来源:互联网 发布:考研网络培训班 编辑:程序博客网 时间:2024/06/18 13:27
【题目描述】
Given an array with positive and negative integers. Re-range it to interleaving with positive and negative integers.
Notice
You are not necessary to keep the original order of positive integers or negative integers.
给出一个含有正整数和负整数的数组,重新排列成一个正负数交错的数组。
【注】:不需要保持正整数或者负整数原来的顺序。
【题目链接】
www.lintcode.com/en/problem/interleaving-positive-and-negative-numbers/
【题目解析】
1.先用parition把数组分为左边为负数,右边为正数。
2.如果负数比较多,把多余的负数与尾部的值交换。(这样多余的数会放在数组的末尾)
3. left指向数组的左边,right指向数组的右边减掉多余的数。
4.第3步中,根据是正数多,还是负数多,起始位置要变一下。正数多,我们希望开始的是正数:
例如3 -1 2。负数多,我们希望开始的是负数,如-1 3 -2
5.不断交换left, right指针,并一次前进步长2.直到left, right相遇。
【参考答案】
www.jiuzhang.com/solutions/interleaving-positive-and-negative-numbers/
阅读全文
0 0
- Lintcode144 Interleaving Positive And Negative Numbers solution 题解
- Interleaving Positive and Negative Numbers
- Interleaving Positive and Negative Numbers
- Lintcode - Interleaving Positive and Negative Numbers
- [leetcode]Interleaving Positive and Negative Numbers
- lintcode:Interleaving Positive and Negative Numbers
- LintCode Interleaving Positive and Negative Numbers
- 代码面试题:Interleaving Negative and Positive Numbers 正负交替
- *[Lintcode]Interleaving Positive and Negative Numbers 交错正负数
- 144.Interleaving Positive and Negative Numbers-交错正负数(中等题)
- 1002 Positive and Negative
- 【HDU5183】Negative and Positive (NP)
- Lintcode29 Interleaving String solution 题解
- Counting positive and negative number and computing the average of numbers
- True Positive,True Negative,False Positive and False Negative
- True Positive,True Negative,False Positive and False Negative
- hdu 5183 Negative and Positive (NP)
- HDU 5183 Negative and Positive (NP)
- Opsive 的Third Person Controller使用手记(1)
- PAT乙级1029. 旧键盘(20)
- javascript执行上下文、作用域与闭包(第四篇)---作用域与执行上下文
- vijos1843(货车运输)
- Python实现RGB与HSI颜色空间的互换
- Lintcode144 Interleaving Positive And Negative Numbers solution 题解
- C++学习摘录(类型和声明)
- DetachedCriteria用法
- Android读书笔记(一)
- java编程实现学生成绩管理系统,采用顺序存储结构
- Linux中的命令
- POJ 3252-Round Numbers
- 软件工程_项目需求分析
- java for循环与if语句结合 通过辗转相除求 最大公约数和最小公倍数