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 ...
随机推荐
- C语言 HTTP上传文件-利用libcurl库上传文件
原文 http://justwinit.cn/post/7626/ 通常情况下,一般很少使用C语言来直接上传文件,但是遇到使用C语言编程实现文件上传时,该怎么做呢? 借助开源的libcurl库,我们 ...
- I can do it!(贪心)
I can do it! Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- sql 时间和字符串 取到毫秒级
(select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')+(Select ri ...
- Thread.Sleep(0)的意义 操作系统中CPU的竞争策略
在线程的学习中遇到的 不太明白就搜了一下 有一篇觉得写得很好的分享一下 转载:http://www.360doc.com/content/12/1220/07/1054746_255212714.s ...
- Servlet学习的两个案例之网站访问次数的统计
一.统计次数的Servlet源码 package com.shanrengo; import java.io.IOException; import javax.servlet.ServletCont ...
- PHP 字符串处理 总结
PHP 字符串处理 PHP 字符串处理 PHP 的字符串处理功能非常强大,主要包括: 字符串输出 echo():输出一个或多个字符串 print():输出一个字符串 printf():输出格式化字符串 ...
- 使用phpQuery实现批量文件处理
能够将置顶文夹下的指定类型文件进行处理 <?php header('Content-Type:text/html;Charset=utf-8'); include './phpQuery/php ...
- 如何解决JavaWeb乱码问题
作为一个合格的web开发人员应该是什么问题都遇到过的,尤其是乱码问题.大家也许都体会到了,我们中国人学编程,很大的一个不便就是程序的编码问题,无论学习什么技术,我们都需要探讨他的编码问题. 今天来讲一 ...
- Flask中路由模块的实现
在Flask中的路由功能主要通过修饰函数route实现,下面我们就来挖掘下route在源代码中是怎么分配视图函数的. def route(self, rule, **options): def dec ...
- Kqueue与epoll机制
首先介绍阻塞与非阻塞:阻塞是个什么概念呢?比如某个时候你在等快递,但是你不知道快递什么时候过来,而且你没有别的事可以干(或者说接下来的事要等快递来了才能做):那么你可以去睡觉了,因为你知道快递把货送来 ...