LeetCode OJ--Search Insert Position
https://oj.leetcode.com/problems/search-insert-position/
数组有序,给一个数,看它是否在数组内,如果是则返回位置,如果不在则返回插入位置。
因为原数组有序,所以使用二分查找。
注意:如果是要插入这个元素的话,当target元素小于begin时候,是在begin位置上插入,如果大于则在begin+1位置上插入。
要测试数组为空,或者只有一个元素时候情况。
#include <iostream>
using namespace std; class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(n==)
return ;
int begin = ;
int end = n-;
int middle = (begin + end)/;
while(begin<end)
{
if(target == A[middle])
return middle;
else if(target<A[middle])
end = middle - ;
else
begin = middle+;
middle = (begin + end)/;
}
if(A[begin] >= target )
return begin;
return begin +; }
}; int main()
{
int arr[] = {,,,};
Solution myS;
cout<<myS.searchInsert(arr,,);
}
LeetCode OJ--Search Insert Position的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [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 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 【题解】【数组】【查找】【Leetcode】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 解决思路
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 (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- 基于idea创建Tomcat远程调试
编辑完catalina文件后重启tomcat
- 201621123080 《Java程序设计》 第7周学习总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 事件 事件源 事件监听器 事件处理方法 1.2 任意 ...
- 【linux】【CPU】【x86】平台说明
节选自 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_basic/0520rpm_and_srpm_1.php 操作硬件平台:这是个很好玩的地 ...
- ultraedit编辑器破解版下载
ultraedit一款功能丰富的网站建设软件,需要的朋友可以看看. 百度百科:UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足 ...
- 3.layhm框架的流程与Boot类启动
思路 在项目根目录里新建好对应的目录 cmd里在项目根目录里,composer init初使化,一路回车 把要自动加载的文件和目录定在composer.json文件的autoload里,file是自动 ...
- 用Python对微信好友进行简单统计分析,获取好友的基本信息!
早些日子有人问我我的微信里面有一共多少朋友,我就随后拉倒了通讯录最下面就找到了微信一共有多少位好友.然后他又问我,这里面你认识多少人?这一句话问的我很无语.一千多个好友我真的不知道认识的人有多少. ...
- CodeForce:732B-Cormen — The Best Friend Of a Man
传送门:http://codeforces.com/problemset/problem/732/B Cormen - The Best Friend Of a Man time limit per ...
- source insight
关于source inlight的版本 http://www.camnpr.com/archives/559.html 最新版本 http://www.sourceinsight.com/upda ...
- selenium2基本控件介绍及其代码
输入框:input 表现形式: 1.在html中一般为:<input id="user" type="text"> 主要操作: ...
- Xpath - Xpath定位
selenium 提供的xpath定位方法名为:find_element_by_xpath(xpath表达式) Xpath基本定位语法: / 绝对定位,从根节点选取 // 相对定位,从匹配选择的当前 ...