题目:

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.

[1,3,5,6],5 → 2

[1,3,5,6],2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6],0 → 0

  

题解:

Solution 1 ()

class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
if (A.size() == ) {
return ;
}
int start = ;
int end = A.size() - ; while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
return mid;
} else if (A[mid] > target) {
end = mid;
} else {
start = mid;
}
}
if (A[start] >= target) {
return start;
} else if (A[end] >= target) {
return end;
} else {
return end + ;
}
}
};

Solution 2 ()

class Solution {
/**
* param A : an integer sorted array
* param target : an integer to be inserted
* return : an integer
*/
public:
int searchInsert(vector<int> &A, int target) {
if (A.size() == ) {
return ;
}
int start = ;
int end = A.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] == target) {
return mid;
} else if (A[mid] > target) {
end = mid;
} else {
start = mid;
}
}
if (A[end] == target) {
return end;
}
if (A[end] < target) {
return end + ;
}
if (A[start] >= target) {
return start;
} return start + ;
}
};

【LintCode】060.Search Insert Position的更多相关文章

  1. 【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 ...

  2. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  3. 【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 ...

  4. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  5. 【Lintcode】011.Search Range in Binary Search Tree

    题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...

  6. 【Lintcode】062.Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  7. 【Lintcode】038.Search a 2D Matrix II

    题目: Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence ...

  8. 【Lintcode】028.Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  9. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

随机推荐

  1. js 显示当前系统时间

    <div id="test"></div>                <script >                    setInt ...

  2. 重新编译Nginx指导手册【修复静态编译Openssl的Nginx漏洞 】(转)

    1. 概述    当前爆出了Openssl漏洞,会泄露隐私信息,涉及的机器较多,环境迥异,导致修复方案都有所不同.不少服务器使用的Nginx,是静态编译opensssl,直接将openssl编译到ng ...

  3. 18- php Redis扩展编译

    一:php扩展编译Redis :wget http://pecl.php.net/get/redis-2.2.5.tgz :tar -zxvf redis-.tgz :cd redis- :/usr/ ...

  4. 中面试中你不可回避的C、C++的问题(一)

    基础中的基础 局部变量与全局变量问题 (使用’ ::’) 2.      如何在另个文件中引用一个全局变量 (extern) 3.      全局变量可以定义被多个C文件包含,并且是static 4. ...

  5. Android setTag()与getTag(),与set多个setTag()

    首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essential ...

  6. 实现RTSP摄像机进行网页直播和微信直播的技术方案:EasyNVR自动更新方法

    问题背景: 1.EasyNVR的用户越来越多,技术人员一一对应解答效率不高: 2.随着EasyNVR应用场景的不断增加,以及EasyNVR自身在技术上的不断优化,版本更新比较快: 3.由于开发人力有限 ...

  7. 【题解】CF891CEnvy

    [题解] CF891C Envy 很好玩的一道题.尽管不难,但是调了很久QAQ 考虑克鲁斯卡尔最小生成树的算法,可以发现这些最小树生成的性质: 当生成树所有边的权值都\(\le\)某个$ w$的时刻, ...

  8. 【题解】cycle

    [题解]cycle 题目描述 给定一个无向图,求一个环,使得环内边权\(\div\)环内点数最大. 数据范围 \(n \le 5000\) \(m\le 10000\) \(Solution\) 考虑 ...

  9. 【题解】P1156垃圾陷阱

    [题解]P1156 垃圾陷阱 乍看此题,我们感觉状态很多,很复杂. 遇到这类型条件比较多的\(dp\),我们不要首先考虑全部设出来,而是要看到这些状态的本质.而在这道题目中,时间和高度就是关键. 考虑 ...

  10. ABAP 性能优化001

    红方框里那一步之行很慢,lt_iflos这个内表才200多条数据 1.关键是你from那个表有多少数据.... 注意点: 1.不要用 CORRESPONDING FIELDS OF 2.LT_IFLO ...