【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 ...
随机推荐
- [MAC OS] 常用工具
1.Charles mac下的抓包.代理神器 比如我想本地配置 到我的开发机上面.我可以通过Charles 下Tools -> Map Remote 配置跳转. 2.Postman chrom ...
- 语义后承(semantic consequence),句法后承(syntactic consequence),实质蕴含(material implication / material conditional)
作者:罗心澄链接:https://www.zhihu.com/question/21191299/answer/17469774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 【BZOJ】【1038】【ZJOI2008】瞭望塔
计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...
- 【BZOJ】【3668】【NOI2014】起床困难综合症
贪心 位运算的题……基本都是按位来做的?... 从高位到低位,贪心来搞就可以了…… 这都算不上是数位DP吧= = /****************************************** ...
- LinkedHashMap源码剖析
首先还是类似的,我们写一个简单的LinkedHashMap的程序: public class Test { public static void main(String[] args) { Map&l ...
- BFS(广搜)DFS(深搜)算法解析
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
- Informatica 常用组件Source Qualifier之九 创建SQ转换
可以配置 Designer 在您将源拖到映射中时默认创建源限定符转换,您也可以手动创建源限定符转换. 默认创建源限定符转换 可以配置 Designer 在您将源拖到映射中时自动创建一个源限定符转换. ...
- 简单JavaScript语句实现搜索关键字高亮功能
高亮功能主要是指对页面中指定区域的指定文字进行高亮显示,也就是背景着色.一般在搜索结果页面会经常用到这个功能. 下面就为大家提供一种解决方案,用javascript实现. 首先在<head> ...
- GO语言基础条件、跳转、Array和Slice
1. 判断语句if 1. 条件表达式没有括号(这点其他语言转过来的需要注意) 2. 支持一个初始化表达式(可以是并行方式,即:a, b, c := 1, 2, 3) 3. 左大括号必须和条件语句或 e ...
- Note.js的stream用法一例
Note.js,用stream读取文件的内容,注意decoder的用法 const fs = require('fs'); var rr = fs.createReadStream('data ...