Given an array of positive and negative integers, re-arrange it 
so that you have postives on one end and negatives on the other, 
BUT retain the original order of appearance.

For eg. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15

可以使用块交换技术实现题目要求,

1,7,-5 交换为 -5,1,7

然后

-5,1,7,9,-12 交换为 -5,-12,1,7,9

目测时间复杂度是o(n^2)

但是可以用递归将时间复杂度降维nlogn

http://haixiaoyang.wordpress.com/?s=Rearrange

有空可以研究下

//Given an array of positive and negative integers,
//re-arrange it so that you have postives on one end
//and negatives on the other, BUT retain the original order of appearance. do it in-place
//e.g. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15//When considering optimization from O(n^2) to O(nlogn),use divided & conquer
void swapRange(int a[], int nBeg, int nEnd)
{assert(a && nBeg <= nEnd);while (nBeg < nEnd)swap(a[nBeg++], a[nEnd--]);
}//solution:
//e.g. in any stage: ---- "+++ --" ++++, reverse middle part
// ==> ---- "--" "+++" ++++, reverse middle 2 parts seperatly to keep "stable"
void ReArrange(int a[], int n)
{assert(a && n > 0);if (n <= 1) return;ReArrange(a, n/2);ReArrange(a + n/2, n - n/2); //pitfall, notice both parametersint nLft = 0;while (a[nLft] < 0) nLft++;int nRgt = n-1;while (a[nRgt] >= 0)nRgt--;//Very important, no need to swap under this situation, returnif (nRgt <= nLft) return; swapRange(a, nLft, nRgt);int nBegRgt = nLft;while (a[nBegRgt] < 0) //no need to use "&& nBegRgt < n"nBegRgt++;swapRange(a, nLft, nBegRgt-1);swapRange(a, nBegRgt, nRgt);
}

Rearrange an array of positive and negative integers相关推荐

  1. Linux交叉编译问题strace解决 signalfd.c:15: xlat/sfd_flags.h:17: error: size of array 'type name' is negative

    产品不是很稳定,执行shell容易出现段错误,为了定位打算移植strace到嵌入式板子上,环境是MIPS平台,从github上下载的strace源码,编译ARM平台通过( ubuntu 12.04 交 ...

  2. 【雅思大作文考官范文】——第十二篇:'positive or negative' essay

    题目: Some universities now offer their courses on the Internet so that people can study online. Is th ...

  3. 使用XGBClassifier出现Dataset is empty, or contains only positive or negative samples.错误

    问题: 之前在lightgbm中使用了早停early_stopping参数,于是我也想在xgboost中使用类似的方法进行早停,从其他地方找到的代码大致都是这样写的: from xgboost imp ...

  4. 4kyu Sum by Factors

    4kyu Sum by Factors 题目背景: Given an array of positive or negative integers I= [i1,-,in] you have to p ...

  5. hdu 1081To The Max

    http://acm.hdu.edu.cn/showproblem.php?pid=1081 题意:求子矩阵的和的最大值 思路:把多维转化为一维,只要会一维的就简单了... To The Max Ti ...

  6. Maximum Sum UVA - 108(连续子序列最大和—变形之子矩阵最大和)

    题目大意:给出 n*n 的矩阵,找每隔数字之和最大的子矩阵,输出最大和.  解题思路:枚举矩阵左上和右下的坐标,分别合并子矩阵的每列,使得二维转化为一维,然后利用连续子序列最大和去做就行. Time ...

  7. 【POJ - 1050】To the Max (dp)

    题干: Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguo ...

  8. java 打印不换行_Swift的print不换行打印的方法

    github上写blog http://www.elbow95.me/blog/My-First-Blog-Essay.html 阮一峰的: http://www.ruanyifeng.com/blo ...

  9. POJ1050-To the Max

    描述: Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguo ...

最新文章

  1. java设计模式-适配器模式
  2. java中this关键词
  3. POJ-3414 Pots BFS+记忆路径
  4. 【Linux】一步一步学Linux——od命令(266)
  5. 三种安防监控摄像机供电方式,如何合理选择?
  6. 计算机2013知识,2013年全国计算机一级考试B基本知识点五
  7. LeetCode 1381. 设计一个支持增量操作的栈(deque/数组)
  8. java请求百度短链接_长链接生成短链接Java源码(调用百度接口)
  9. 《C++ 进阶心法》书籍修正记录
  10. 安装sqlyog和使用注册码
  11. Detours学习之十二:Detours API用于修改二进制文件的api
  12. mac 截图工具只能截取桌面问题
  13. 终于搞清前端和后端的区别啦!
  14. CAD多行文本中文字的堆叠
  15. tableau 集动作_Tableau训练营:7天,每天30分钟,零基础get数据可视化基本姿势
  16. CMD命令全集(转)
  17. 如何安装使用Oracle10g
  18. 国外流行网上支付方式
  19. hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container
  20. 英国Foresight太阳能基金卖股集资 投资200MW光伏项目

热门文章

  1. java compareandset 包_在Java中,AtomicInteger compareAndSet()和synced关键字的性能如何?...
  2. 推荐几个出论文的好方向
  3. AAAI 2021 | 幻灯片中文字的重要性预测赛亚军DeepBlueAI团队技术分享
  4. 一文详解超参数调优方法
  5. 直播预告:基于强化学习的关系抽取和文本分类 | PhD Talk #18
  6. Object类与Objects类总结
  7. poj3254 Corn Fields 状压DP入门
  8. 十进制小数转换为二进制
  9. SpringBoot 集成 clickhouse + mybatis-plus 配置及使用问题说明(含建表语句、demo源码、测试说明)
  10. Java 调用接口工具类并设置请求和传输超时时间