【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
题解:简单的二分搜索,直接放代码:
JAVA版本:
public int searchInsert(int[] A, int target) {
int answer = 0;
int front = 0;
int end = A.length-1;
while(front <= end){
int mid = (front + end)/2;
if(A[mid] == target)
return mid;
if(A[mid] < target)
front = mid + 1;
else
end = mid-1;
}
return front;
}
【leetcode刷提笔记】Search Insert Position的更多相关文章
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 【leetcode刷题笔记】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- leetcode 刷道题 70 earch 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. I ...
- 【leetcode刷提笔记】Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【leetcode刷提笔记】Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- Mybatis结果生成键值对
在实际应用中我们经常会遇到这样的情况,需要给下拉框赋值,这个时候就需要键值对了,具体使用方法如下 1,在maper.xml文件中定义结果类型(resultType)定义为hashmap,如下所示 &l ...
- javascript 最佳实践 ( 24 章 )
代码约定 易于维护, 形成了一套 JavaScript 代码书写的约定: 跟别的语言差不多, 不过 javascript 中的大括号一定要放在 行尾, 例如: function abc() { // ...
- PHP递归实现无限级分类
在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢?我们在本文中使用递归算法并结合mysql数据表实现无限级分类. 在一些复杂的系统中,要求对信 ...
- (转)初步认识拓展UnityEditor编辑器定制
初步认识拓展UnityEditor编辑器定制 热度 9529 2015-9-4 18:50 |个人分类:Unity3d| 编辑器, 拓展 我相信无数初学者看别人游戏都经常看到他们的Inspector中 ...
- libevent在windows下用visual studio编译时出现error C2894错误的原因与解决方法
libevent是一个使用很广泛的网络库,今天想了解下它.于是去git clone了一份源码,用vs2005的命令行:nmake -f makefile.nmake编译之,顺利编译通过,生成三个静态库 ...
- linux中使用vi 打开文件时,能显示行号
方法一: 1.显示当前行行号,在VI的命令模式下输入 :nu 2.显示所有行号,在VI的命令模式下输入 :set nu方法二: 使用vi编辑~/.vimrc文件,在该文件中加入一行" ...
- codevs1058 合唱队形==洛谷P1091 合唱队形
P1091 合唱队形 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的 ...
- 《从零开始学Swift》学习笔记(Day48)——类型检查与转换
原创文章,欢迎转载.转载请注明:关东升的博客 继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Pers ...
- A Secret(KMP)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- String、StringBuffer与StringBuilder的区别。
无论是做Java或是Android,都避免不了遇到这个问题,其实开发过程中一般情况下是不会纠结,这个问题是面试必选经典题,今天有时间,就总结一下. String.StringBuffer.String ...