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相关推荐

  1. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 35. Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  5. [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 ...

  6. [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 ...

  7. Leetcode#35. Search Insert Position

    题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. leetcode python3 简单题35. Search Insert Position

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十五题 (1)题目 英文: Given a sorted array and a ...

  9. leetcode 【 Search Insert Position 】python 实现

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

最新文章

  1. Linux系统如何安装AutoFs挂载服务
  2. Dev 控件中的combox禁止输入
  3. 新版Edge浏览器弹出“ 禁用开发人员模式扩展 ”的解决方案
  4. 【通知】有三AI固态硬盘最后一次补货,再增3套视频!
  5. Java JNI简单实现
  6. Android Binder总结
  7. MATLAB中改变默认当前文件夹
  8. python的数据库_python数据库操作-mysql数据库
  9. 360、金山回应网页劫持 没守住商业底线
  10. C++:fseek( FILE *stream, long offset, int origin )
  11. Python是Python的web框架
  12. 无线通信基础(二):高斯噪声中的检测
  13. 计算机毕业设计、课程设计、实战项目之[含论文+答辩PPT+任务书+中期检查表+源码等]基于ssm的NBA球队管理系统
  14. 学以致用二十四-----shell脚本中的列表及space
  15. 华为交换机释放vlanif接口DHCP地址池下ip命令
  16. 电脑蓝牙模式接收手机文件
  17. Python-批处理修改音频采样率(批量重采样)
  18. CSS 实现平行四边形
  19. 怎么就那么多SlectObject和DeleteObject···········
  20. JavaScript实现网页关灯效果

热门文章

  1. linux设备模型的主要功能,Linux设备模型(3)
  2. 电脑生成siri语音_Siri for Mac:苹果电脑也有语音助手
  3. 7系统软raid_使用图形界面来配置RAID
  4. oracle每季度补丁,Oracle 2020 年第四季度补丁发布
  5. rapidjson 嵌套json_[整理][RapidJson用法2]C++之RapidJson创建嵌套数组json | 勤奋的小青蛙...
  6. flash as3与后台php交互用户注册例子,as3与PHP后台交互2
  7. SSM 整合 2:Java EE 开发环境的搭建(JDK 配置、Tomcat 安装、IDE 安装、IDE 集成 Tomcat、Spring 及其组件的下载)
  8. java多线程编程_《java多线程编程实战指南》读书笔记 -- 基本概念
  9. angularjs sill 创建项目_开源项目——博客项目MyBlogs.Core,基于.NET 5
  10. Nvidia CUDA初级教程4 GPU体系架构概述