LeetCode - 35. Search Insert Position
35. Search Insert Position
Problem's Link
----------------------------------------------------------------------------
Mean:
给定一个有序数组和一个数k,求k在这个数组中插入的下标.
analyse:
二分查找.
Time complexity: O(N)
view code
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
* Author: crazyacking
* Date : 2016-03-02-08.49
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);
class Solution
{
public:
int searchInsert(vector<int>& nums, int target)
{
int len=nums.size();
if(len==0 || target<=nums[0])
return 0;
if(target>nums[len-1])
return len;
int low=0,high=len-1;
while(low<=high)
{
int mid=(low+high)>>1;
if(target<nums[mid])
{
if(mid-1>=0 && target>nums[mid-1])
return mid;
high=mid-1;
}
else if(target>nums[mid])
{
if(mid+1<len && target<nums[mid+1])
return mid+1;
low=mid+1;
}
else
return mid;
}
}
};
int main()
{
Solution solution;
int n,k;
while(cin>>n>>k)
{
vector<int> ve;
for(int i=0;i<n;++i)
{
int tempVal;
cin>>tempVal;
ve.push_back(tempVal);
}
cout<<"pos="<<solution.searchInsert(ve,k)<<endl;
}
return 0;
}
/*
*/
转载于:https://www.cnblogs.com/crazyacking/p/5233587.html
LeetCode - 35. Search Insert Position相关推荐
- 【二分法】LeetCode 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
- 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 ...
- LeetCode 35. Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [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 ...
- [swift] 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 ...
- leetcode python3 简单题35. Search Insert Position
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十五题 (1)题目 英文: Given a sorted array and a ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
最新文章
- 贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)
- LeetCode-链表-203. 移除链表元素
- 大气波导计算MATLAB,基于抛物方程的大气波导环境下电波传播的研究rbedacv8.ppt
- 摩根斯坦利面试题库_经验 | 金融公司摩根士丹利从笔试到实习的全程经验
- 【题解】Luogu P1011 车站
- php serv-u,用php写的serv-u的web申请账号的程序_php
- android真机调试
- 【C语言】【笔记】ASCII码值表;常用转义字符表
- 计算机硬盘内存不足什么原因,电脑硬盘内存不足怎么办
- 重启计算机可以使用什么组合键,死机重启电脑快捷键有哪些
- 手把手带你调参Yolo v5 (v6.2)(推理)
- 登录案例的演示 涉及request的转发
- sparkStreaming常见问题
- java get请求405_get方法没问题,post方法报405错误
- excel离散度图表怎么算_怎样在Excel中计算散点图面积
- 《神经科学:探索脑》学习笔记(第23章 记忆系统)
- Lync Server 2010 呼叫寄存配置和启用
- SSM集成支付宝(沙箱环境)
- 如何让您的网站符合W3C标准
- 2023计算机毕业设计SSM最新选题之java书籍审阅系统dmp8d
热门文章
- java怎么跟qtp脚本传参数_QTP多个Action之间传递参数的方法详解
- 阿帕奇链接mysql_apache guacamole 使用mysql 连接
- android1.6,令人遗憾的Android 1.6系统_戴尔 Mini5(Streak)_手机其它OS-中关村在线
- echart实现3d地图_3D飞线效果——让线“飞”起来的秘密
- .class文件转换.java_Java中的动态链接VS操作系统动态链接
- php连接mysql并操作系统_PHP 连接并操作MySQL的一个实例
- 卸载cuda_NVIDIA驱动和CUDA安装
- Linux编译C没有文件名,crt1.o linux x64上没有这样的文件c编译错误
- a4纸网页打印 table_打印模板不愁人,你还在打印单调的A4纸吗?
- AC_Dream 1224 Robbers(贪心)