Leetcode#35. 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
题意
很简单的题目,注意程序出现数组为空的情况。
C++语言
class Solution {
public:int searchInsert(vector<int>& nums, int target) {//长度为0或者小于数组的最小值if(nums.size() == 0 ||nums[0] >=target)return 0;//大于数组的最大值if(target >nums[nums.size()-1])return nums.size();for(int i=1; i<nums.size(); i++){if(target > nums[i-1] &&target<=nums[i])return i;}}
};
Python语言
class Solution(object):def searchInsert(self, nums, target):""":type nums: List[int]:type target: int:rtype: int"""n = len(nums)if n == 0 or target <= nums[0]:return 0if target > nums[n-1]:return nfor index in range(1,n):if target > nums[index-1] and target <= nums[index]:return index
Leetcode#35. Search Insert Position相关推荐
- 【二分法】LeetCode 35. Search Insert Position
LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...
- LeetCode - 35. Search Insert Position
35. Search Insert Position Problem's Link ---------------------------------------------------------- ...
- leetcode 35 Search Insert Position(二分法)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [LeetCode] 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35. Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [LeetCode]--35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [swift] LeetCode 35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- leetcode python3 简单题35. Search Insert Position
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十五题 (1)题目 英文: Given a sorted array and a ...
- leetcode 【 Search Insert Position 】python 实现
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
最新文章
- html canvas text 居中,HTML5 Canvas Text文本居中实例
- 从《王者荣耀》谈游戏的帧同步
- html输入框颜色属性,css 修改input输入框属性
- decltype 遇到计算结果为左值的表达式会得到引用类型
- android相册拍照剪切上传封装,安卓,图片裁剪上传真机测试好使,打包后显示:无法保存裁剪的图像...
- 如何制定客户留存策略_企业如何制定数字化转型策略?
- MyCat分布式数据库集群架构工作笔记0002---数据库中间件对比
- python数据挖掘与分析
- java rxtx_Java使用开源Rxtx实现串口通讯(串口开发) | 学步园
- 网络协议学习--SOAP协议(二)
- java输出斐波那契数列前20项,java输出斐波那契数列
- zoomit的使用 - 一个可以直接在演示的时候写字的软件
- 最新Centos7.6 部署ELK日志分析系统
- RenderTexture实现小地图和炫酷的传送门!(干货收藏)
- SaaSBase:什么是石墨文档?
- 前端对用户身份证号码进行验证以及模糊显示
- HEVC/H.265编码HM码率控制
- python图像主色调(主颜色)提取,在hsv空间k均值迭代实现
- .bas是什么文件以及如何打开导入导出.bas文件的方法
- Lua string字符串相关操作