博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 35. Search Insert Position
阅读量:4098 次
发布时间:2019-05-25

本文共 549 字,大约阅读时间需要 1 分钟。

题目:

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
& nums, int target) { for (unsigned int i = 0; i
nums[i] && i == nums.size() - 1){//如果刚好在末尾 return i + 1; } } }};

转载地址:http://wnmii.baihongyu.com/

你可能感兴趣的文章
Python3中无法导入ssl模块的解决办法和python3.7 ModuleNotFoundError: No module named bz2解决办法
查看>>
Linux下mysql数据库安装教程详解
查看>>
windows下mysql数据库迁移到linux方法
查看>>
ImportError: cannot import name ‘HTMLParseError‘ from ‘html.parser‘ (/lib/python3.7/html/parser.py)
查看>>
Linux查看CPU及核数方法
查看>>
java下载图片示例
查看>>
ubuntu14.04.2搭建MPI环境
查看>>
Ubuntu14.04上Hadoop安装问题之解决方案
查看>>
vi编辑下加载行号
查看>>
centOS终端下修改字体
查看>>
centos安装wine
查看>>
Ubuntu 终端打开方式
查看>>
2014-8-21
查看>>
常见传感器的红外(IR)和红光(R)波段
查看>>
2014年07月23日
查看>>
C++中fstream的使用方法
查看>>
strcat函数
查看>>
cout与cerr的区别
查看>>
conio.h
查看>>
getline的使用
查看>>