Leetcode_35_Search Insert Position
本文是在学习中的总结,欢迎转载但请注明出处: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相关推荐
- LeetCode - 35. Search Insert Position
35. Search Insert Position Problem's Link ---------------------------------------------------------- ...
- LeetCode算法入门- Search Insert Position -day19
LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...
- leetcode -- Search Insert Position
2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...
- 【二分法】LeetCode 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
- 7.2—查找—Sear Insert Position
描述 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 二分法:search insert position 插入位置
问题描述: 给定一个排序数组nums(无重复元素)与目标值target,如果target在nums里 出现,则返回target所在下标,如果target在nums里未出现,则返回target应该 插入 ...
- Leetcode: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 ...
- 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
最新文章
- android 线程list.add,ListenableWorker 中的线程处理
- mysql union all 别名_MySQL Union合并查询数据及表别名、字段别名用法分析
- ZOJ 38727(贪心)
- 详解ADO.NET操作数据库合力创享
- c语言如何获取串口列表,如何通过串口来读写数据,请教达人
- LeetCode 198. 打家劫舍(DP)
- 【java基础】——java枚举类型基本介绍
- 简说设计模式——策略模式
- 免费计算机网络基础ppt,计算机网络基础
- 【PS】怎么让一张模糊图片更清晰?
- Visio 画流图 程序流图 斜线
- VS2012(2013、2015) OpenCV “HEAP:Invalid Address specified to RtlValidateHeap( 000D0000, 019FEF18 )
- 【论文翻译 IJCAI-20】Heterogeneous Network Representation Learning 异构网络表示学习
- c语言dht网络爬虫,用Node.js实现一个DHT网络爬虫,一步一步完成一个BT搜索引擎(一)...
- lisp画配筋_请教:CAD中如何用lisp画四棱台?
- 怎样使局域网和无线(WiFi)共存
- java中的定时器的实现样例
- python软件是干嘛的-Python到底能做什么?
- ISO的国家代码和语言代码
- [汇编语言]变量的定义