LeetCode Search Insert Position (二分查找)
题意:
给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标。
思路:
来一个简单的二分查找就行了,注意边界。
1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums,int target) 4 { 5 int L=0, R=nums.size(); 6 while(L<R) 7 { 8 int mid=R-(R-L+1)/2; 9 if(nums[mid]>=target) R=mid; 10 else L=mid+1; 11 } 12 return R; 13 } 14 };
AC代码
转载于:https://www.cnblogs.com/xcw0754/p/4921625.html
LeetCode Search Insert Position (二分查找)相关推荐
- leetcode -- Search Insert Position
2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- 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 ...
- [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
原题链接:http://oj.leetcode.com/problems/search-insert-position/ 题意描述: Given a sorted array and a target ...
- 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 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
最新文章
- 将Tensor核心引入标准Fortran
- 融云任杰:强互动,RTC 下一个“爆点”场景 | 拟合
- 范式变革与规律涌现:世界科技发展新趋势
- 使用ASIHTTPRequest 编译提示找不到libxml/HTMLparser.h的解决方法
- 分布式缓存使用介绍MemCache
- C# 生成编号的方法
- java冒泡排序找最大的值_(13)數組操作:遍歷、輸出最大值、冒泡排序、選擇排序,java已有的排序方法、折半查找...
- 逆向工程核心原理学习笔记(五):实战“打补丁方法”修改字符串
- observable_Java Observable deleteObserver()方法与示例
- 又又叒更新,Win 12要来了?
- innosetup 同名文件替换_运维工程师必备命令之文件管理
- 用Python一键批量将任意结构的CSV文件导入 SQLite_用 Python 使用 Google Colab?岂止是炫酷...
- 厉害了!这款百度炼丹神器绝了!
- signature=70706aa92e6532c935fb1dce5adc16b3,Thunder signatures
- LCD12864新手入门
- 入职美团定级P7,总结2022年最新最全180道高级岗面试题及答案
- MySQL复制表数据到新表的方法 亲测可用
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- ContestHunter #26 B 玩骰子
- 【ZJOJ1321】灯
热门文章
- java中gc是怎么工作的_java中的GC(gabage collection)如何工作
- 解析java源代码_一步步解析java执行内幕
- android gps 锁屏更新坐标_MIUI内测版更新日志解析,以及动画解说!
- 螺旋矩阵Python解法
- 1亿以内素数的个数_神奇的素数
- php mysqli不识别,不识别数据库PHP MYSQLi中的密码
- python摄像头人脸识别代码_python 实现摄像头人脸识别
- 微型计算机原理课程设计计算器,微机原理课程设计简易计算器的设计.docx
- php nginx配置404页面,Nginx实现404页面的几种方法
- eclipse maven项目 class类部署不到tomcat下_Spring Boot的两种部署方式:jar包和war包