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

解法一:线性查找(linear search)

class Solution
{
public:
int searchInsert(int A[], int n, int target)
{
if(n> && target <= A[])
return ;
for(int i = ; i < n; i ++)
{
if(A[i] >= target)
return i;
}
return n;
}
};

解法二:二分查找(binary search)

class Solution {
public:
int searchInsert(int A[], int n, int target) {
int low = ;
int high = n-;
while(low <= high)
{
int mid = low + (high-low) / ;
if(A[mid] == target)
return mid;
else if(A[mid] > target)
high = mid - ;
else
low = mid + ;
}
return low;
}
};

【LeetCode】35. Search Insert Position (2 solutions)的更多相关文章

  1. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  2. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  3. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  4. 【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 ...

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

  6. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  7. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  8. LeetCode Problem 35:Search Insert Position

    描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...

  9. 【LintCode】060.Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

随机推荐

  1. 【XJOI】【NOI考前模拟赛7】

    DP+卡常数+高精度/  计算几何+二分+判区间交/  凸包 首先感谢徐老师的慷慨,让蒟蒻有幸膜拜了学军的神题.祝NOI2015圆满成功 同时膜拜碾压了蒟蒻的众神QAQ 填填填 我的DP比较逗比……( ...

  2. html嵌套iframe怎样实现等iframe页面载入完进行下一步调用

    </pre>假设想在你的html里面显示一张图片.或者显示一个报表,常常会在里面嵌套iframe,当我们点查询报表时.在报表显示过程中,我们想做个遮罩层,提示等待...可是报表显示出来后. ...

  3. iOS:制作一个简易的计算器

    初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...

  4. 最大熵,熵,MLE的解释,还行

    这篇文章: https://blog.csdn.net/jiaoyangwm/article/details/81276921

  5. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  6. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

  7. CSS 的优先级机制总结

    一.样式优先级: 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使用多重样式的情况. 一般情况下,大家都认为优先级是:内联样式 > 内部样 ...

  8. QlikView图表显示同比数据

    数据准备例如以下: SalesData: LOAD Num(ID) as ID, Date(Date) as Date, Month, Num(Year) as Year, Num(Sales) as ...

  9. 剑指offer面试题12-打印1到最大的n位数

    题目: 输入一个数字n,按顺序打印出从1最大的n位十进制数.比方输入3,则打印出1.2.3最大的三位数即999 这道题的主要陷阱就在大数的处理,仅仅要将这个考虑进去,用字符串来表示.就好说了. 那差点 ...

  10. js正则表达式/replace替换变量方法

    转自:http://www.blogjava.net/pingpang/archive/2012/08/12/385342.html 1. javascript 正则对象替换创建和用法:/patter ...