【leetcode】35-Search Insert Position
problem
一种容易想到的是暴力破解法,一种是常用的二分法。
暴力破解法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的更多相关文章
- 【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 ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- 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 ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 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 ...
- 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 ...
- 【LintCode】060.Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
随机推荐
- Qt_自定义菜单
一.右键菜单 右键菜单实现:通过重写contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美实现! 重写voidcontextMen ...
- 微信小程序初见+nodejs服务端 (一个简单的博客)
推荐网址: 腾讯云快速开发(nodejs前后端):https://developers.weixin.qq.com/miniprogram/dev/qcloud/qcloud.html#%E5%AF% ...
- Python3+BaiduAI识别高颜值妹子图片
一.在百度云平台创建应用 为什么要到百度云平台创建应用,首先来说是为了获取获取access_token时需要的API Key和Secret Key 至于为什么需要API Key和Secret Key才 ...
- 用WebStorm进行Angularjs 2的开发
环境准备: WebStorm开发工具 https://pan.baidu.com/s/1o8maQLG 提取密码(加群获取599606903) nodejs https://nodejs.org ...
- PHP中的Trait方法
<?php /* * 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. * Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制. * Trait ...
- 1.python函数式编程-map函数
编程方法论 面向过程 函数式 面向对象 面向过程 将编程过程拆分成多个步骤,在函数中按照每个步骤进行编程: 函数式编程 编程语言定义的函数+数学意义的函数 1.不可变,不用变量保存状态,不修改变量: ...
- Win10系列:VC++绘制位图图片
在使用Direct2D绘制图片的过程中,通过IWICImagingFactory工厂接口来得到绘制图片所需要的资源.本小节将介绍如何通过IWICImagingFactory工厂接口得到这些资源,并使用 ...
- 双向链表--首页大小不一卡片排序 --- react --- js
1.4中类型(grid_type)的卡片:1:大方块:2:竖长块:3:横长块:4:小方块 var order = 0; // 创建链表 function List(head) { this.head ...
- python学习 面向对象高级编程
---恢复内容开始--- 面向对象编程---oop,是一种编程思想,oop把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数 ...
- bzoj3277
题解: 后缀自动机 然后抄了一发题解 可以看看这个博客:http://blog.csdn.net/clover_hxy/article/details/53861268 代码: #include< ...