问题:链接

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

解答:

搜索,注意边界条件。

代码:

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

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. java 构造函数是如何执行的

    1.构造函数不是方法!! 原因1:方法的话,会直接执行方法体内的代码,但是构造函数首先执行的不是{}里的代码块,而是给对象的成员初始化: 2.方法可以被调用其他方法调用,但是构造函数不能被方法或变量调 ...

  2. Web打印组件jatoolsPrinter(转载)

    应用web化,不论对开发商,还是对用户来说,实在是一种很经济的选择,因为基于web的应用,客户端的规则很简单,容易学习,容易维护,容易发布.但对程序员来说,因为浏览器的局限性,却要面对很多挑战.怎么样 ...

  3. js动画学习(一)

    一.运动框架实现思路 1.匀速运动(属性值匀速变化)(改变 left, right, width, height, opacity 等): 2.缓冲运动(属性值的变化速度与当前值与目标值的差成正比): ...

  4. BarChart控件的使用

    The HTML Markup <asp:BarChart ID=" ChartType="Column" ChartTitle="United Stat ...

  5. Python学习之路——模块

    一.模块: 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

  6. Django内置template标签

    html过滤{% autoescape on|off %} {{body}} {% endautoescape %} 注释{% comment %} {% endcomment %} csrf攻击 { ...

  7. QLabel设置行间距(使用html的语法,比较巧妙)

    1.设置行间距 QLabel没有设置行间距的函数,所以这种办法是行不通的.只能采用其它类似的方法来实现,例如设置行高,使用样式代码如下: <p style='line-height:18px'& ...

  8. Spring Boot特性(转)

    摘要: 1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringA ...

  9. poj 1256 Anagram(dfs)

    题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...

  10. Pagodas(等差数列)

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...