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. 【LeetCode】矩阵操作

    1. 矩阵旋转 将 n × n 矩阵顺时针旋转 90°. 我的思路是 “ 从外到内一层一层旋转 ”. 一个 n × n 矩阵有 (n + 1) / 2 层,每层有 4 部分,将这 4 部分旋转. 顺时 ...

  2. Space Ant

    Space Ant The most exciting space discovery occurred at the end of the 20th century. In 1999, scient ...

  3. loj 10004 智力大冲浪

    智力大冲浪 题目描述: 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的.接下来主持人宣布了 ...

  4. 【转】Vue 脱坑记 - 查漏补缺(汇总下群里高频询问的xxx及给出不靠谱的解决方案)

    前言 文章内容覆盖范围,芝麻绿豆的破问题都有,不止于vue; 给出的是方案,但不是手把手一字一句的给你说十万个为什么! 有三类人不适合此篇文章: “喜欢站在道德制高点的圣母婊” – 适合去教堂 “无理 ...

  5. python3实现的rtsp客户端脚本

    一.说明 此客户端使用python3编写 此客户端实现RTSP的OPTIONS, DESCRIBE, SETUP , PLAY, GET_PARAMETER,TEARDOWN方法,未实现ANNOUNC ...

  6. paysoft 网关出现EntityRef:expecting;的错误。

    paysoft 网关出现EntityRef:expecting;的错误. 原因是传进去的url里有&字符,之前是自动转义的,现在要手工改成& http://www.runoob.com ...

  7. mac crontab时间断内随机时间执行定时任务

    首先需要了解crontab使用,这里不多,主要是时间断内随机时间: 然而crontab 并没有具体方法实现时间段内随机时间执行,我的办法如下: 这里测试一个例子: 执行一个数据存文件python脚本, ...

  8. 2-Servlet和servletContext

    2018-08-09   22:34 * Servlet(好好学)  * 动态WEB的资源.   * 什么是Servlet    * 实现Servlet接口,重写5个方法.           * S ...

  9. 逆袭之旅DAY16.东软实训.Oracle.序列

    2018-07-12 14:07:44 序列 序列1.创建序列create sequence 序列名 [increment by n] ---步长 [start with n] ---序列的起始值 序 ...

  10. 解决Tomcat v6.0 Server at localhost was unable to start within 45 seconds

    eclipse 中tomcat启动超时报错如下: Starting Tomcat v7.0 Server at localhost' has encountered a problem Server ...