Partition an array around an interger
Partition an array of integers around a value such taht all elements less than x come before elements greater than or equal to x.
Idea: Just use of subroutine PARTION of quicksort() with a slight modification.
array[start, end] to be partitioned around value x.
We need to additional pointers i and j, to maintain an invariant, which is true at all times:
array[start, i] stores elements less than x
array[i+1, j-1] stores elements greater than or equal to x
array[j, end] stores elements which haven't been explored yet.
void Partition(A, start, end){
int i = start - 1;
for(int j = start; j <= end; ++j){
if(A[j] < x){
i++;
swap A[i] with A[j];
}
}
}
Partition an array around an interger的更多相关文章
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- [Swift]LeetCode1013. 将数组分成和相等的三个部分 | Partition Array Into Three Parts With Equal Sum
Given an array A of integers, return true if and only if we can partition the array into three non-e ...
- LeetCode 1013 Partition Array Into Three Parts With Equal Sum 解题报告
题目要求 Given an array A of integers, return true if and only if we can partition the array into three ...
- Partition Array Into Three Parts With Equal Sum LT1013
Given an array A of integers, return true if and only if we can partition the array into three non-e ...
- LeetCode 1043. Partition Array for Maximum Sum
原题链接在这里:https://leetcode.com/problems/partition-array-for-maximum-sum/ 题目: Given an integer array A, ...
- 【leetcode】1043. Partition Array for Maximum Sum
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...
- 【leetcode】1020. Partition Array Into Three Parts With Equal Sum
题目如下: Given an array A of integers, return true if and only if we can partition the array into three ...
- 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Linux 命令 - history: 显示或操作历史列表
命令格式 history [-c] [-d offset] [n] history -anrw [filename] history -ps arg [arg...] 命令参数 -c 清除历史列表. ...
- centos6.5下磁盘创建交换分区
1.创建磁盘交换分区 2.创建文件交换分区
- Agile.Net 组件式开发平台 - 内核管理组件
敏捷开发体系 软件构件技术:所谓软件构件化,就是要让软件开发像机械制造工业一样,可以用各种标准和非标准的零件来进行组装.软件的构件化和集成技术的目标是:软件系统可以由不同厂商提供的,用不同语言开发 ...
- Linux进程调度
原文地址: http://cchxm1978.blog.163.com/blog/static/35428253201092910491682/ 相当不错的文章,读了后收藏,多谢博主分享! ----- ...
- 分享我写的IOCP:源码+思路
首先说明,下面的代码仅是一个IOCP的demo,很多地方的设计非常差,当然也有一些设计还算可以:).此篇仅供对IOCP有些了解但又不深入的.需要一个稍微完整示例的.对网络编程感兴趣的同学参考.点击这里 ...
- Mysql 数据库安装配置
MySQL的多种安装方法 在当今的互联网企业,Mysql数据服务几乎都是运行在LINUX系统操作系统上,当然你也可以在WINDOWS.UNIX等商业操作系统上运行. 但是一般企业都会采用LNMP.LA ...
- C++中执行windows指令
执行windows指令: BOOL ExecDosCmd(]) { SECURITY_ATTRIBUTES sa; HANDLE hRead,hWrite; sa.nLength = sizeof(S ...
- Java中的堆内存、栈内存、静态存储区
一.栈 栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器,当超过变量的作用域后,java会自动释放掉为该变量分配的内存空间,该内存空间可以立刻被另作他用.但缺点是,存在栈中的数据大小与生存 ...
- DTCMS展示一级栏目并展示各自栏目下的二级栏目
c#代码中 <!--C#代码--> <%csharp%> string parent_id=DTRequest.GetQueryString("parent_id&q ...
- MAC机中安装RUBY环境
在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY的环境进行一总结.安装Ruby环境首先需要安装Xcode然后需要安装Homebrew,接下来需要安装RVM最后安装Ruby环境 ...