题目

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相关推荐

  1. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  2. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  3. 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 ...

  4. [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 ...

  5. LeetCode 35. Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  6. [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 ...

  7. [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 ...

  8. leetcode python3 简单题35. Search Insert Position

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第三十五题 (1)题目 英文: Given a sorted array and a ...

  9. leetcode 【 Search Insert Position 】python 实现

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

最新文章

  1. html canvas text 居中,HTML5 Canvas Text文本居中实例
  2. 从《王者荣耀》谈游戏的帧同步
  3. html输入框颜色属性,css 修改input输入框属性
  4. decltype 遇到计算结果为左值的表达式会得到引用类型
  5. android相册拍照剪切上传封装,安卓,图片裁剪上传真机测试好使,打包后显示:无法保存裁剪的图像...
  6. 如何制定客户留存策略_企业如何制定数字化转型策略?
  7. MyCat分布式数据库集群架构工作笔记0002---数据库中间件对比
  8. python数据挖掘与分析
  9. java rxtx_Java使用开源Rxtx实现串口通讯(串口开发) | 学步园
  10. 网络协议学习--SOAP协议(二)
  11. java输出斐波那契数列前20项,java输出斐波那契数列
  12. zoomit的使用 - 一个可以直接在演示的时候写字的软件
  13. 最新Centos7.6 部署ELK日志分析系统
  14. RenderTexture实现小地图和炫酷的传送门!(干货收藏)
  15. SaaSBase:什么是石墨文档?
  16. 前端对用户身份证号码进行验证以及模糊显示
  17. HEVC/H.265编码HM码率控制
  18. python图像主色调(主颜色)提取,在hsv空间k均值迭代实现
  19. .bas是什么文件以及如何打开导入导出.bas文件的方法
  20. Lua string字符串相关操作

热门文章

  1. 视觉效果阿诺德渲染器Mac C4D R26 Arnold for Cinema 4D
  2. 苹果iMessage垃圾信息泛滥:运营商难涉及
  3. 【libcurl 】win32 构建 Release版本 修改cmakelist 链接openssl1.1.*
  4. CAD导出插件逆向分析
  5. Vue 进阶系列丨自定义指令实现按钮权限功能
  6. 手把手教你把你的作品上传到github上
  7. 大数据培训Hadoop数据压缩之压缩方式选择
  8. 宜信珍惜初心,吸引坚持梦想的人才
  9. 能用5GWiFi就别用2.4GWiFi
  10. Linux中yum源不能使用故障解决---网络yum配置