【Leetcode】Search Insert Position
来源:互联网 发布:喝水提醒电脑软件 编辑:程序博客网 时间:2024/06/05 21:17
题目:
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) { int mid, low=0, high=n-1; while(low<high){ mid=(low+high)/2; if(A[mid]==target)return mid; if(A[mid]>target){ high=mid-1; }else{ low=mid+1; } } return target>A[low]?low+1:low; }};
0 0
- LeetCode: Search Insert Position
- LeetCode Search Insert Position
- [Leetcode] Search Insert Position
- LeetCode : Search Insert Position
- [LeetCode] Search Insert Position
- [Leetcode] Search Insert Position
- [LeetCode]Search Insert Position
- [LeetCode] Search Insert Position
- LeetCode-Search Insert Position
- LeetCode - Search Insert Position
- LeetCode | Search Insert Position
- Leetcode: Search Insert Position
- 【LeetCode】Search Insert Position
- Leetcode Search Insert Position
- Leetcode: Search Insert Position
- LeetCode:Search Insert Position
- leetcode Search Insert Position
- LeetCode Search Insert Position
- hdu4400 BFS+STL
- Java Socket编程(二)
- 链表的一些基本操作
- [xml] <![CDATA[ ]]>是什么意思?
- 黑马程序员-我的第二篇学习日记:C语言中的 if 语句总结
- 【Leetcode】Search Insert Position
- redo记录中改变向量的操作代码
- 沈大海cocos2d-x教程
- IIS配置asp.net的运行环境(Win7旗舰版)
- 快速获取数据库连接字串
- 数组实现动态栈,java
- MySQL-5.6.13免安装版配置方法
- android,标题栏显示加载,对话框显示加载
- 完成程序国际化