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.

Example

If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.

Challenge

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】的更多相关文章

  1. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  2. 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 ...

  3. 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 ...

  4. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  5. 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 ...

  6. 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 ...

  7. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  8. 657. Judge Route Circle【easy】

    657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...

  9. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

随机推荐

  1. 【分块】bzoj3295 [Cqoi2011]动态逆序对

    考虑每次删除pos位置一个数x后,所造成的的影响就是,逆序对的个数少了在1~pos-1中大于x的数的个数加上pos+1~n中小于x的数的个数. 那么我们需要的操作就只有查询区间内比某数大(小)的个数. ...

  2. 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching

    题意:在树中找到一个点i,并且找到这个点子树中的一些点组成一个集合,使得集合中的所有点的c之和不超过M,且Li*集合中元素个数和最大 首先,我们将树处理出dfs序,将子树询问转化成区间询问. 然后我们 ...

  3. 【单调队列优化DP】BZOJ1855-[Scoi2010]股票交易

    [题目大意] 已知第i天的股票买入价为每股APi,第i天的股票卖出价为每股BPi(数据保证对于每个i,都有APi>=BPi),第i天的一次买入至多只能购买ASi股,一次卖出至多只能卖出BSi股. ...

  4. iOS开发技巧——Autolayout动画

          使用Autolayout时需要在动画的代码前面后面添加 layoutIfNeeded方法 Animation AutoLayout也可以配合传统的animation方法,整体代码结构如下. ...

  5. adobe flash builder 4.6最新能用的序列号

    神key来了: 1424-4464-3877-6130-5013-5457 妈的,我两台开发机器,有一台死活激活不了,每隔一段时间就去网上搜搜激活方式已经快成为我的一个生活习惯了,可惜每次出来的都是下 ...

  6. GetAdaptersInfo & GetAdaptersAddresses

    I use GetAdaptersInfo to get MAC addresses of interfaces.   GetAdaptersInfo exist on old and new ver ...

  7. 重新认识JavaScript里的创建对象

    一.序 面向对象有一个标志,那就是它们都有类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.ECMA-262把对象定义为“无序属性的集合,其属性可以包含基本值.对象或者函数”. 使用Obje ...

  8. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  9. JS中event.preventDefault()取消默认事件能否还原?

    参考知乎讨论:https://www.zhihu.com/question/21891734

  10. LNMP第二部分nginx、php配置

    内容概要:一. nginx.confvim /usr/local/nginx/conf/nginx.conf //清空原来的配置,加入如下内容:user nobody nobody;worker_pr ...