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 index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2
Example 2:
Input: [1,3,5,6], 2
Output: 1
Example 3:
Input: [1,3,5,6], 7
Output: 4
Example 1:
Input: [1,3,5,6], 0
Output: 0 很简单的一个题目,找有序数列里的目标数的下标,找不到就返回可以插入的下标。既然已经排好序,直接用二分法查找即可,一次提交就A掉。上代码。
public static int searchInsert(int[] nums, int target) {
if (nums.length == 0||target<nums[0])
return 0;
if(target>nums[nums.length-1])return nums.length;
int low, high;
low = 0;
high = nums.length - 1;
while (nums[(low + high) / 2] != target) {
if (low<high) {
if (nums[(low + high) / 2] > target) {
high = (low + high) / 2;
} else if (nums[(low + high) / 2] < target) {
if(low==(low + high) / 2)return (low + high) / 2+1;
low = (low + high) / 2;
} else {
return (low + high) / 2;
}
}
}
return (low + high) / 2;
}
Leetcode 35——Search Insert Position的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [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] 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 ...
- [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(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- Java [leetcode 35]Search Insert Position
题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
随机推荐
- 使用AspNetPager控件分页
页面后台写法 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } pro ...
- 生物结构变异分析软件meerkat 0.189使用笔记(一)
一.准备工作 meerkat 0.189版本和以前的版本相比,支持bwa mem 输出的bam文件,还支持全外显子数据count SV. meerkat原理:参见http://compbio. ...
- hihocoder1391 Country
题解的那种前缀和以前没学过,感觉是种套路 #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; ...
- 使用WebApiClient请求和管理Restful Api
前言 本篇文章的内容是WebApiClient应用说明篇,如果你没有了解过WebApiClient,可以先阅读以下相关文章: WebApi client 的面向切面编程 我来给.Net设计一款Http ...
- CentOS 7离线安装MySQL 5.7
系列文章首发平台为果冻想个人博客.果冻想,是一个原创技术文章分享网站.在这里果冻会分享他的技术心得,技术得失,技术人生.我在果冻想等待你,也希望你能和我分享你的技术得与失,期待. 前言 网上已经有那么 ...
- Hibernate中事务小案例
理论知识: 什么是事务? 指作为单个逻辑工作单位执行的一系列操作,要么完全的执行,要么完全不执行.事务处理可以确保非事务性单元内的所有操作都完全完成,否则永久不会更新面向数据的资源.通过将一组操作组合 ...
- RobotFramework自动化测试框架的基础关键字(二)
1.1.1 如何快速查询某一个关键字的API说明 鼠标选中我们关键字,同时按住Ctrl+Alt键,即可出来该关键字的帮助API以及使用示例 1.1.2 如何快速补全关键字 ...
- 在实训时做的项目出现的ajax json数据传送的问题
json数据在前后端数据交互的时候非常常见,但是大部分人对json都么有系统的学习过,所以就会出现一些很简单的问题却要非很大劲去解决. 在用json传递数据的时候属性必须用双引号括住,一般如果在进行字 ...
- 都是SCI惹的祸?
都是SCI惹的祸? 过去只知道地质学家需要跋山涉水寻找宝藏,最近同一位海外归来的学者谈起,方知少数其它领域的科研人员,也"跋山涉水",在内地研究机构寻找可以写好文章的研究成果,不管 ...
- 元素(WebElement)-----Selenium快速入门(三)
上一篇<元素定位-----Selenium快速入门(二)>说了,如何定位元素,本篇说说找到的元素(WebElement)该怎么用. WebElement常用方法: 返回值 方法名 说 ...