problem

Search Insert Position

一种容易想到的是暴力破解法,一种是常用的二分法。

暴力破解法1(不推荐)

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int index = ;
for(size_t i=; i<nums.size()-; i++)
{
if( (target<nums[i])&&(i==)) return ;
else if(target==nums[i]) return i;
else if(target>nums[i] && target<nums[i+]) return i+;
}
if(target>nums.back()) return nums.size();
return nums.size()-; }
};

没想到竟然绕了这么复杂的一圈圈。。。

 暴力破解法2(推荐)

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for (int i = ; i < nums.size(); ++i) {
if (nums[i] >= target) return i;
}
return nums.size(); }
};

二分法(墙裂推荐)

binary search

参考

1.leetcode;

【leetcode】35-Search Insert Position的更多相关文章

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

  2. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  3. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

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

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

  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 OJ 35. Search Insert Position

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

  7. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  8. Leetcode No.35 Search Insert Position(c++实现)

    1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...

  9. LeetCode Problem 35:Search Insert Position

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

  10. 【LintCode】060.Search Insert Position

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

随机推荐

  1. [codechef July Challenge 2017] IPC Trainers

    IPCTRAIN: 训练营教练题目描述本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 ...

  2. mybatis.xml和mapper.xml的配置

    mybatis.xml和mapper.xml的配置 1.创建一个Source Folder 2.完成分包mapper和mybatis 3.创建mybatis.xml文档 4xml文档名 5.名字规范 ...

  3. VNC安装配置及连接(CentOS)

    1.安装VNC yum install -y tigervnc-server #安装VNC服务端,一般执行此句即可yum groupinstall -y 'X Window System' 'Desk ...

  4. Vscode extensions开发

    Vscode extensions开发   1◆ generatorcode install npm install -g yo generator-code     2◆ 步骤 yo code   ...

  5. JQuery进度条

    需要实现效果如下图. 页面代码<div class='progress_bar' data-color='#f66' data-per='"+list[i].percent+" ...

  6. ubuntu16.10安装搜狗输入法

    一.搜狗输入法安装 1.首先到搜狗输入法官网下载搜狗输入法,下载的是个deb文件. 搜狗输入法Linux版下载地址:http://pinyin.sogou.com/linux/?r=pinyin 2. ...

  7. pl/sql美化

    因为这个问题曾经浪费过俺很多时间,不过今天终于发现一个小技巧,分享给大家, 在上面DDL语句前后加上begin 和 end,哈哈,再美化下试试看,DDL被成功被美化了. 具体如下: begin---① ...

  8. linux初始化宏__init, __exit

    我们在内核中经常遇到初始化函数是这样定义的:static int __init init_func(); ,与普通函数相比,定义中多了__init.那么,__init是什么意思呢?还有与其匹配的__e ...

  9. 数据泵导入 ORA-31626

    Oracle,10G,数据泵导入时,报错如下: 解决方案:对当前用户做如下授权 . 具体操作:grant connect,resource to user;

  10. 前端vue项目-关于下载文件pdf/excel(三)

    最近在做一些需求,需要下载一些文件信息,最频繁的就是下载excel文件到本地了 看过了很多方法,做个整理吧哈哈哈哈 参考的文章链接: https://www.cnblogs.com/jiangweic ...