leetcode - 35. Search Insert Position - Easy

descrition

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 1:
Input: [1,3,5,6], 0
Output: 0

解析

二分查找的实现。

code


#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class Solution{
public:
int searchInsert(vector<int>& nums, int target){
int ileft = 0, iright = nums.size() - 1;
while(ileft <= iright){
int imid = ileft + (iright - ileft) / 2;
if(nums[imid] == target)
return imid;
else if (nums[imid] < target){
ileft = imid + 1;
}else{
// nums[imid] > target
iright = imid - 1;
}
} return ileft;
}
}; int main()
{
return 0;
}

[array] leetcode - 35. Search Insert Position - Easy的更多相关文章

  1. [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 ...

  2. [leetcode 35] Search Insert Position

    1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. [LeetCode] 35. Search Insert Position ☆(丢失的数字)

    转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html    思路 Given a sorted array ...

  7. Leetcode 35 Search Insert Position 二分查找(二分下标)

    基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...

  8. Java [leetcode 35]Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  9. 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 ...

随机推荐

  1. thinkphp系列:类的自动加载是如何设计的

    在使用框架开发时,可以发现框架有很多核心类,却很少看到显示的引入某个文件的代码,这是因为框架都采用了类的自动加载机制,即使用到类时,框架会自动找到该类所在文件的位置并引入该文件.为了更容易看出代码思路 ...

  2. tensorflow初探

    TensorFlow是一个采用数据流图,用于数值计算的开源软件库.自己接触tensorflow比较的早,可是并没有系统深入的学习过,现在TF在深度学习已经成了"标配",所以打算系统 ...

  3. JS的数据类型及转换(还是基础的东西)

    朋友说我这是再自娱自乐,我只想说,你说的对

  4. VMware下设置Centos7联网与固定IP连接Xshell

    爱折腾的小伙伴应该经常会用Vmware安装一些虚拟机用于学习,但是比如装了Linux,经常操作的时候非常切换窗口的时候非常麻烦,所以很多人都会选择用Xshell来连接本地的Linux虚拟机,但是用Xs ...

  5. js中一些注意点 ps不断更新中....

    nextSibling 和 nextElementSibling 的区别 (previousSibling 和 previousElementSibling ) nextSibling 在IE8及以下 ...

  6. 盘点CSS中可以和不可以继承的属性

    CSS中可以和不可以继承的属性   一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定 ...

  7. Python Requests 库学习笔记

    概览 实例引入 import requests response = requests.get('https://www.baidu.com/') print(type(response)) prin ...

  8. iOS 视频播放方式整理

    初衷 多媒体这整个系列的文章自己也准备好开始整理了,先从视频音频最简单也是最常用的播放出发慢慢的往下深究,探索到底层的编码解码等等,这篇文章就从视频的播放这个最简单的说起. iOS的视频播放方式有几种 ...

  9. 算法提高 9-3摩尔斯电码 map

    算法提高 9-3摩尔斯电码 时间限制:1.0s   内存限制:256.0MB     问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...

  10. javac 小记

    javac 到底是什么? javac 就是一个编译器,它把 Java 源代码编译成 Java 字节码,即 JVM 能够识别的二进制形式的文件. javac 由什么构成? 词法分析器:识别源代码中的 J ...