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 ...
随机推荐
- Cannot instantiate the type HttpClient问题
看自己动手写爬虫,没想到一上来就跪了. 里面提到用的jar包是apache的http客户端开源项目---HttpClient 就去官网下载了一个版本4.3 当按书上代码敲时 HttpClient ht ...
- ecshop被加入了黑链
朋友一个ecshop网站被攻击了,查看代码如下: <?php $password = "1";//设置密码 error_reporting(E_ERROR); header( ...
- Thrift安装问题
1.error: Bison version 2.5 or higher must be installed on the system! 哈哈,Bison版本低了吧,用下面的命令 wget ht ...
- [golang学习] 在idea中code & debug
[已废弃]不需要看 idea 虽然审美倒退了n年. 不过功能还是相当好用的. idea 的go插件堪称最好的go ide. 1. 语法高亮支持 2. 智能提示 3. 跳转定义(反跳转回来) 4. 集成 ...
- EF——一个实体对应两张表,两个实体对应一张表 06 (转)
本篇日记我们将详细探讨如何将表现领域的类映射到现有的数据库.现在的经济形势不是太好,很多公司都取消了开发新系统的预算.在这种情况下,通常的做法是把原有的几个系统修改一下做个集成,先凑合用着得了.如果要 ...
- Spark installation for windows
download spark from spark.apache.org download hadoop from hadoop.apache.org download hadoop.dll and ...
- 精通C#(第6版)
<精通C#(第6版)> 基本信息 原书名:Pro C# 5.0 and the .NET 4.5 framework,sixth edition 作者: (美)Andrew Troelse ...
- 怒刷DP之 HDU 1114
Piggy-Bank Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- 如何优雅地制作精排 ePub —— 个人电子书制作规范及基本样式表
随着大数据时代的到来,多种移动阅读终端方兴未艾 —— Amazon Kindle不再小众.各互联网巨头纷纷推出旗下的电子书阅读软件.有了阅读的软件/硬件支持,必不可少的就是阅读什么的问题了.ePub格 ...
- Understand
快捷键: Ctrl+Shift+H 折叠 Ctrl+Alt+F 替换