【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 ...
随机推荐
- Windows和Linux创建软链接和硬链接
1.Wondows创建软链接和硬链接 mklink [/d] [/h] link target /d--创建目录软链接:默认为文件软链接:创建目录链接时必须使用该选项不然创出的软链接无效 /h--创建 ...
- 针对unicode对象---检测字符串是否只由数字组成
- 转 Deep Learning for NLP 文章列举
原文链接:http://www.xperseverance.net/blogs/2013/07/2124/ 大部分文章来自: http://www.socher.org/ http://deepl ...
- mysql sql_mode=only_full_group_by问题?
视图查询的时候本地数据库报错: 解决办法: 1.查看sql_mode select @@global.sql_mode STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_Z ...
- java 一些容易忽视的小点-控制语句
随机数 .Math.random()该方法用于产生一个0到1区间的double类型的随机数,但是不包括1 if-else循环语句 如果if语句不写{},则只能作用于后面的第一条语句 switch语句 ...
- 在项目中使用 SCSS
背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...
- js 操作dom
childNodes 返回当前元素所有子元素的数组 parentNode 返回元素的父节点 document.createElement(tagName) 文档对象上的createElement方法可 ...
- day7-python打开文件方式
文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 基本操作 import codecs #主要用来解决乱码问题 f = codecs.open('1. ...
- JavaMail发送邮件、带附件邮件(完整版)
工程目录如下: 1.准备javaMail需要的两个Jar包:mail.jar.activation.jar,然后add to build path 2.QQ邮箱开启SMTP服务,开启后,它会给你一串授 ...
- RabbitMQ 队列、消息持久化
RabbitMQ的消息队列的持久化是一个很不错的功能,设置也非常简单.如下代码: 1.设置队列持久化(在声明队列的时候设置) channel.QueueDeclare(queue: "q.l ...