14. First Position of Target 【easy】
14. First Position of Target 【easy】
For a given sorted array (ascending order) and a target
number, 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 ...
随机推荐
- [USACO Special 2007 Chinese Competition]The Bovine Accordion and Banjo Orchestra
[原题描述以及提交地址]:http://acm.tongji.edu.cn/problem?pid=10011 [题目大意] 给定两个长度为N的序列,要给这两个序列的数连线.连线只能在两个序列之间进行 ...
- [Codeforces 7E] Defining Macros
Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...
- 【动态规划】【零一背包】CODEVS 1014 装箱问题 2001年NOIP全国联赛普及组
#include<cstdio> #include<algorithm> using namespace std; ],f[]; int main() { scanf(&quo ...
- iOS 文字渐变色
// 创建UILabel UILabel *label = [[UILabel alloc] init]; label.text = @"我是渐变的label"; [label s ...
- How to Analyze "Deadlocked Schedulers" Dumps?---WINDBG
https://blogs.msdn.microsoft.com/karthick_pk/2010/06/22/how-to-analyze-deadlocked-schedulers-dumps/ ...
- java中的注解详解和自定义注解
一.java中的注解详解 1.什么是注解 用一个词就可以描述注解,那就是元数据,即一种描述数据的数据.所以,可以说注解就是源代码的元数据.比如,下面这段代码: @Override public Str ...
- 防止木马利用iframe框架来调用外域JS代码
<!--防止木马利用iframe框架来调用外域JS代码,不过滤自己网站的域名的框架网页开始--><SCRIPT LANGUAGE="JavaScript"> ...
- oracle 10g函数大全--转换函数
chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SELECT ...
- 怎样使用libcurl获取隐藏了文件后缀的url网络文件类型
CURLINFO_CONTENT_TYPE CURL: Get Returned Content Mime Type 例如 :以下代码可以查询出天地图的tile图像类型为jpg "http: ...
- python抓取360百科踩过的坑!
学习python一周,学着写了一个爬虫,用来抓取360百科的词条,在这个过程中.因为一个小小的修改,程序出现一些问题,又花了几天时间研究,问了各路高手,都没解决,终于还是自己攻克了,事实上就是对lis ...