【LeetCode】35. Search Insert Position
来源:互联网 发布:四维单子上数据看男女 编辑:程序博客网 时间:2024/06/02 08:29
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 low = 0; int high = nums.size()-1; int result; int mid; if(nums.size()==1) { if(target<=nums[0]) return 0; else return 1; } while(low <= high) { mid = (high+low)/2; if(nums[mid]==target) return mid; else if(nums[low]>target) return low; else if(nums[high]<target) return high +1; else if(nums[mid]<target) low = mid+1; else if(nums[mid]>target) high = mid-1; } return low; }};
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
- appbarlayout.addOnOffsetChangedListener失效问题
- cocos2dx读写xml文件
- 重温C语言之--文件操作
- 2016第二学期AspNet-MVC教学案例总目录
- 类加载器、反射、动态代理
- 【LeetCode】35. Search Insert Position
- github:推送至远程仓库
- Leetcode-97 Interleaving String
- Swift - 判断设备方向(或监听设备方向的改变)
- 记录微信分享不成功的可能原因
- 顺序表查找算法的实现与优化
- 创建安卓app的30个经验教训
- body下面要紧接着scroll
- android各种优秀博客,代码资源链接(开发必备几种神级工具) 收藏(不定时更新)