博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Search Insert Position
阅读量:4559 次
发布时间:2019-06-08

本文共 810 字,大约阅读时间需要 2 分钟。

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

1 public class Solution { 2     public int searchInsert(int[] A, int target) { 3         int end = A.length-1; 4         int start = 0; 5         while(start<=end){ 6             int mid =(start+end)/2; 7             if(A[mid]==target) return mid; 8             else if( mid>0 && A[mid-1]
target) return mid; 9 else if(A[mid]>target) end = mid-1;10 else start = mid+1;11 }12 return start;13 }14 }
View Code

 

转载于:https://www.cnblogs.com/krunning/p/3540027.html

你可能感兴趣的文章
sql server 用户'sa'登录失败(错误18456)
查看>>
SQL语句:子查询
查看>>
14蓝桥杯 矩阵翻硬币
查看>>
开博缘由 , 可点下看看 http://www.cnblogs.com/jshare
查看>>
MySQL连接问题【mysql_connect和mysql_pconnect区别】
查看>>
从MySQL随机选取数据
查看>>
页面置换算法(FIFO,LRU,OPT,LRF)
查看>>
selenium+python笔记1
查看>>
Python字典 (dict)
查看>>
JavaScript:综合案例---房贷计算器的实现
查看>>
MapXtreme开发(一)
查看>>
请问乘客最终买了几等座?
查看>>
获取表行数
查看>>
less与sass的区别点
查看>>
event.keycode值大全
查看>>
array and ram
查看>>
工作笔记——禁用浏览器的返回按钮
查看>>
免费获得盛大网盘EverBox125G容量方法
查看>>
如何用spidermonkey在python里调用javascript代码
查看>>
2016级算法第一次练习赛-A.群鸦的盛宴
查看>>