Search Insert Position:二分搜索
来源:互联网 发布:中国农业生产总值数据 编辑:程序博客网 时间:2024/05/22 13:58
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[] nums, int target) { if(nums.length==0||nums==null) return -1; int l = 0; int r = nums.length-1; while(l<=r){ int mid = (l+r)/2; if(nums[mid]==target) return mid; if(nums[mid]<target){ l = mid +1; }else{ r = mid -1; } } return l; }}
阅读全文
0 0
- Search Insert Position:二分搜索
- leetcode-二分搜索:Search Insert Position
- (LeetCode)Search Insert Position --- 二分搜索
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- Leetcode 二分查找 Search Insert Position
- LeetCode @ Search Insert Position 二分查找
- 二分查找——Search Insert Position
- LeetCode 35. Search Insert Position 二分查找
- (二分查找)LeetCode#35. Search Insert Position
- 35. Search Insert Position (二分)
- 【LintCode】Search Insert Position 搜索插入位置
- 搜索插入位置:Search Insert Position
- leetcode——Search Insert Position 二分查找 binary search
- [LeetCode]Search Insert Position (二分查找总结) Java Version
- Search Insert Position Leetcode 非二分查找法
- leetcode 35. Search Insert Position-二分查找|递归|非递归
- LeetCode 35 Search Insert Position(搜索并插入)
- LeetCode 35. Search Insert Position(搜索插入点)
- 1068. 万绿丛中一点红(20)
- 经典试题(1)
- [Archive]更改ORACLE默认归档路径
- Wannafly模拟赛4 B.Distance 最大曼哈顿距离
- 多线程编程之ReentrantReadWriteLock
- Search Insert Position:二分搜索
- Node.js启程之旅(一)之安装和检查
- 经典试题(2)
- 自己写的一个猜拳小游戏(C语言)
- JQuery实用控件
- 栈和队列 生成窗口最大数组
- Python错误:bs4.FeatureNotFound: Couldn't find a tree builder with the features you
- Linux下TCP最大连接数受限问题
- 使用SurfaceView+Medaplayer+SeekBar完成视频的播放加进度条