[LeetCode] Search 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
这题其实便是二分搜索,需要考虑没有找到的时候的情况。
#include <iostream>
using namespace std; class Solution {
public:
int searchInsert(int A[], int n, int target) {
if(n==) return ;
// if(target<=A[0]) return 0;
if(A[n-]<target) return n;
int l=,r=n-,m=;
while(l<=r){
m=(l+r)/;
// cout<<l<<" "<<m<<" "<<r<<endl;
if(A[m]==target) return m;
if(A[m]<target) l=m+;
else r=m-;
}
if(target<A[m]) return m;
return m+;
}
}; int main()
{
int a[]={,};
Solution sol;
for(int i=;i<=;i++)
cout<<i<<" "<<sol.searchInsert(a,sizeof(a)/sizeof(int),i)<<endl;
return ;
}
[LeetCode] Search Insert Position 二分搜索的更多相关文章
- 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: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [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] 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 t ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode——Search Insert Position
Description: Given a sorted array and a target value, return the index if the target is found. If no ...
- [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 Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
随机推荐
- 关于sql查询结果集的链接
开通博客有一段时间了,第一次博文.本身是个理工科的,没啥文采,就想着把平时遇到的问题记录下来,防止自己以后忘了还要去翻找. 今天看到同事写的代码,查询两张表里的数据,结果集类型是一样的.写了两条查询, ...
- php中处理字符串的常见函数
编写程序的时候,经常要处理字符串,最基本就是字符串的查找,在php检测字符串中是否包含指定字符串可以使用正则,如果你对正则不了解,那么有几个函数可以为您提供方便. 1. strstr strstr() ...
- js操作地址栏
//判断地址里是否有?号,如果没有就从最后一个/截到最后,如果有?就从最后一个/截至?号处 listTable.url = location.href.lastIndexOf("?" ...
- Flask初学者:URL(传参,请求,重定向)
URL传参: 良好的URL:视图函数对应的url以/结尾是一种良好url,因为用户在访问的时候无论他有没有加上最后这个斜杠,都是能访问到的,相反,视图函数的url没有以/结尾,用户访问的时候却加上了这 ...
- Bubblesort冒泡算法
最简单的算法,大家都知道两层for循环,中间加一个过渡用来交换数据 小例子: package com.neuedu.algorithm;//算法 public class Bubblesort { / ...
- Windows下安装配置SQLite和使用的教程
什么是SQLite SQLite是一款非常轻量级的关系数据库系统,支持多数SQL92标准.SQLite在使用前不需要安装设置,不需要进程来启动.停止或配置,而其他大多数SQL数据库引擎是作为一个单独的 ...
- mysql双机热备实现
说明 机器A:(172.16.1.251),机器B:(172.16.1.252) 两台机器都创建数据库web:create database hello default charset utf8; 实 ...
- [BZOJ2947]促销(Splay)
Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...
- 3 ways of including JavaScript in HTML
Code written in JavaScript must be executed from a document written in HTML. There are three ways of ...
- 数据预处理之独热编码(One-Hot Encoding)
问题的由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑以下三个特征: ["male","female"] ["from ...