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 ...
随机推荐
- SQL还原备份数据库读取失败 38错误解决办法
连接上数据库后新建查询执行以下命令: RESTORE DATABASE 还原后的数据库名 FROM DISK = 'D:\yjdb\pms_yj_20110722.bak(备份文件)' WITH RE ...
- Viewport解决分辨率适配问题
Viewport : 字面意思为视图窗口,在移动 web 开发中使用.表示将设备浏览器宽度虚拟成一个特定的值(或计算得出),这样利于移动 web 站点跨设备显示效果基本一致. 基本写法: &l ...
- php.ini的配置
一.user_agent ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)') user_agent,再 ...
- Domain Driven Design and Development In Practice--转载
原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about ma ...
- C++实现日期转换类DateTime
概述 工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用: // DateTime.hpp #ifndef _DATETIME_H # ...
- 将远程数据库中的某表数据复制到本数据库(ORACLE)
1. 建立 DATABASE LINKCREATE PUBLIC DATABASE LINK ABCCONNECT TO SA IDENTIFIED BY PASSWORDUSING '(DESCRI ...
- 1.4.2 solr字段类型--(1.4.2.4)使用Dates(日期)
1.4.2 solr字段类型 (1.4.2.1) 字段类型定义和字段类型属性. (1.4.2.2) solr附带的字段类型 (1.4.2.3) 使用货币和汇率 (1.4.2.4) 使用Dates(日期 ...
- Android 珍藏(二)
一.如何控制Android LED等?(设置NotificationManager的一些参数) 代码如下: final int ID_LED=19871103; NotificationManage ...
- Oracle基础(九) Oracle的体系结构
一.Oracle体系结构概述: Oracle的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制.要了解Oracle数据库的体系结构,必须理解Oracle系统的重要概念和主要 ...
- Lnmp下安装memcached
Lnmp下安装memcached 1.先安装 libevent,再安装 Memcached主程序 # tar xf libevent-2.0.21-stable.tar.gz # cd ...