LeetCode——Search Insert Position
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
基于数组的查找:
public class Solution {
public int searchInsert(int[] nums, int target) {
if(target <= nums[0]) {
return 0;
}
if(target > nums[nums.length-1]) {
return nums.length;
}
for(int i=0; i<nums.length; i++) {
if(target <= nums[i]) {
return i;
}
}
return nums.length;
}
}
LeetCode——Search Insert Position的更多相关文章
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [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] Search Insert Position 二分搜索
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [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 Search Insert Position Python
#Given a sorted array and a target value, return the index if the target is found. If #not, return t ...
- LeetCode Search Insert Position (二分查找)
题意 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [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 Search Insert Position (二分查找)
题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...
随机推荐
- 4G模块ME3760_V2 端口映射
/dev/ttyUSB0 ECM // ECM 口 /dev/ttyUSB1 / //ECM口 /dev/ttyUSB2 AT ...
- spingboot集成jpa(二)
一.使用单元测试 单元测试在每个项目环境中必不可少,springboot中如何使用单元测试 在src/test/java中新建测试类DemoApplicationTest.java 项目结构: De ...
- Linux启动与禁止SSH用户及IP的登录
以下就针对SSH方面讨论一下.假设有人特别关注Linux环境的安全性,第一就从login方面来进行讨论 1:Linux启动或禁止SSH root用户的登录 2:Linux限制SSH用户 事实上这些东西 ...
- mongo 内存限制wiredTigerCacheSizeGB = 10
[root@iZ2zed126f44v90yv59ht3Z rabbitmq]# cat /usr/local/mongodb/mongodb.confport = 27017dbpath = /us ...
- ★ java删除代码注释
package com.witwicky.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java ...
- Laravel Debugbar
Installation Require this package with composer: composer require barryvdh/laravel-debugbar After up ...
- 如果通过html 链接打开app
https://my.oschina.net/liucundong/blog/354029 <!doctype html> <html> <head> <me ...
- mssql 设置id自增 设置主键
主键自增长列在进行数据插入的时候,很有用的,如可以获取返回的自增ID值,接下来将介绍SQL Server如何设置主键自增长列,感兴趣的朋友可以了解下,希望本文对你有所帮助 1.新建一数据表,里 ...
- Struts2学习笔记(OGNL表达式)
Struts 2支持以下几种表达式语言: OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言: JSTL(JSP Standard T ...
- 编写一个Filter,除继承HttpServlet类外还需要( )。
A.继承Filter 类 B.实现Filter 接口 C.继承HttpFilter 类 D.实现HttpFilter接口 解答:B