The-ith-Element
Brief: the-ith-element,given a array A with n element , return the i-th element of A. A(n,i)
this problem can be solved using quick-sort idear, every time we choose a pivot ,after rearrangement, we know the pivot is the i-th element of A, so we can fixed this problem as the follow action.
i) choose pivot, rearrangement array, get the pivot index of j;
ii) if j > i , then recursion the 1-th partition , A(1-th , i);
iii)if j < i, then recursion the 2-th partition, A(2-th, i-j);
int element(int *array, int left, int right, int index)
{
if(left >= right)
return array[left];
int pivot = array[left];
int i=0, j = 0; for(i=left+1,j = left+1; j <=right; ++j)
{
if(array[j] < pivot)
swap(array[i++], array[j]);
}
swap(array[left], array[i-1]); //pivot is the (i-left)-th element, and pivot's index is i-1 if(index == i-left)
return array[i-1];
else if(index < i-left)
return element(array, left, i-2, index);
else
return element(array, i,right, index-i+left);
}
The-ith-Element的更多相关文章
- Google C++单元测试框架GoogleTest---GMock的CheatSheet文档
CheatSheet文档中包含了GMock所有常用的东西,看了这个基本上就可以用它了,本文接上篇博文:Google C++单元测试框架GoogleTest---Google Mock简介--概念及基础 ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- [LeetCode] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 4 Best Time to Buy and Sell Stock III_Leetcode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- 用iDSDT制作声显卡DSDT
已有 2299 次阅读2011-10-24 21:00 |个人分类:Mac| DSDT 快速增加积分秘笈! windows下!--------------------------------第一步.下 ...
- 进程控制之wait和waitpid函数
当一个进程正常或异常终止时,内核就向其父进程发送SIGCHLD信号.因为子进程终止是个异步事件(这可以在父进程运行的任何时候发生),所以这种信号也是内核向父进程发的异步通知.父进程可以选择忽略该信号, ...
- Ubuntu下安装FTP服务及使用(VSFTPD详细设置)(二)
vsftpd 作为一个主打安全的FTP服务器,有很多的选项设置.下面介绍了vsftpd的配置文件列表,而所有的配置都是基于vsftpd.conf这个配置文件 的.本文将提供完整的vsftpd.conf ...
- apache htaccess
#RewriteCond :重新条件#RewriteRule : 重写规则# .htaccess文件中的规则会以它们出现的顺序被处理 <IfModule mod_rewrite.c>Rew ...
- jackson使用示例
Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. Jackson 2.x版提供了三个JAR包供下载: 1. Core库:strea ...
- [Tips]解决HG之waiting for lock on repository
# Symptom 在tortoiseHg中commit或者Sync change的时候,总是出现下面的错误: waiting for lock on repository ... # Solutio ...
- Python-Day11 RabbitMQ/redis
写在前面: 好久不写了,实在是不想写,坚持果然是一件不容易的事情. 我喜欢玩,我更爱学习,然而喜欢是放肆,爱是克制,哈哈.每天上班有些忙就不想动,只想在床上翻滚或者鏖战召唤师峡谷.上班闲着时想了想,一 ...
- 1.6.9 UIMA Integration
1. UIMA 集成 你可以使用solr集成Apache的非结构化信息管理架构(UIMA).UIMA可以让你定义自己的分析引擎通道,逐步添加元数据到文档的标注. 关于Solr UIMA的更多信息,参考 ...
- Android-ViewPagerIndicator框架使用——TabPageIndicator以及样式的修改
今天介绍一个常用的框架,一般app都会用到这样的框架,下面就来介绍框架的使用以及样式的修改,那就以我自己写的例子来向大家介绍吧! 首先给出xml ,在相应窗口的布局文件中引入TabPageIndica ...
- <c:forEach> 详解
<c:forEach>标签用于通用数据循环,它有以下属性 属 性 描 述 是否必须 缺省值 items 进行循环的项目 否 无 begin 开始条件 否 0 end 结束条件 否 集合中的 ...