Search Insert Position
来源:互联网 发布:淘宝店装修怎么弄 编辑:程序博客网 时间:2024/06/06 02:37
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
思路:典型排序搜索。
class Solution {public: int searchInsert(int A[], int n, int target) { if (A == NULL) { return NULL; } int start = 0; int end = n - 1; int mid; while (start <= end) { mid = start + (end - start) / 2; if (A[mid] > target) { end = mid - 1; } else if (A[mid] < target) { start = mid + 1; } else if (A[mid] == target) { return mid; } } return target>=A[mid] ? mid+1:mid; }};
0 0
- LeetCode: Search Insert Position
- Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode34:Search Insert Position
- Search Insert Position
- [35]Search Insert Position
- Search Insert Position
- Search Insert Position
- [LeetCode]Search Insert Position
- Search Insert Position
- [LeetCode] Search Insert Position
- Search Insert Position
- Search Insert Position
- 项目clean之后,R.java不见了
- 微新余微信号ivxinyu新余农村户口多值钱?@一张表告诉你(农民的逆袭)!
- Java中多态以及向上转型和向下转型
- java+poi实现word转html显示
- jsp学习之路之实现一个用户信息页面并实现跳转到另外一个页面输出用户的相关信息
- Search Insert Position
- 最小中值平方估计
- Linux设备驱动程序-调试技术
- 用opengl方式打开unity
- sql查出表的各个字段的所有属性
- 程序入口-UIApplicationMain详解
- 制作根文件系统
- 自然底数e大揭秘
- JAVA并发编程学习笔记之MCS队列锁