35 Search Insert Position(找到数的位置Medium)

题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置。

思路:折半查找,和相邻数比较,注意边界

 1 class Solution {
 2 public:
 3     int searchInsert(vector<int>& nums, int target) {
 4         int start=0,end=nums.size()-1;
 5         int flag=0;
 6         while(start<=end){
 7             flag=(start+end)/2;
 8             if(nums[flag]==target)return flag;
 9             else if(nums[flag]<target){
10                 if(flag==nums.size()-1||nums[flag+1]>=target)return flag+1;  //和相邻数比较
11                 else start=flag+1;
12             }
13             else{
14                 if(flag==0||nums[flag-1]<target)return flag;
15                 else end=flag-1;
16             }
17         }
18     }
19 };
时间: 2024-10-02 02:50:11

35 Search Insert Position(找到数的位置Medium)的相关文章

LeetCode练题——35. Search Insert Position

1.题目 35. Search Insert Position Easy 1781214Add to ListShare 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 th

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

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

[Leetcode][Python]35: Search Insert Position

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 35: Search Insert Positionhttps://oj.leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, return the index if the target is found.If not, return the index whe

LeetCode 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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

[LeetCode] 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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

LeetCode 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 ma

LeetCode OJ 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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

LeetCode 35 Search Insert Position (C,C++,Java,Python)

Problem: 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,

35. Search Insert Position【leetcode】

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 →