leetcode25—Search Insert Position
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.
Example 1:
Input: [1,3,5,6], 5 Output: 2
Example 2:
Input: [1,3,5,6], 2 Output: 1
Example 3:
Input: [1,3,5,6], 7 Output: 4
Example 4:
Input: [1,3,5,6], 0 Output: 0
想法:使用二分查找法,找出target应该在的位置。
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
== nums.size()){
;
}
))
;
if(target > nums.back()){
return nums.size();
}
;
;
){
;
if(nums.at(mid) == target)
return mid;
if(nums.at(mid) < target){
left = mid;
}else{
right = mid;
}
}
return right;
}
};
leetcode25—Search Insert Position的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- 你不知道的JavasScript上篇·第五章·原型·上
1.[[Prototype]] JS中的对象有一个特殊的[[Prototype]]内置属性,其实就是对于其他对象的引用.几乎所有的对象在创建时这个属性都被赋予一个非空的值 (proto) var my ...
- 网络基础 利用vnc viewer访问在vmware虚拟机上的linux
利用vnc viewer访问在vmware虚拟机上的linux by:授客 QQ:1033553122 Linux服务器为架设在VMware之上的虚拟机,那么可以直接使用VMware自带的vnc,而不 ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- alpha阶段 代码结构及技术难点简介
我们的产品是安卓端app,所以目前主要就是用Android Studio来进行代码开发. Android Studio的项目的结构还是比较清晰的,如下图,主要就是java文件夹内的代码部分(.java ...
- keystone令牌三种生成方式
keystone认证方式:UUID.PKI.Fernet; 知识点复习: 通俗的讲,token 是用户的一种凭证,需拿正确的用户名/密码向 Keystone 申请才能得到.如果用户每次都采用用户名/密 ...
- Windows安装PostgreSQL数据库 无法初始化数据库问题
背景 由于项目的需要,使用PostgreSQL数据库,因此在Windows上安装PostgreSQL数据库.但是在安装后,无法访问本地数据库,这个时候查看/data目录,没有任何文件.而且安装过程中, ...
- LeetCode题解之 Increasing Order Search Tree
1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...
- LeetNode 题解之Reverse Nodes in k-Group
1.题目描述 2.问题分析 这个题本质上还是按照链表翻转的思路来解,只是需要添加一些细节判断. 3.代码 class Solution { public: ListNode* reverseKGrou ...
- Debian 常用命令
换源 用中科大的比较快 deb http://mirrors.ustc.edu.cn/debian jessie main contrib non-free deb-src http://mirror ...
- mssql 监控随笔
性能监控列表: • Memory: Pages/sec ( 从硬盘上读取或写入硬盘的页数(参考值:00~20) • Physical Disk: % Disk time 或 Physi ...