[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 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

【分析】

这道题比较简单,就是二分查找。思路就是每次取中间,如果等于目标即返回,否则根据大小关系切去一半。因此算法复杂度是O(logn),空间复杂度O(1)。

【代码】

/*********************************
*   日期:2015-01-24
*   作者:SJF0115
*   题目: 35.Search Insert Position
*   网址:https://oj.leetcode.com/problems/search-insert-position/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int searchInsert(int A[], int n, int target) {
        if(n <= 0){
            return -1;
        }//if
        int start = 0,end = n - 1;
        // 二分查找
        while(start <= end){
            int mid = start + ((end - start) >> 1);
            // 目标找到
            if(A[mid] == target){
                return mid;
            }//if
            // 目标在左半部分
            else if(A[mid] > target){
                end = mid - 1;
            }//else
            // 目标在右半部分
            else{
                start = mid + 1;
            }//else
        }//while
        // 目标元素没有找到则找插入位置
        return start;// end + 1
    }
};

int main(){
    Solution solution;
    int A[] = {1,3,5,6};
    int n = 4;
    int target = 0;
    int result = solution.searchInsert(A,n,target);
    // 输出
    cout<<result<<endl;
    return 0;
}

时间: 2024-09-27 06:27:27

[LeetCode]35.Search Insert Position的相关文章

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

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

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] Design Search Autocomplete System 设计搜索自动补全系统

Design a search autocomplete system for a search engine. Users may input a sentence (at least one word and end with a special character '#'). For each character they type except '#', you need to return the top 3historical hot sentences that have pref

[LeetCode]34.Search for a Range

[题目] Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [

[LeetCode]74.Search a 2D Matrix

[题目] Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previo

[LeetCode]240.Search a 2D Matrix II

题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to botto

[LeetCode]81.Search in Rotated Sorted Array II

[题目] Search in Rotated Sorted Array II  Total Accepted: 3749 Total Submissions: 12937My Submissions Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a f

[LeetCode]33.Search in Rotated Sorted Array

[题目] Search in Rotated Sorted Array  Total Accepted: 5827 Total Submissions: 20925My Submissions Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value