【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 ...
随机推荐
- Mysql优化要点
优化MySQL Mysql优化要点 慢查询 Explain mysql慢查询 注意事项 SELECT语句务必指明字段名称 SELECT *增加很多不必要的消耗(cpu.io.内存.网络带宽):增加了使 ...
- LSTM UEBA异常检测——deeplog里其实提到了,就是多分类LSTM算法,结合LSTM预测误差来检测异常参数
结合CNN的可以参考:http://fcst.ceaj.org/CN/article/downloadArticleFile.do?attachType=PDF&id=1497 除了行为,其他 ...
- which/whereis/locate/find的区别
which--在$PATH目录下查找文件 whereis--在预定目录下(whereis -l查看)查找文件 locate--在数据库中查找目录或文件 find--遍历目录查找文件 说明: 1.关于w ...
- 微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
几天不动代码,再运行成这样了, {errMsg: "getLocation:fail Error: tunneling socket could not…d, cause=connect E ...
- python3线程启动与停止
转自: https://blog.csdn.net/weixin_38125866/article/details/76795462 https://www.cnblogs.com/lcchuguo/ ...
- studio配置本地gradle-x.x.x-all.zip
在引入别的项目时,一般会突然一直卡在了building...,下载网络gradle. 我们从网络下载gradle.zip到本地,通过将.\项目\gradle\wrapper下的gradle-wrapp ...
- JQuery操作input
// 不可编辑 $("#id").attr("disabled","disabled"); $("#id").remov ...
- latex 公式距离
\setlength{\abovedisplayshortskip}{0cm} 公式和文本之间的间距 \setlength{\belowdisplayshortskip}{0cm} \setlengt ...
- php读取excel时间42930转化为时间然后正则验证时间是否通过
excel时间 function exceltimtetophp($days,$time=false) { if(is_numeric($days)) { //凯撒日计数,要把我们运用的从1970年开 ...
- 学习笔记-AngularJs(四)
之前学习的事视图与模版,我们在控制器文件中直接定义一个数组,让其在模版文件中用ng-repeat指令构造一个迭代器,定义的数组http://t.cn/RUbL4rP如同以下: $scope.phone ...