本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43739647

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)题意为给定一个已排序的数组和一个整数,求该整数在数组中的位置。

(2)由于数组是已排序的,本题可以通过“二分查找”算法来寻找目标整数的位置。但需要注意对边界值判断,当目标整数小于数组第一个元素时,应返回0;当目标整数大于数组最后一个元素时,返回数组长度加1。该题还是比较简单,详情见下方代码。

(3)希望本文对你有所帮助。

算法代码实现如下:

/*** @author liqq*/
public class SearchInsert {public int searchInsert(int[] A, int target) {int high = A.length - 1;int low = 0;int mid = (high + low) / 2;if (A[low] > target)return 0;if (A[high] < target)return high + 1;while (mid >= 0 || mid <= high) {if (A[mid] > target) {if (A[mid] > target && A[mid - 1] < target) {return mid;} else {mid--;}} else if (A[mid] < target) {if (A[mid] < target && A[mid + 1] > target) {return mid + 1;} else {mid++;}} else {return mid;}}return -1;}
}

Leetcode_35_Search Insert Position相关推荐

  1. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  2. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  3. leetcode -- Search Insert Position

    2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...

  4. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  5. 7.2—查找—Sear Insert Position

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

  6. 二分法:search insert position 插入位置

    问题描述: 给定一个排序数组nums(无重复元素)与目标值target,如果target在nums里 出现,则返回target所在下标,如果target在nums里未出现,则返回target应该 插入 ...

  7. Leetcode: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. 35. Search Insert Position

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

最新文章

  1. android 线程list.add,ListenableWorker 中的线程处理
  2. mysql union all 别名_MySQL Union合并查询数据及表别名、字段别名用法分析
  3. ZOJ 38727(贪心)
  4. 详解ADO.NET操作数据库合力创享
  5. c语言如何获取串口列表,如何通过串口来读写数据,请教达人
  6. LeetCode 198. 打家劫舍(DP)
  7. 【java基础】——java枚举类型基本介绍
  8. 简说设计模式——策略模式
  9. 免费计算机网络基础ppt,计算机网络基础
  10. 【PS】怎么让一张模糊图片更清晰?
  11. Visio 画流图 程序流图 斜线
  12. VS2012(2013、2015) OpenCV “HEAP:Invalid Address specified to RtlValidateHeap( 000D0000, 019FEF18 )
  13. 【论文翻译 IJCAI-20】Heterogeneous Network Representation Learning 异构网络表示学习
  14. c语言dht网络爬虫,用Node.js实现一个DHT网络爬虫,一步一步完成一个BT搜索引擎(一)...
  15. lisp画配筋_请教:CAD中如何用lisp画四棱台?
  16. 怎样使局域网和无线(WiFi)共存
  17. java中的定时器的实现样例
  18. python软件是干嘛的-Python到底能做什么?
  19. ISO的国家代码和语言代码
  20. [汇编语言]变量的定义

热门文章

  1. AD域控的搭建与加入AD域
  2. windows AD域的特点
  3. 利用Qt来进行文件后缀的更改
  4. 图的无权最短路径算法
  5. 第32篇 网络(二)HTTP
  6. 【ACWing】2188. 无源汇上下界可行流
  7. 无线路由传输速率的秘密
  8. Nginx基本使用方法
  9. J2EE技术简单介绍
  10. JAVA 开发命名规范——阿里巴巴Java开发手册