Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Search Insert Position
Total Accepted: 14279 Total
Submissions: 41575
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
题意:输出一个元素在一个已排序的数组中的位置,假设不存在输出它应该插入的位置
思路:二分查找,假设找到就输出位置。找不到就输出它应该插入的位置
复杂度:时间O(log n),空间O(1)
相关题目:
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int s = 0, e = n , m ; //中间数,偶数个的时候取前一个
while(s < e){
m = s + (e - s) / 2;
if(A[m] == target) return m;
if(A[m] < target) s = m + 1;
if(A[m] > target) e = m;
}
if(A[m] > target) return m;
else return m + 1;
}
};
Leetcode 二分查找 Search Insert Position的更多相关文章
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 in Rotated Sorted ArrayII
Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- Leetcode No.35 Search Insert Position(c++实现)
1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- LeetCode OJ 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 Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode OJ:Search Insert Position(查找插入位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- nginx配置解决vue单页面打包文件大,首次加载慢的问题
cnpm run build 文件过大,其中主要是vender.js有1.5M,代码部署到服务器,首次访问加载页面时比较慢,耗时6.5s左右,所以需要优化下. 1.Nginx开启gzip 找到ngin ...
- Java基础(十一):接口
一.接口: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是 ...
- IOS开发-提升app性能的25条建议和技巧
前言 这篇文章介绍了作者开发工作中总结的25个iOS开发tips, 多年之前读过这篇文章.收益良多,基本每一个tips在我的应用开发过程中都使用过.今天把这篇文章又一次整理转发下,与大家一起学习,不论 ...
- Immediately-Invoked Puzzler
The Poplar Puzzle-makers weren’t too impressed. They barely noticed your simple and beautiful array ...
- 火狐浏览器FireFox 如何将整个网页保存为图片
使用Friefox的Pearl Cresent Page Saver插件 如图所示网页有很长的滚动条 点击右下角的该插件选项,将整个页面保存为图片 在桌面上得到了这样一个文件,大小是1263×6083 ...
- objective-c 字符串基本操作
.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型 NSString *a = @"1.2.30"; ,)] intValue]; NSLog(@" ...
- PHP高级教程-文件上传
PHP 文件上传 通过 PHP,可以把文件上传到服务器. 本章节实例在 test 项目下完成,目录结构为: test |-----upload # 文件上传的目录 |-----form.html # ...
- php之快速入门学习-15(php函数)
PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. PHP 内建函数 如需查看所有数组函数的完整参考手册和实例,请访问我们的 PHP 参考手册. P ...
- Struts2(二)action的三种方式
一.普通java类 package com.pb.web.action; /* * 创建普通的java类 */ public class HelloAction1 { public String ex ...
- Hibernate(四)结构-基础语义和事务
一.基础语义 核心: Configuration SessionFactory Session 二.Configuration Configuration类负责管理Hibernate的配置信息,Hib ...