问题:链接

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

解答:

搜索,注意边界条件。

代码:

  1. class Solution {
  2. public:
  3. int searchInsert(int A[], int n, int target) {
  4. if(A[0] >= target || n == 0)
  5. return 0;
  6. for(int i = 0; i < n; i++)
  7. {
  8. if(A[i] == target)
  9. return i;
  10. else if(i == n-1)
  11. return n;
  12. else if(A[i] < target && A[i+1] > target)
  13. {
  14. return i+1;
  15. }
  16. }
  17. }
  18. };

Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target的更多相关文章

  1. [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 ...

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

  3. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  4. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  5. LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

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

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

  7. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  8. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

  9. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

随机推荐

  1. php 实现简单的登录

    //登录页面: V层前端模板: Administrator@QCEE61NZ66FEX2D /cygdrive/c/wamp/www/thinkphp/Home/Tpl/Login $ ls inde ...

  2. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  3. http://qt-project.org/wiki/Category:Developing_with_Qt::QtWebKit#ff7c0fcd6a31e735a61c001f75426961

    404: Not Found | Qt Project QtWebKit documentation http://dwz.cn/hr2013

  4. Cocos2d_x的特点及环境配置

    Cocos2d_x的特点: 什么是"x"?Cocos2d_x方式:有时候我们写的cpp文件扩展,CXX."X"标致着该项目是由c++,并提供c++中的API编写 ...

  5. Thrift对多接口服务的支持

    Thrift对多接口服务的支持 Thrift在0.9.1版本之前,一直只提交了对单一接口服务的支持,即一个RPC服务器(对应一个端口)支持一个服务接口的实现. 但是很多时候,我们的服务不能实现在一个接 ...

  6. php:检测用户当前浏览器是否为IE浏览器

    /** * 检测用户当前浏览器 * @return boolean 是否ie浏览器 */ function chk_ie_browser() { $userbrowser = $_SERVER['HT ...

  7. 去除List列表中反复值(稍作调整,也适合于List&lt;T&gt; 和 List&lt;?&gt;)

    方法一 循环元素删除 [c-sharp] view plaincopy public static void removeDuplicate(List list) { for ( int i = 0  ...

  8. runtime的概念,message send如果寻找不到相应的对象,如何进行后续处理

    运行时刻是指一个程序在运行(或者在被执行)的状态.也就是说,当你打开一个程序使它在电脑上运行的时候,那个程序就是处于运行时刻.在一些编程语言中,把某些可以重用的程序或者实例打包或者重建成为“运行库”. ...

  9. python之列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 1,比如:要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, ...

  10. C#调用C/C++动态库 封送结构体,结构体数组

    因为实验室图像处理的算法都是在OpenCV下写的,还有就是导航的算法也是用C++写的,然后界面部分要求在C#下写,所以不管是Socket通信,还是调用OpenCV的DLL模块,都设计到了C#和C++数 ...