Search Insert Position
来源:互联网 发布:java聊天软件源代码 编辑:程序博客网 时间:2024/05/04 19:44
题目:
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(vector<int>& nums, int target) { int length = nums.size();int begin = 0;int end = length - 1;while (begin <= end){int middle = (begin + end) >> 1;if (nums[middle] == target)return middle;else if (nums[middle] < target){begin = middle + 1;}elseend = middle - 1;}if (begin>end){return begin;} }};
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
- Highcharts中字符串处理方式(4)
- poj 3907
- java_data_structure_genericity
- Hibernate AccessType.PROPERTY和AccessType.FIELD的区别
- WebSocket-SockJS
- Search Insert Position
- Android.mk的用法和基础
- opencv中facedetect例子浅析
- Linux创建密钥登录的方法
- Android启程
- 8.1省选模拟总结
- UINavigationBar-使用总结
- Codeforces Round #291 (Div. 2) C. Watto and Mechanism Trie字典树+dfs
- ViewPager的基本使用