描述

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(vector<int>& nums, int target) {
for(int i = 0; i != nums.size(); ++i){
if(target > nums[i]) continue;
else return i;
}
return nums.size();
}
};

更优雅的解法是二分查找:

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int low = 0,high = nums.size() - 1;
while(low <= high){
int mid = low + (high - low) / 2;
if(nums[mid] < target)low = mid + 1;
else high = mid - 1;
}
return low;
}
};

leetcode解题报告(11):Search Insert Position的更多相关文章

  1. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  2. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  3. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  4. LeetCode Arrary Easy 35. Search Insert Position 题解

    Description Given a sorted array and a target value, return the index if the target is found. If not ...

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

  6. LeetCode记录之35——Search Insert Position

    这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...

  7. LeetCode: Search Insert Position 解题报告

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  8. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  9. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  10. Leetcode35 Search Insert Position 解题思路(python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...

随机推荐

  1. vue 中使用rem布局

    在使用vue-cli搭建好项目框架后,在目录结构的index.html文件中添加一段js代码: fnResize(); window.onresize = function () { fnResize ...

  2. SAS学习笔记29 logistic回归

    变量筛选 当对多个自变量建立logistic回归模型时,并不是每一个自变量对模型都有贡献.通常我们希望所建立的模型将具有统计学意义的自变量都包含在内,而将没有统计学意义的自变量排除在外,即进行变量筛选 ...

  3. SAS学习笔记15 SAS导入数据(import txt csv xlsx spss)

  4. 解决使用绝对定位absolute后,margin:0 auto居中方法失效(转)

    https://blog.csdn.net/qq_40678503/article/details/82780680

  5. kNN(K-Nearest Neighbor)最邻近规则分类(转)

    KNN最邻近规则,主要应用领域是对未知事物的识别,即判断未知事物属于哪一类,判断思想是,基于欧几里得定理,判断未知事物的特征和哪一类已知事物的的特征最接近: K最近邻(k-Nearest Neighb ...

  6. MySQL之字符函数

    MySql中提供一些函数对我们的开发有很多的帮助,下面就把MysQL提供的一些常用函数整理下,首先是字符处理函数: 1.CONCAT() 用法:字符串链接函数,将字符串字段连结在一块 举例: sele ...

  7. Centos超详细安装步骤

    Linux中三大主流操作系统 Ubuntu 优点:用户界面友好.工具完善 缺点:vps(虚拟服务器)成本较高.不具备商业化服务器操作系统 Centos--目前常用centos6.x,centos7.x ...

  8. linux添加地址映射

  9. 用js刷剑指offer(二维数组中的查找)

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  10. linq学习(二)

    百度搜索:C# linq查询新对象 直接从list中查出一个新对象集合. 文章:https://blog.csdn.net/lym940928/article/details/80278783 fro ...