35.查找插入位置
来源:互联网 发布:mac第三方应用商店 编辑:程序博客网 时间:2024/06/06 06:54
Search Insert Position
问题描述:
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
测试代码:
int searchInsert(vector<int>& nums, int target) { int mid = 0,left = 0,right = nums.size(); while(left<right) { mid = (left+right)/2; if(nums[mid]<target) { left = mid +1; }else if(nums[mid]>target) { right = mid; }else{ return mid; } } mid = (left+right)/2; return mid; }
性能:
阅读全文
0 0
- 35.查找插入位置
- 35. Search Insert Position 查找插入位置
- 折半查找数据,折半查找插入位置
- LeetCode---35. Search Insert Position(查找插入位置)
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- 查找排序数组的插入位置
- LeetCode Search Insert Position查找插入位置
- Search Insert Position 查找插入的位置
- 二分查找 上下界 插入位置
- 【leetcode】查找插入位置(二分)
- 插入元素到有序数组,二分搜索查找插入位置
- 25,使用折半查找找到插入位置,并插入数字
- Leetcode刷题记—— 35. Search Insert Position(查找插入位置)
- 【LeetCode】- Search Insert Position(查找插入的位置)
- LeetCode | Search Insert Position(查找插入位置)
- LeetCode OJ 之 Search Insert Position (查找插入位置)
- LintCode 二分法查找, 搜索插入位置 和 二维矩阵
- 折半查找确定插入数组中元素的位置
- 动态库与静态库
- 【Leetcode】【python】Construct Binary Tree from Preorder and Inorder Traversal (Inorder and Postorder)
- QQ怎么找回/恢复被删掉的好友
- 2122数据结构实验之链表七:单链表中重复元素的删除
- 二叉树的基本操作
- 35.查找插入位置
- 第二次作业
- JavaScript函数式编程(二)
- leetcode 241. Different Ways to Add Parentheses
- tensorflow安装卸载
- 【OpenStack源码分析之九】看 nova-scheduler 如何选择计算节点
- Python自学之路第一步——缩进问题
- JavaScript函数式编程(三)
- fzu 2275 Game