描述

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. go实现简单的tcp编程

    服务端的代码 package main import ( "fmt" "net" ) func main () { fmt.Println("star ...

  2. Java HeapSort

    Java HeapSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational & ...

  3. springboot mvc自动配置(三)初始化mvc的组件

    所有文章 https://www.cnblogs.com/lay2017/p/11775787.html 正文 在springboot mvc自动配置的时候,获得了DispatcherServlet和 ...

  4. restTemplate源码解析(一)构造restTemplate的Bean实例

    所有文章 https://www.cnblogs.com/lay2017/p/11740855.html 正文 构造一个restTemplate的Bean实例很容易,只需这样配置 @Bean publ ...

  5. 记一次Git提交报错的问题

    通常代码版本控制的步骤是: 在代码版本控制平台新建一个仓库 clone远程仓库到本地 开始编码,然后是一系列add,commit,push 我的步骤是: 在远程代码版本管理平台新建一个仓库 在本地新建 ...

  6. express 和 vue-cli 的博客网站

    已经上传到github地址:https://github.com/13476075014/node-vue/tree/master/mynodeproject/15.TimeBlog # 个人博客系统 ...

  7. js大数计算之展示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 使用私有api实现自己的iphone桌面,并根据app的使用次数对app排序

    使用<iphone SprintBoard部分私有API总结>中提到的api,除了能对app运行次数进行监控以外,还可以实现自己的iphone桌面,并根据app 的使用次数对app图标进行 ...

  9. 逐步探究ObjC的Weak技术底层

    前言 之前的文章有说过 Atomic 原子操作的原理,其作为一个特殊的修饰前缀,影响了存取操作. 在属性修饰定义中,还有另一类修饰前缀,他们分别是 strong weak assign copy,这些 ...

  10. 2.原子变量 CAS算法

    前面提到,使用volatile无法保证 变量状态的原子性操作,所谓原子性,就是不可再分 如:i++的原子性问题,i++ 的操作实际上分为三个步骤  "读-改-写" (1)保存i的值 ...