/*
* @lc app=leetcode.cn id=35 lang=c
*
* [35] 搜索插入位置
*
* https://leetcode-cn.com/problems/search-insert-position/description/
*
* algorithms
* Easy (42.89%)
* Total Accepted: 31.6K
* Total Submissions: 73.6K
* Testcase Example: '[1,3,5,6]\n5'
*
* 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
*
* 你可以假设数组中无重复元素。
*
* 示例 1:
*
* 输入: [1,3,5,6], 5
* 输出: 2
*
*
* 示例 2:
*
* 输入: [1,3,5,6], 2
* 输出: 1
*
*
* 示例 3:
*
* 输入: [1,3,5,6], 7
* 输出: 4
*
*
* 示例 4:
*
* 输入: [1,3,5,6], 0
* 输出: 0
*
*
*/
int searchInsert(int* nums, int numsSize, int target) {
int i;
for(i=;i<numsSize;i++){
if(nums[i]>=target){
return i;
}
}
return numsSize;
}

这里就在数组中循环,找到比target大或者等于的位置就好。如果找不到的话,那target肯定就是最大的,返回numsize就行。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=35 lang=python3
#
# [35] 搜索插入位置
#
# https://leetcode-cn.com/problems/search-insert-position/description/
#
# algorithms
# Easy (42.89%)
# Total Accepted: 31.6K
# Total Submissions: 73.6K
# Testcase Example: '[1,3,5,6]\n5'
#
# 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
#
# 你可以假设数组中无重复元素。
#
# 示例 1:
#
# 输入: [1,3,5,6], 5
# 输出: 2
#
#
# 示例 2:
#
# 输入: [1,3,5,6], 2
# 输出: 1
#
#
# 示例 3:
#
# 输入: [1,3,5,6], 7
# 输出: 4
#
#
# 示例 4:
#
# 输入: [1,3,5,6], 0
# 输出: 0
#
#
#
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
for i in range(len(nums)):
if(nums[i]>=target):
return i
return len(nums)

Leecode刷题之旅-C语言/python-35.搜索插入位置的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. 狂欢圣诞节,Azure云邀你一起云端跑酷!

    想要速度更快? 希望绕过障碍? 还想安全抵达目的地? …… 平安夜夜游时的各种畅想,不也正是 IT 同事所追求的,更是业务发展的终极目标呀. 好啦,小编说的太多了,这样不好不好.过节休息不谈工作,那就 ...

  2. nagios外部命令接口

    http://nagios.manubulon.com/traduction/docs14en/extcommands.html https://old.nagios.org/developerinf ...

  3. pl/sql 存储过程执行execute immediate 卡住

    在存储过程中,执行了create table.update table.insert into table 但是在使用pl/sql的存储过程调试的时候,一有问题就直接卡住(标识:执行中.....) 后 ...

  4. June 13th 2017 Week 24th Tuesday

    There are no regrets in life, just lessons. 人生中没有后悔,只有教训. Some people can learn from their past mist ...

  5. selenium启动不了浏览器或者启动后不会写入网址,先更新下浏览器驱动

    平时自动化习惯用Chrome浏览器.有几个月没用selenium启动IE和Firefox,今天跑兼容性测试,需要验证其他浏览器.结果遇到两个异常: 1 IE启动不了,直接报错. 2 Firefox启动 ...

  6. 类型系统(type system)是一门编程语言最核心也是最基础的部分---编程语言最终的目标,本质上无非是回答两个问题:如何表示信息、如何处理信息

    https://www.cnblogs.com/feng9exe/p/9712059.html 类型系统(type system)是一门编程语言最核心也是最基础的部分.无论该语言基于何种编程范式,都必 ...

  7. BZOJ1123:[POI2008]BLO(双连通分量)

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...

  8. 【洛谷P3225】[HNOI2012]矿场搭建

    矿场搭建 题目链接 根据题意,发生事故时会有一个挖煤点坍塌, 只有当这个点是割点,会对图的连通性产生影响, 我们首先Tarjan一遍找到所有割点,将原图除去这些割点后, 遍历一遍,找出所有连通块,分三 ...

  9. 【洛谷P3818】小A和uim之大逃离 II

    小A和uim之大逃离 II 题目链接 比较裸的搜索,vis[i][j]再加一层[0/1]表示是否使用过魔液 转移时也将是否使用过魔液记录下来,广搜即可 #include<iostream> ...

  10. 【luogu P4711 「化学」相对分子质量】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4711 要细心模拟 #include <cstdio> #include <algorith ...