2019独角兽企业重金招聘Python工程师标准>>>

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.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 1:

Input: [1,3,5,6], 0
Output: 0

Solution:

class Solution {public int searchInsert(int[] nums, int target) {if (nums==null){return 0;}if(target<nums[0]){return 0;}if(target>nums[nums.length-1]){return nums.length;}int left = 0;int right = nums.length;while(left<=right){int mid = (left + right) /2;if(target==nums[mid]){return mid;}else if(target>nums[mid] && target<nums[mid+1]){return mid+1;}else if(target > nums[mid]){left = mid+1;}else{right = mid-1;}}return -1;}
}

转载于:https://my.oschina.net/u/1447519/blog/1572373

leetcode -- Search Insert Position相关推荐

  1. [LeetCode] Search Insert Position 搜索插入位置

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

  2. [LeetCode]Search Insert Position

    原题链接:http://oj.leetcode.com/problems/search-insert-position/ 题意描述: Given a sorted array and a target ...

  3. Leetcode: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 --Search Insert Position

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

  5. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. 1 class Solution { 2 publi ...

  6. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

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

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

  8. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  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. maven项目首页显示的问题
  2. Arp协议和Arp欺骗
  3. acer clear 工具_50个能帮你节省时间的开发工具!(值得收藏)-头条
  4. raise errorclass(errno, errval) sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1366, u
  5. python删除字符串中重复字符_从Python中删除字符串标点符号的最佳方法
  6. CompletableFuture详解~runAfterBoth
  7. transmac使用方法_Mac苹果电脑降级方法?
  8. matlab平稳随机过程的功率谱密度,平稳随机过程的功率谱密度
  9. 右击计算机管理出现乱码,win10系统右键菜单出现乱码的处理方案
  10. DOS-强制结束进程命令(ntsd命令/taskkill命令)使用教程
  11. Algorithm:数学建模大赛(CUMCM/NPMCM)之建模历年题目类型、数据分析及官方评阅要点之详细攻略
  12. python股票交易系统是如何实现自动交易的?
  13. Dragonfly 三维可视化数据分析处理软件-切片分析工具使用教程
  14. 智慧工地管理平台系统赋能建筑企业数字化管理
  15. 申宝理财-指数全天探底回升
  16. 知识图谱介绍-我的学习笔记
  17. 单隐层神经网络可以拟合任意单值连续函数
  18. 如何配合RestTemplate优雅的记录请求以及返回的信息
  19. Linux下的文本网页浏览器
  20. buildroot 修改和保存xxx_defconfig make savedconfig错误解决

热门文章

  1. 为什么default package包里面的方法不能被其他包调用
  2. RPC 框架之 Goole protobuf
  3. 使用LIstView和自定义Adapter完成列表信息显示
  4. 常见HTTP状态(304,200等)转
  5. sql2005安装过程,(不装C盘)
  6. Ecipse:calculating requirements and dependencies(长时间阻塞问题)
  7. oracle一个表拆成多个表,oracle拆分函数,将字符串拆分成多行多字段表数据
  8. python安装sqlalchemy python2_Python SQLAlchemy --2
  9. 统计信号处理基础_0基础学Python,就业中你需要建立360度无死角技能树
  10. python3异步task_并发,异步编程_Python中的asyncio模块中的Future和Task的区别?,并发,异步编程,python,asyncio - phpStudy...