leetcode 35. Search Insert Position
来源:互联网 发布:sn ty gm js是什么 编辑:程序博客网 时间:2024/05/19 02:26
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
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 l = 0; int r = nums.size() - 1; int mid; while(l + 1 < r) { mid = l + (r - l) / 2; if (nums[mid] == target) return mid; else if (nums[mid] < target) l = mid; else r = mid; } //double check if (nums[r] < target) return r + 1; else if (nums[l] < target && target <= nums[r]) return r; else return l; }};
阅读全文
0 0
- [LeetCode]35.Search Insert Position
- LeetCode --- 35. Search Insert Position
- LeetCode 35.Search Insert Position
- [Leetcode] 35. Search Insert Position
- LeetCode 35. Search Insert Position
- [leetcode] 35.Search Insert Position
- Leetcode 35. Search Insert Position
- 35. Search Insert Position LeetCode
- [LeetCode]35. Search Insert Position
- LeetCode 35. Search Insert Position
- 【LeetCode】35. Search Insert Position
- leetcode 35. Search Insert Position
- leetcode 35. Search Insert Position
- LeetCode 35. Search Insert Position
- LeetCode *** 35. Search Insert Position
- leetcode 35. Search Insert Position
- [LeetCode]35. Search Insert Position
- LeetCode 35. Search Insert Position
- JavaScript学习——childNodes属性、nodeType属性
- 搜索二叉树
- spring boot 连接 redis
- java输出保留n位小数
- Django官方教程(八)【创建你的第一个 Django 项目,第六部分】
- leetcode 35. Search Insert Position
- nodemcu响应chunked数据
- MySQL日期时间函数大全
- 比特币挖矿——集群矿池btcpool
- MNIST数据库格式的解析和生成
- C++报计算机中丢失ucrtbased.dll
- 欢迎使用CSDN-markdown编辑器
- button与input标签
- pl/sql 下载安装