14. First Position of Target 【easy】
14. First Position of Target 【easy】
For a given sorted array (ascending order) and a targetnumber, find the first index of this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
If the count of numbers is bigger than 2^32, can your code work properly?
解法一:
class Solution {
public:
/**
* @param nums: The integer array.
* @param target: Target number to find.
* @return: The first position of target. Position starts from 0.
*/
int binarySearch(vector<int> &array, int target) {
if (array.size() == ) {
return -;
}
int start = ;
int end = array.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (array[mid] == target) {
end = mid;
}
else if (array[mid] < target) {
start = mid;
}
else if (array[mid] > target) {
end = mid;
}
}
if (array[start] == target) {
return start;
}
if (array[end] = target) {
return end;
}
return -;
}
};
解法二:
class Solution {
public:
int find(vector<int> &array, int start, int end, int target) {
if (start > end) {
return -;
}
int mid = start + (end - start) / ;
if (array[mid] == target) {
if (array[mid - ] != target) {
return mid;
}
return find(array, start, mid - , target);
}
else if (array[mid] > target) {
return find(array, start, mid - , target);
}
else if (array[mid] < target) {
return find(array, mid + , end, target);
}
}
/**
* @param nums: The integer array.
* @param target: Target number to find.
* @return: The first position of target. Position starts from 0.
*/
int binarySearch(vector<int> &array, int target) {
// write your code here
int start = ;
int end = array.size();
return find(array, start, end, target);
}
};
14. First Position of Target 【easy】的更多相关文章
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 283. Move Zeroes【easy】
283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 657. Judge Route Circle【easy】
657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
随机推荐
- [BZOJ2111][ZJOI2010]Perm排列计数(组合数学)
题意就是求一个n个点的堆的合法形态数. 显然,给定堆中所有数的集合,则这个堆的根是确定的,而由于堆是完全二叉树,所以每个点左右子树的大小也是确定的. 设以i为根的堆的形态数为F(i),所以F(i)+= ...
- 【二分】【动态规划】Codeforces Round #393 (Div. 1) B. Travel Card
水dp,加个二分就行,自己看代码. B. Travel Card time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 【容斥原理】CDOJ - 1544 - 当咸鱼也要按照基本法
众所周知zhu是一个大厨,zhu一直有自己独特的咸鱼制作技巧. tang是一个咸鱼供应商,他告诉zhu在他那里面有NN条咸鱼(标号从1到N)可以被用来制作. 每条咸鱼都有一个咸鱼值KiKi,初始时所有 ...
- 【单调队列】bzoj1047 [HAOI2007]理想的正方形
先把整个矩阵处理成b[n][m-K+1].c[n][m-K+1]大小的两个矩阵,分别存储每行每K个数中的最大.最小值,然后再通过b.c处理出d.e分别表示K*K大小的子矩阵中的最大.最小值即可.单调队 ...
- ajaxfileupload-上传文件示例
1.引用文件 ajaxfileupload.js @{ ViewBag.Title = "数据导入"; Layout = "~/Views/Shared/_IndexLa ...
- 三星s3c24xx平台GPIO操作详解
转:http://blog.chinaunix.net/uid-22030783-id-3391515.html 先介绍三星S3C24XX平台BSP中定义外设寄存器和GPIO的相关头文件 以linux ...
- MORMOT数据库连接池
MORMOT数据库连接池 MORMOT封装了一堆的PROPS控件,用于连接各种数据库. MORMOT的封装是武装到了牙齿的,这堆PROPS控件居然数据库连接池也封装好了.这就为我们省了不少事,笔者非常 ...
- Swift,集合
1.创建(Set)集合(无序不可重复) (1)创建空集合 var a=Set<Int>() //[] (2)创建集合 var a:Set=[1,2,3] //[2,3,1] 2.集合插入( ...
- JS向后台传递json数组对象
var Obj = []; //一下代码可以循环插入 var returnObj = new Object();//创建一个对象 returnObj.id = “123”: returnObj.mon ...
- javascript(JQuery)元素操作
html代码如下: <div id="picK"> <ul> <li style="float:left;width:90px;" ...