【Search Insert Position 】cpp
题目:
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0
代码:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int begin = ;
int end = nums.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target ) return mid;
if ( nums[mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
return begin;
}
};
tips:
二分查找。如果没有找到,注意返回的值是begin还是end,找几个corner case测试一下。
===========================================
第二次过这道题,二分查找。找一侧的边界。
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int begin = ;
int end = nums.size()-;
while ( begin<=end )
{
int mid = (begin+end)/;
if ( nums[mid]==target ) return mid;
if ( nums[mid]>target )
{
end = mid-;
}
else
{
begin = mid+;
}
}
return begin;
}
};
【Search Insert Position 】cpp的更多相关文章
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- 【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:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
随机推荐
- 判断手机,pc方式登录
<script type="text/javascript"> function browserDetect() { var ...
- c3p0配置xml
c3p0-config.xml <c3p0-config> <default-config> <property name="automaticTestTabl ...
- Apache实现Web Server负载均衡
修改服务器A上apache的http.conf文件: 首先,加载相应的代理模块,去掉以下模块前面的#号: LoadModule proxy_module modules/mod_proxy.soLoa ...
- js 实现栈
function Stack() { this.dataStore = []; this.top = 0; this.push=push; this.pop=pop; this.peek=peek; ...
- 安卓手机的touchend事件不触发问题
问题描述 $(document).on("touchstart touchmove",".btn-highlight",function(event){ $(t ...
- 室内净化ThinkPHP复习
"$_GET[id]"这个是和$_GET['id']一样的 foreach的是 name 和 item if(!empty($_GET['id'])){ $where.= &quo ...
- Oracle存储过程学习备忘
之前的项目使用存储过程很少,但在实际的项目中,存储过程的使用是必不可少的. 存储过程是一组为了完成特定功能的SQL 语句 集,经编译后存储在数据库中:存储过程创建后,一次编译在程序中可以多次调用,对安 ...
- Python核心编程--学习笔记--8--条件与循环
本章讲述if.while.for以及与他们搭配的else.elif.break.continue.pass等语句. 1 if语句 语法:三部分——关键字if.条件表达式.代码块.(记住冒号) if c ...
- SRF之日志和异常
日志: 日志功能采用log4net实现 log4配置文件在站点目录下的log4net.config. 调用log4写日志的代码如下: log4net.ILog logger = log4net.Log ...
- hdu 4609 3-idiots <FFT>
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 给定 N 个正整数, 表示 N 条线段的长度, 问任取 3 条, 可以构成三角形的概率为多 ...