给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引。如果没有,返回到它将会被按顺序插入的位置。
你可以假设在数组中无重复元素。
案例 1:
输入: [1,3,5,6], 5
输出: 2
案例 2:
输入: [1,3,5,6], 2
输出: 1
案例 3:
输入: [1,3,5,6], 7
输出: 4
案例 4:
输入: [1,3,5,6], 0
输出: 0
详见:https://leetcode.com/problems/search-insert-position/description/

Java实现:

  1. class Solution {
  2. public int searchInsert(int[] nums, int target) {
  3. int size=nums.length;
  4. if(size==0||nums==null){
  5. return -1;
  6. }
  7. int l=0;
  8. int r=size-1;
  9. int m=0;
  10. while(l<=r){
  11. m=(l+r)>>1;
  12. if(nums[m]==target){
  13. return m;
  14. }else if(nums[m]>target){
  15. --r;
  16. }else{
  17. ++l;
  18. }
  19. }
  20. return l;
  21. }
  22. }

C++实现:

  1. class Solution {
  2. public:
  3. int searchInsert(vector<int>& nums, int target) {
  4. int size=nums.size();
  5. if(size==0||nums.empty())
  6. {
  7. return -1;
  8. }
  9. int l=0,r=size-1,m=0;
  10. while(l<=r)
  11. {
  12. m=(l+r)>>1;
  13. if(nums[m]==target)
  14. {
  15. return m;
  16. }
  17. else if(nums[m]>target)
  18. {
  19. r=m-1;
  20. }
  21. else
  22. {
  23. l=m+1;
  24. }
  25. }
  26. return l;
  27. }
  28. };

035 Search Insert Position 搜索插入位置的更多相关文章

  1. lintcode:Search Insert Position 搜索插入位置

    题目: 搜索插入位置 给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置. 你可以假设在数组中无重复元素. 样例 [1,3,5,6],5 → 2 ...

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

  4. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  5. [LeetCode] 035. Search Insert Position (Medium) (C++)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...

  6. LeetCode 035 Search Insert Position

    题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...

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

  8. 【LeetCode每天一题】Search Insert Position(搜索查找位置)

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

  9. [Leetcode] search insert position 寻找插入位置

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

随机推荐

  1. powermock, 强力模拟

    1. powermock是基于mockito或者easymock,TestNG之上的mock: 2. 提供了对于静态函数,私有函数的mock 3. 下载地址:https://github.com/po ...

  2. 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较

        几大最短路径算法比较 转自:http://blog.csdn.net/v_july_v/article/details/6181485 几个最短路径算法的比较: Floyd        求多 ...

  3. Hibernate Validator--创建自己的约束规则

    尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约束条件. 3.1. 创 ...

  4. C++模板特化编程

    在C++中,模板特化是除了类之外的一种封装变化的方法.模板特化可以通过编译器来对不同的模板参数生成不同的代码. 模板特化通常以模板结构体作为载体.常用技法包括:类型定义.静态成员常量定义和静态成员函数 ...

  5. 15 Practical Grep Command Examples In Linux / UNIX

    You should get a grip on the Linux grep command. This is part of the on-going 15 Examples series, wh ...

  6. Nodejs调试技术

    基于Chrome浏览器的调试器 既然我们可以通过V8的调试插件来调试,那是否也可以借用Chrome浏览器的JavaScript调试器来调试呢?node-inspector模块提供了这样一种可能.我们需 ...

  7. 手机连接fiddler后,浏览器无法打开网页或者fiddler抓取不到手机应用相关数据的情况

    关于手机如何连接fiddler,网上有很多教程,我暂时就不写了 今天在使用fiddler的过程中,发现fiddler突然无法抓取移动端应用的数据包,再三确认连接无误.因此就开始了解决之旅 起因是安卓手 ...

  8. PHP获取原生POST数据

    To get the Raw Post Data: <?php $postdata = file_get_contents("php://input"); ?> 参考官 ...

  9. kingadmin

    kingadmin 是一个模拟 Django admin 开发的后台管理系统,可以用来嵌套在其他的项目中作为单独的 app 程序存在. 执行流程 1.项目启动,开始执行 app_setup.py 文件 ...

  10. 001.SQLServer高可用简介

    一 SQLServer高可用集群相关概念 1.1 Windows故障转移群集 Windows故障转移群集是由多个服务器组成的共同提供某高可用服务,该服务用于防止单台服务器故障导致服务失效.故障转移群集 ...