1 #include 2 #include 3 #include 4 #define sc(x) scanf("%d",&(x)) 5 #define pf(x) printf("%.4lf\n", x) 6 using namespace std; 7 int T; 8 double Y;...
分类:
其他好文 时间:
2015-02-28 18:36:30
阅读次数:
156
Implementint sqrt(int x).Compute and return the square root ofx.解法:二分搜索 时间复杂度O(logN), 空间复杂度O(1) 1 class Solution { 2 public: 3 int sqrt(int x) {...
分类:
其他好文 时间:
2015-02-25 19:53:35
阅读次数:
143
二分搜索,和LeetCode 153. Find Minimum in Rotated Sorted Array相似。
只是在num[begin] == num[mid]时,需要binary_search(++ begin, end, num); 这时仅将begin移进一位,并没有进行二分查找。
所以如测试用例为 num = {1, 1, 1, 1, 1, ..., 1}等特殊情况时,最坏情况...
分类:
其他好文 时间:
2015-02-24 00:50:43
阅读次数:
206
题意:n个男孩n个女孩,女孩选男孩,每个女孩都要选到不同的人k对女孩有相同选择标准,女孩每轮都选择没选过的男孩,问总共能选几轮。思路:女孩1..n,男孩n+1..2*n编号由女孩到男孩建容量为1的边起点st=2*n+1,到1..n建边;n+1..2*n到终点ed=2*n+2建边二分搜索最大容量即为答案。详见代码:
/****************************************...
分类:
其他好文 时间:
2015-02-18 09:34:05
阅读次数:
121
维基上的代码:
int binary_search(int A[], int key, int imin, int imax)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
// calculate the midpoint for roughly equ...
分类:
编程语言 时间:
2015-02-13 14:49:10
阅读次数:
205
题目:有n个物品的重量和价值分别为wi,viw_i,v_i,从中选取k个物品,使得单位重量的价值最大样例:输入:
n=3
k=2
(w,v)={ (2 , 2) , (5 , 3) , (2 , 1) }输出
0.75(选0号和2号 ( 2 + 1)/( 2 + 2) = 0.75)思路首先想到的方法是先把物品按照单价排序,再从大到小进行选取。但是这样选出来的不一定是最优的,例如上面的案例,...
分类:
其他好文 时间:
2015-02-12 16:15:49
阅读次数:
160
题目链接:Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to sear...
分类:
其他好文 时间:
2015-02-04 23:24:53
阅读次数:
261
题目链接:Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the...
分类:
其他好文 时间:
2015-02-04 23:24:39
阅读次数:
254
题目链接:Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume ...
分类:
其他好文 时间:
2015-02-04 23:21:55
阅读次数:
148
题目链接:Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
这道题的要求是在不使用乘法、除法、取模运算的前提下实现两个整数相除。如果溢出,返回MAX_INT。
这道题的直接思路是...
分类:
其他好文 时间:
2015-02-02 23:11:20
阅读次数:
158