Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target
问题:链接
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的更多相关文章
- [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 ...
- [Leetcode] search insert position 寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- 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 ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
随机推荐
- BZOJ 3211 花神游历各国 (树状数组+并查集)
题解:首先,单点修改求区间和可以用树状数组实现,因为开平方很耗时间,所以在这个方面可以优化,我们知道,开平方开几次之后数字就会等于1 ,所以,用数组记录下一个应该开的数,每次直接跳到下一个不是1的数字 ...
- BZOJ 1058 报表统计 (STL)
题解:数据结构的基本操作,用STL可以完美实现,就是比较慢…… #include <cstdio> #include <map> #include <set> #i ...
- Unity3D游戏开发之连续滚动背景
Unity3D游戏开发之连续滚动背景 原文 http://blog.csdn.net/qinyuanpei/article/details/22983421 在诸如天天跑酷等2D游戏中,因为游戏须要 ...
- :before :after
#p1:before{ content: "哈哈哈 "; color: red;}#p1:after{ content: "哈哈哈"; color: #452d ...
- SharePoint BCS
1. 开启相关的服务:管理中心-->应用程序管理-->管理服务器上的服务 2.
- for语句的嵌套(示例及练习)
for(初始条件:循环条件:状态改变) {for(初始条件:循环条件:状态改变) { 循环体 }} 一般,用来解决循环的方法:穷举法.迭代法. 示例一:阶乘的和 示例二: 练习一:兔 ...
- 获得view所在的控制器
- (UIViewController*)getViewController{ for (UIView* next = [self superview]; next; next = next.supe ...
- 【转】Tomcat7.0.42源代码运行环境搭建
转自:http://tyrion.iteye.com/blog/1903608 以前看过Tomcat5的一部分源代码,当时只看了个大概的启动分析,后来看了<How Tomcat Works> ...
- Android Studio does not point to a valid jvm
环境变量 JAVA_HOME的值,去掉后面的分号,一般情况下就可以启动
- C语言之三大查找算法
查找算法 1.二分查找 二分查找就是折半查找,其基本思想是:首先选取表中间位置的记录,将其关键字与给定关键字key进行比较,若相等,则查找成功.若key值比该关键字值大,则要找的元素一定在右子表中,则 ...