leetcode_35题——Search Insert Position(二分查找)

Search Insert Position

Total Accepted: 56150 Total Submissions: 158216My Submissions

Question Solution

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 no duplicates in the array.

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

Hide Tags

Array Binary Search

Have you met this question in a real interview?

Yes

No

Discuss

这道题是个简单题,没啥说的

#include<iostream>
#include<vector>
using namespace std;
int searchInsert(vector<int>& nums, int target) {
	if(nums.empty())
		return 0;
	int x=0;
	int y=nums.size()-1;
	while(1)
	{
		if(x==y)
			if(target<=nums[x])
				return x;
			else
				return x+1;
		int z=(y-x)/2+x;

		if(target==nums[z])
			return z;
		else if(target>nums[z])
			x=z+1;
		else
			y=z;
	}
}
int main()
{
	vector<int> vec;
	vec.push_back(1);vec.push_back(3);vec.push_back(5);vec.push_back(6);
	cout<<searchInsert(vec,7)<<endl;
}

  

时间: 2024-10-05 15:44:42

leetcode_35题——Search Insert Position(二分查找)的相关文章

Leetcode 35 Search Insert Position 二分查找(二分下标)

基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第一题的解法,比较类似,当然是今天临时写的. 知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒... 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 in

35. 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 no duplicates in the array. Example 1: Input: [1,3,5,6], 5 Output: 2 Example 2:

leetcode 刷题之路 70 earch 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 no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

【LeetCode】- 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 no duplicates in the array. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

[LC]35题 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 no duplicates in the array. Example 1: Input: [1,3,5,6], 5Output: 2Example

leetcode第34题--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 no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

LeetCode 35 Search Insert Position(查找插入位置)

题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入该数字的下标 由于该数组是已经排好序的数组,可以利用二分查找. 二分查找的返回结果: 1. 当查找的数字在数组中时,返回第一次出现的下标 2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 参考代码: package leetcode_50; import java.u

LeetCode OJ:Search Insert Position(查找插入位置)

首线是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,兵器记下插入位置,指针递增到那个地方的时候就找到了那个位置.如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅: 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 set<int&

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 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 i