LeetCode & Q35-Search Insert Position-Easy
Array
Binary Search
Description:
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
my Solution:
public class Solution {
public int searchInsert(int[] nums, int target) {
int i = 0;
for(i = 0; i < nums.length; i++) {
if(target <= nums[i])
break;
}
if(i < nums.length)
return i;
else
return i++;
}
}
Best Solution:
public int searchInsert(int[] A, int target) {
int low = 0, high = A.length-1;
while(low<=high){
int mid = (low+high)/2;
if(A[mid] == target) return mid;
else if(A[mid] > target) high = mid-1;
else low = mid+1;
}
return low;
}
差别就在于,我用的是从首到尾循环,没有完全利用好已排序这个条件。最优解用的是二分法,基本是排序里的算法。
LeetCode & Q35-Search Insert Position-Easy的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode--Array--Remove Element && Search Insert Position(Easy)
27. Remove Element (Easy)# 2019.7.7 Given an array nums and a value val, remove all instances of tha ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [LeetCode] 35. Search 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
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 【题解】【数组】【查找】【Leetcode】Search 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 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 the ...
随机推荐
- Linux系统中svn服务器设置开机启动
安装完svn服务器后虽然好用但是因为经常重启Linux服务器,每次重启完就要去手动启动svn服务器,很是麻烦,于是在网上找了一些方法后,自己把svn服务器设置成开机启动 步骤一:安装svn服务器: h ...
- 面试题-NSDate\CFAbsoluteTimeGetCurrent\CACurrentMediaTime的区别
在昨天的学习视频中,正好案例中用到了.就随机给大家讲了一个面试题.以及遇到技术问题从哪些角度去回答.嗯.一边讲解,一边写了些小笔记.希望能给最近面试的同学一些帮助.在分享这篇面试文章的小编先分享自己建 ...
- mysql在win10下的卸载
转自http://blog.csdn.net/sxingming/article/details/52601250 本文介绍,在Windows10系统下,如何彻底删除卸载MySQL... 1> ...
- 怎么用代码制作WordPress的归档页面
先看看效果,这个是我网站的归档页面:http://www.shenjieblog.com/archives 其实WordPress自带了一个归档的功能,但是只能显示在网页中的某一个部分,但是我想单独制 ...
- web 直播&礼物赠送------腾讯云(四)
直播项目搁置了将近1年,以为都搁浅了,没想到头头又提起来了,这次直播技术更替为了腾讯云,消息系统没变,采用的依然是融云,新增了礼物赠送功能. 项目完成基本就是这样子: 一,播放器 由阿里云转腾讯云,w ...
- Python 中的登陆获取数据跳转页面(不含数据库)
简单表单和模板: import os.path import tornado.httpserver import tornado.ioloop import tornado.options impor ...
- java编程基础知识及常见例题
⒈标识符: 只能包含数字.字母.下划线.$,并且不能以数字开头.语义直观规范 驼峰法则: 如:方法名.变量名采用驼峰法则 帕斯卡命名法: 如: 类.接口.枚举采用帕斯卡命名法包名:网址倒写,com.网 ...
- ELK学习笔记(一)安装Elasticsearch、Kibana、Logstash和X-Pack
最近在学习ELK的时候踩了不少的坑,特此写个笔记记录下学习过程. 日志主要包括系统日志.应用程序日志和安全日志.系统运维和开发人员可以通过日志了解服务器软硬件信息.检查配置过程中的错误及错误发生的原因 ...
- CXF 开发 REST 服务
今天我们将视角集中在 REST 上,它是继 SOAP 以后,另一种广泛使用的 Web 服务.与 SOAP 不同,REST 并没有 WSDL 的概念,也没有叫做"信封"的东西,因为 ...
- 设计模式 --> (1)工厂模式
工厂模式 工厂模式属于创建型模式,大致可以分为三类,简单工厂模式.工厂方法模式.抽象工厂模式. 适用性: 例如部署多种数据库的情况,可能在不同的地方要使用不同的数据库,此时只需要在配置文件中设定数据库 ...