leetcode115:search -insert-position
题目描述
假设数组中没有重复项。
下面给出几个样例:
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
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
输出
2
class Solution {
public:
/**
*
* @param A int整型一维数组
* @param n int A数组长度
* @param target int整型
* @return int整型
*/
int searchInsert(int* A, int n, int target) {
// write code here
if (n==0) return 0;
int l=0,h=n,m;
while (l<h){
m=l+((h-l)>>1);
if (A[m] >target){
h=m;
}
else if (A[m]<target){
l=m+1;
}
else {
return m;
}
}
if (target<A[0])return 0;
if (target>A[n-1]) return n;
return h;
}
};
#
#
# @param A int整型一维数组
# @param target int整型
# @return int整型
#
class Solution:
def searchInsert(self , A , target ):
# write code here
if not A:
return 0
if target in A:
return A.index(target)
else :
for i in range(len(A)):
if A[i]>target:
return i
return len(A)
leetcode115: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][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- leetcode-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
随机推荐
- JS-YAML -YAML 1.2 JavaScript解析器/编写器
下载 JS-YAML -YAML 1.2 JavaScript解析器/编写器JS-YAML -YAML 1.2 JavaScript解析器/编写器 在线演示 这是YAML的实现,YAML是一种对人友好 ...
- install Wine + WeChat in Fedora 31
install Wine + WeChat in Fedora 31 dnf -y install dnf-plugins-core dnf config-manager --add-repo htt ...
- 多测师_肖sir_性能测试之性能测试了解001(jmeter)
一.了解jmeter 1.Jmeter的概念? JMeter是Apache组织开发的基于Java的压力测试工具.具有开源免费.框架灵活.多平台支持等优势.除了压力测试外,JMeter在接口测试方面也有 ...
- 多层级makefile
多层级makefile 当项目变大之后,需要多层级的makefile来编译,每个makefile的具体功能实现参考单源文件目录makefile.然后再在顶层目录写一个总的makefile来实现编译逻辑 ...
- python 产生随机函数random
random是内建(built-in)函数,作用是产生随机数 导入模块: 接着就可以调用random模块下的函数了使用 dir(random)可以查看random模块下有哪些函数,结果如下: 最常用的 ...
- 灵魂拷问:你真的理解System.out.println()执行原理吗?
原创/朱季谦 灵魂拷问,这位独秀同学,你会这道题吗? 请说说,"System.out.println()"原理...... 这应该是刚开始学习Java时用到最多一段代码,迄今为止 ...
- centos8上使用gitosis管理git项目
零,centos8平台如何安装gitosis服务? 参见:centos8平台安装gitosis服务 地址:https://www.cnblogs.com/architectforest/p/12456 ...
- printStackTrace()造成的并发瓶颈
一 背景 在一次活动前的压测中,发现一个服务(平响为250ms左右)存在并发瓶颈,单实例的QPS压力从20升高到40后服务就雪崩了(平响急剧升高). 通过<jstack -F>命令查看线程 ...
- mysql复制一个表到其他数据库
db1为原数据库,db2为要导出到的数据库,fromtable 是要导出的表名1.方法一:登录导出到的数据库,执行create table fromtable select * from db1.fr ...
- servlet 验证生命周期过程调用方法的次数
1.书写一个servlet并编译,如: package testservlet; import java.io.IOException;import java.io.PrintWriter; impo ...