Leetcode: Search Insert Position
来源:互联网 发布:矩阵分析引论视频 编辑:程序博客网 时间:2024/06/14 21:25
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 lower = 0; int upper = n - 1; int mid; while (lower <= upper) { mid = (lower + upper) >> 1; if (A[mid] == target) { return mid; } else if (A[mid] < target) { lower = mid + 1; } else { upper = mid - 1; } } return (upper+1); }};
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
- DIV+CSS圆角边框
- 说说真实的心里话吧
- java interface 介绍
- [iOS]解决键盘弹出时挡住输入框的问题
- 求大神赐教严重: Error configuring application listener of class org.springframework.web.context.ContextLoad
- Leetcode: Search Insert Position
- 黑马程序员—静态与非静态、构造函数
- 第17周项目19-数组用于进制转换
- 深入Android通过Apache HTTP访问HTTP资源
- ubuntu常用PPA
- 【stagefrightplayer】5 音频输出AudioPlayer介绍
- 生产环境下hadoop集群配置之awk生成hadoop拷贝脚本
- PHP register_shutdown_function函数详解
- MFC几个弹窗的代码