【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 ...
随机推荐
- php中empty(), is_null(), isset()函数区别
empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...
- LtUpload上传组件
<?php/** * The Upload class * @author Alex Lee <iuyes@qq.com> * @license http://opensource. ...
- Thinkpad 小紅點設定
因为我只需要这么多设置,所以就只写这么多了sudo gedit /etc/rc.local echo -n 240 > /sys/devices/platform/i8042/serio1/se ...
- DevExpress GridControl 部分用法
1.GridControl赋值:this.GridControl1.DataSouce=dt; 2.GridContro总合计及分组合计: 常规总合计直接RunDesigner-Group Summa ...
- POJ--1416
#include <stdio.h> #include <stdlib.h> ]={};//代表有没有切割的数组 ;//输入的要被切割的数字 ]={};//切完输出的数组成的数 ...
- 第十八章 数据访问(In .net4.5) 之 I/O操作
1. 概述 本章内容包括 文件操作.流操作.读写网络数据 以及 异步I/O操作. 2. 主要内容 2.1 文件操作 ① 使用 Drive 和 DriveInfo 访问磁盘信息. DriveInfo[] ...
- 008-python基础-数据类型
一.基本数据类型: 数字 int 字符串 str 布尔值 bool 真或假 1或0 列表 list 元组 tuple (不可变列表) 字典 dict (无序)
- Python科学计算利器——Anaconda
(搬运自我在SegmentFault的博客) 最近在用Python做中文自然语言处理.使用的IDE是PyCharm.PyCharm确实是Python开发之首选,但用于科学计算方面,还略有欠缺.为此我尝 ...
- java中的排序
排序是数据结构中重要的一个部分,也是在实际开发中最易遇到的问题之一,当然了,你也可以不考虑这些排序的算法,直接把要排序的数据insert到数据库中,用数据库的order by再select一下,也能产 ...
- FineUI PK DWZ
一.概述 FineUI(ExtAspNet)是基于 jQuery / ExtJS 的 ASP.NET 控件库. DWZ是基于JQuery的一个客户端框架. 二.比较 三.总结 这两个东西实质都是对控件 ...