【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). ...
随机推荐
- ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。
Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...
- Linux 文件管理(C语言库函数三)
找到当前目录 char *getcwd(char * buf,size_t size) getcwd函数把当前工作目录的绝对路径名复制到buf中,size指示buf的大小 如果buf不够大,不能装下整 ...
- thinkphp 视频教程
http://edu.51cto.com/lesson/id-28238.html thinkphp
- Allegro转换PADS终极篇.....
allegro转pads终极篇 ...
- C++ RTTI的应用
先看下方的代码,我们所处的context在<<< void* pX = (void*)pGiven; >>>处,只知道上面这些类的信息和pX指针,怎么判断pX指向对 ...
- PHPCMS 前台移用地区联动数据
在PHPCMS中,有时候需要建立模型有地区联动,这个联动数据在这前台调用显示呢?今天清源就给大家介绍一下! 地区联动菜单的缓存文件是 caches\caches_linkage\caches_dat ...
- Android开发:使用DialogFragment实现dialog自定义布局
使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期. TestFragment的代码: public class TestFragment ex ...
- TP系统常量信息
[系统常量信息] 获取系统常量信息: 如果加参数true,会分组显示: 显示如下: [跨控制器调用] 一个控制器在执行的时候,可以实例化另外一个控制,并通过对象访问其指定方法. 跨控制器调用可以节省我 ...
- HTML学习笔记——标准网页设计+使用CSS、Javascript
一.标准网页设计 1.标准网页概述: 标准网页设计要遵循,内容与表现相分离. 内容 + 表现 = 页面 --- 即 :XHTML + CSS = PAGE 内容与变现相分离,也就是内容使用HT ...
- LightOJ1089
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26806 题目大意:略 题目思路:前缀和与离散化 可用线段树做,但是 ...