leetcode 刷道题 70 earch Insert Position 二进制搜索插入位置
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
二分查找。一点小的差别就是当数组不含目标数字时,返回应该插入的位置。
AC code:
class Solution {
public:
int searchInsert(int A[], int n, int target)
{
int begin=0,end=n-1,mid=0;
while(begin<=end)
{
mid=(begin+end)/2;
if(A[mid]==target)
return mid;
if(A[mid]>target)
end=mid-1;
else
begin=mid+1;
}
if(A[mid]>target)
return mid;
return mid+1;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
leetcode 刷道题 70 earch Insert Position 二进制搜索插入位置的更多相关文章
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- Leecode刷题之旅-C语言/python-35.搜索插入位置
/* * @lc app=leetcode.cn id=35 lang=c * * [35] 搜索插入位置 * * https://leetcode-cn.com/problems/search-in ...
- 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(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- LeetCode OJ: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. I ...
- 【leetcode刷题笔记】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- tokumx经营报表
#见数据库列表 show dbs #切换/创建数据库(当创建一个集合(table)的时候会自己主动创建当前数据库) use admin; #添加用户 db.addUser("zhoulf ...
- Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法)
Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法) ...
- Codeforces 9A-Die Roll(意甲冠军)
A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...
- JSplitPane demo
package example; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; imp ...
- RH033读书笔记(6)-Lab 7 Standard I/O and Pipes
Lab 7 Standard I/O and Pipes 1. [student@stationX ~]$ cat /proc/cpuinfo /proc/meminfo 2. [student@st ...
- uboot的relocation原理具体分析
近期在一直在做uboot的移植工作,uboot中有非常多值得学习的东西.之前总结过uboot的启动流程,但uboot一个非常核心的功能没有细致研究.就是uboot的relocation功能. 这几天研 ...
- Chapter 1 Securing Your Server and Network(2):管理服务的SIDs
原文出处:http://blog.csdn.net/dba_huangzj/article/details/37927319 ,专题文件夹:http://blog.csdn.net/dba_huang ...
- Linux下一个Nginx安装步骤
一个.下载pcre 官网下载:http://www.pcre.org/ # wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre ...
- FileZilla 错误425 Can't open data connection 读取目录列表失败
新装FileZilla FTP Server,设置好后,客户端能连接,但是出Error:[读取目录列表失败]:同时,服务端出Error:[425 Can't open data connection] ...
- Http与协议TCP协议简单易懂
于C#编写代码,很多时候会遇到Http协议或TCP合约,这里做一个简单的了解. TCP对应于该传送层协议,和HTTP对应于应用层协议,从本质上讲,两者是没有可比性.Http该协议是基于TCP之上的,当 ...