Leecode-Search Insert Position
来源:互联网 发布:mysql count限制 编辑:程序博客网 时间:2024/06/08 00:02
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 index=0; int length=nums.size(); if(nums[0]>=target) { index=0; return index; } if(nums[length-1]<target) { index=length; return index; } if(nums[length-1]==target) { index=length-1; return index; } for(int i=0;i<=length-2;i++) { if(nums[i]==target) { index=i; break; } if(nums[i+1]==target) { index=i+1; break; } if(nums[i]<target&&nums[i+1]>target) { index=i+1; break; } } return index; }};
阅读全文
0 0
- Leecode-Search Insert Position
- leecode 解题总结:35. Search Insert Position
- 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
- Java多线程之Executor框架
- http(2)
- 335. Self Crossing
- sql语句的优化
- jQuery定位图片在页面没有完全加载完成的时候无法居中
- Leecode-Search Insert Position
- Android蓝牙开发资料
- 图片的热点区域
- crontab定时任务调用Kettle任务报错处理
- 日期插件可选范围控制在5周内
- Spring通过ApplicationContext主动获取bean
- 【Java Web开发】认识JSP 9个内置对象和序号方法说明
- 关于蓝牙的一些详细知识
- 1002 二哥种花生