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 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
思路1:直观的想法,遍历数组,一旦当前元素>=target,当前元素的位置即为插入位置,边界问题处理一下即可。时间复杂度:O(n)
class Solution {
public:
int searchInsert(int A[], int n, int target) { if(target > A[n-])
return n; else {
for(int i = ; i < n; i++) {
if(A[i] >= target)
return i;
}
}
}
};
思路2:由于给定数组为已经排好序的,可以使用二分查找,比较A[mid]与target的大小。时间复杂度:O(logn)
class Solution {
public:
int searchInsert(int A[], int n, int target) { int start = ;
int end = n - ;
int mid = ; while(end >= start) { mid = (start + end) / ;
if(target == A[mid])
return mid;
else if(target > A[mid])
start = mid + ;
else
end = mid - ;
} if(target > A[mid])
return mid + ;
else
return mid; }
};
LeetCode Problem 35: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】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 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 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 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导致的溢 ...
随机推荐
- hdu1863 畅通project(判定最小生成树)
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 破解 zip 压缩包程序
目录 项目文件结构 代码实现过程 演示效果 代码地址如下:http://www.demodashi.com/demo/12021.html 项目文件结构 在当前目录有三个文件: 3-zipCrack. ...
- 牛散NO.2:MACD西施说风情,柳下惠高位勿迷情
创业板日线“高位夺命勾魂枪” 话说在创业板的波段调整中,MACD的勾魂枪同样让多头“魂断蓝桥”.圈内图形又好比西施姑娘回眸一笑,吴王夫差便注定命赴黄泉了.范蠡的精心设计让西施 布了一个风情万种的局,被 ...
- chrome 非安全模式解决开发跨域问题
这个参数可以降低chrome浏览器的安全性,禁用同源策略,利于开发人员本地调试. ps:如果是mac用户(记得 Command + Q 关闭chrome): open -a Google\ Chro ...
- Android WebView学习
Android WebView学习 文章来源:小小懒羊羊个人知识管理库 权限: <uses-permission android:name="android.permission.IN ...
- iOS的isnan函数
假设一个数是一个确定的数.那它就不是nan值 假设一个数是无穷大,无穷小.那它就是nan值 我试着打印了下面的值是不是nan值 if (isnan(1)) { DLog(@"1是NAN&qu ...
- 树莓派学习笔记——apt方式安装opencv
0.前言 本文介绍怎样在树莓派中通过apt方式安装opencv.并通过一个简单的样例说明怎样使用opencv. 相比于源码方式安装opencv,通过apt方式安装过程步骤简单些,消耗的时间也少 ...
- 李洪强经典面试题41-iOS选择题
1.及时聊天app不会采用的网络传输方式是 DA UDP B TCP C Http D FTP 2.下列技术不属于多线程的是 AA Block B NSThread C NSOperation D G ...
- spring boot 打包报错
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repac ...
- MFC 点击按钮,弹出另一个对话框(模态及非模态对话框)
1. 模态对话框 资源视图->Dialog->右键->添加资源->新建->对话框->右键->添加类. 例如:在A_dialog中点击按钮弹出B_dialog ...