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 ...
随机推荐
- [BZOJ 1566] 管道取珠
Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1566 Solution: 思路十分精奇的一道题目 题目要求的是$\sum_{i=1}^k ...
- POJ 3622 Gourmet Grazers(贪心)
[题目链接] http://poj.org/problem?id=3622 [题目大意] 给出一些物品拥有两个属性值,价格和精美程度 给出一些需求表示要求获得的物品两个属性值的两种属性下界, 一个物品 ...
- 【CCpp程序设计2017】迷宫游戏
大一寒假作业!写了第一个小游戏! //maze_test By lizitong #include<stdio.h> #include<time.h> #include< ...
- 7.2(java学习笔记)URL
一.URL URL类表示统一资源定位符,指向万维网上“资源”的指针. 资源可以是简单的文件或目录,也可以是对更复杂对象的引用,比如对数据库或搜索引擎的查询. URL即是定位也是资源,定位到网络中一个具 ...
- Java高级架构师(一)第02节:分模块、分工程管理
本节课程的目标在于:利用Maven构建分工程.分模块的空项目. -------- 基本的构建大致相同,有一个强调调点: 在总web的pom里边(architecture01web中),加入要合并的wa ...
- Android Studio 下载地址 & SDK 更新教程
Android Studio 下载地址: http://www.android-studio.org/ SDK 更新教程: http://www.androiddevtools.cn/ SDK 推荐使 ...
- Linux之ps查找进程用kill终止命令
http://www.cnblogs.com/peida/archive/2012/12/20/2825837.html http://blog.csdn.net/andy572633/article ...
- 扑克模拟,牌型判断java版
Card类 package com.company; public class Card { private String color; private Integer value; public S ...
- Pressed状态和clickable,duplicateParentState的关系
做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...
- Coherence装载数据的研究 - Invocation Service
这里验证第三个方法,原理是将需要装载的数据分载在所有的存储节点上,不同的地方是利用了存储节点提供的InvocationService进行装载,而不是PreloadRequest, 原理如图 前提条件是 ...