LeetCode T35 Search Insert Position
文章目录
- 题目地址
- 题目描述
- 思路
- 题解
题目地址
中文:https://leetcode-cn.com/problems/search-insert-position/
英文:https://leetcode.com/problems/search-insert-position/
题目描述
Given a sorted array of distinct integers 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.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Example 4:
Input: nums = [1,3,5,6], target = 0
Output: 0
Example 5:
Input: nums = [1], target = 0
Output: 0
Constraints:
- 1 <= nums.length <= 10^4
- -10^4 <= nums[i] <= 10^4
- nums contains distinct values sorted in ascending order.
- -10^4 <= target <= 10^4
思路
二分变体
参考→https://blog.csdn.net/qq_31650113/article/details/112390133
经典二分代码最后返回值部分稍加改动即可。
题解
class Solution {public static int searchInsert(int[] nums, int target) {int L = 0;int R = nums.length - 1;int mid = L+(R-L)/2;while( L <= R) {mid = L+(R-L)/2;if( target == nums[mid] )return mid;else if( target > nums[mid])L = mid + 1;elseR = mid - 1;}if(nums[mid]>target) return mid;else return mid+1;}
}
LeetCode T35 Search Insert Position相关推荐
- 【二分法】LeetCode 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
- LeetCode - 35. Search Insert Position
35. Search Insert Position Problem's Link ---------------------------------------------------------- ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- 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
1.问题 Given a sorted array and a target value, return the index if the target is found. If not, retur ...
- [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 35. Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [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 ...
最新文章
- 太厉害了!目前 Redis 可视化工具最全的横向评测
- 大众点评订单分库分表实践之路
- 利用WireShark分析由Ping产生的Internet 控制报文协议(ICMP)
- android分辨率比例成像,像素不是唯一 决定成像效果你必知的真相
- 不管什么事,只要用心做,总不会太差!
- 前端为什么非要动静分离 说一下CDN托管的意义
- 行为像指针的类的对象每次作为参数传入函数或者传出函数时都要小心
- 写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。
- 服务器与普通电脑的区别?
- 项目组【网站】的项目
- libtool: Version mismatch error. This is libtool 2.4.6, but the definition of this LT_INIT
- 前端进阶篇——02、CSS和JS
- 在线c语言编程网站_学编程有哪些好的网站推荐?
- AD7705/TM7705使用注意事项
- win10如何强制删除文件
- Kingbase8开发版,启动提示FATAL: XX000: max_connections should be less than orequal than 10
- 小众软件:画简洁风格的原型图
- php错误报告和调试
- LruCache的基本用法
- 最新的CC2642R与CC2640、CC2640R2F有什么改进?
热门文章
- google chrome常用的一些快捷键
- java shell spool_Linux_spool命令
- LeetCode 5955. 摘水果 题目解析
- 知识图谱—关系抽取—远程监督—OpenNRE(一)
- logstash时间戳转换
- fedora 18 19 20 21 22 23 怎么安装中文语言包
- 全球及中国涂料用丙烯酸树脂行业研究及十四五规划分析报告
- 高德地图api @amap/amap-jsapi-loader封装成方法(定位、点标记、路径规划、搜索等) 适用于vue等框架
- 内存溢出(oom)和内存泄漏(leak)
- 张伟伟-层次1 Mean_shift聚类算法和其他的聚类算法