【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 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
解法一:线性查找(linear search)
class Solution
{
public:
int searchInsert(int A[], int n, int target)
{
if(n> && target <= A[])
return ;
for(int i = ; i < n; i ++)
{
if(A[i] >= target)
return i;
}
return n;
}
};

解法二:二分查找(binary search)
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int low = ;
int high = n-;
while(low <= high)
{
int mid = low + (high-low) / ;
if(A[mid] == target)
return mid;
else if(A[mid] > target)
high = mid - ;
else
low = mid + ;
}
return low;
}
};

【LeetCode】35. Search Insert Position (2 solutions)的更多相关文章
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 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: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 Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【LintCode】060.Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
随机推荐
- 计算均值mean的MapReduce程序Computing mean with MapReduce
In this post we'll see how to compute the mean of the max temperatures of every month for the city o ...
- hadoop基准測试
写測试hadoop jarhadoop-0.20.2-test.jar TestDFSIO -write -nrFiles 10 -fileSize 1000 ----- TestDFSIO ---- ...
- iOS开发-DatePicker控件
时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time , Count Do ...
- Android 跳转到系统应用管理
Intent i = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");String pkg = " ...
- jquery中filter(fn)的使用研究
jquery中filter(fn)给出的官方说明是: 筛选出与指定函数返回值匹配的元素集合 这个函数内部将对每个对象计算一次 (正如 '$.each'). 如果调用的函数返回false则这个元素被删除 ...
- quickcocos2dx framework环境变 fatal error C1083: 无法打开源文件:“.Box2D/Dynamics/b2World.h”: No such file or d
: fatal error C1083: 无法打开源文件:".Box2D/Dynamics/b2World.h": No such file or directory 解决方法 ...
- 解决 ECSHOP v273 产品详情页面评论不显示的问题
问题描述: 最近同事使用ECSHOP v273帮客户开发了一个商城系统,部署到服务器在测试的时候发现产品详情页面里测试的评论不显示,只显示了数量(其实是产品购买的次数) 因为同事搞了好长时间都 ...
- [React] Simplify and Convert a Traditional React Form to Formik
Forms in React are not easy. T render() { return ( <React.Fragment> <h2>Regular Maintena ...
- Vector 多字段排序的Java实现
要求实现: Vector 多字段排序,其中首元素不参与排序,第一二三字段升序,空排到前面. //这里是Vector的元素定义 public class AVectorElement { private ...
- Discuz常见大问题-如何开启和使用首页四格
在论坛-首页四格中,勾选开启首页四格,然后可以选择数据来源的板块 注意首页四格刷新是有时间的,5分钟左右,不是你这里更新完了帖子那里就有了(如果你自己建的网站,可能回复和热帖都还没有) 当然你也可以使 ...