【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). ...
随机推荐
- Html--判断客户端类型
公司安排做一个html的app下载页面,需要检测客户端,走不同的css布局,于是从网上搜点资料,简单汇总下,方便日后查阅. 1) 响应式布局设置--@media only screen and onl ...
- 【转】Monkey测试6-Monkey Test Log
Moneky Test Log 分析: 首先用一个最简单的例子分析:monkey --pct-trackball 0 --throttle 100 -v 500/*p参数: 表示指定测试的程序/*v参 ...
- 第一百六十四节,jQuery,常规选择器
jQuery,常规选择器 学习要点: 1.简单选择器 2.进阶选择器 3.高级选择器 jQuery 最核心的组成部分就是:选择器引擎.它继承了 CSS 的语法,可以对 DOM 元 素的标签名.属性名. ...
- Eclipse 透视图(Perspective)
什么是透视图? 透视图是一个包含一系列视图和内容编辑器的可视容器.默认的透视图叫 java. Eclipse 窗口可以打开多个透视图,但在同一时间只能有一个透视图处于激活状态. 用户可以在两个透视图之 ...
- 创建一个Material Design应用过程
创建一个使用Material主题的应用 1.这里需要先搭建一个应用的运行环境 创建一个AVD: 然后运行这个AVD. 2.创建应用 其中的Min SDK和Target SDK 都选择了L Preive ...
- python提供了一个进行hash加密的模块:hashlib
python提供了一个进行hash加密的模块:hashlib下面主要记录下其中的md5加密方式 import hashlib data1 = 'sada' #####字母和数字 m = hashlib ...
- EditText相关属性设置
1.默认不弹出软件盘 在AndroidManifest.xml设置: <activity android:name="com.demo.Activity" ...
- Nginx nginx.conf配置文件详细说明
在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_process ...
- 内核源码阅读vim+cscope+ctags+taglist
杜斌博客:http://blog.db89.org/kernel-source-read-vim-cscope-ctags-taglist/ 武特博客:http://edsionte.com/tech ...
- 玩点不同之CSS的BEM规范
1.BEM引入背景 因为项目的业务逻辑发生重大变化,所以原来大半年的开发周期里做的事情基本上变成无用功.但是公司的项目上线时间依旧没有改变.剩下的时间只有区区的两个月,要做的功能是有社区+电商+核心业 ...