/*
* @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. 在一个服务中实现 多个契约 和终结点 z

    一个服务作为一系列终结点被定义的.每个终结点都有一个地址,绑定和契约.契约就是暴露终结点能力的.地址就是这些应用或服务从网络的哪个地址可找到,契约是关于如何访问他们的. 在终结点和契约间有一对多的关系 ...

  2. 【Leetcode】【Medium】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  3. python3的学习经验

    网上资料非常多,颇有些“乱花渐欲迷人眼”的意味,个人看了不少,拖之前从事前端的福,发现廖雪峰大神的网站里有.学了2天之后发觉获益良多,网址:https://www.liaoxuefeng.com/wi ...

  4. 多层感知机训练minist数据集

    MLP .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1p ...

  5. 2018.11.20 Struts2中对结果处理方式分析&struts2内置的方式底层源码剖析

    介绍一下struts2内置帮我们封装好的处理结果方式也就是底层源码分析 这是我们的jar包里面找的位置目录 打开往下拉看到result-type节点 name那一列就是我们的type类型取值 上一篇博 ...

  6. Python 学习笔记(七)Python字符串(三)

    常用字符串方法 split()  分割字符串,指定分隔符对字符串进行分割 join()   将序列中的元素以指定的字符连接生成一个新的字符串 str.strip() 用于移除字符串头尾指定的字符(默认 ...

  7. [Linux/Unix]用户和用户组管理

    Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统的用户,都必须拥有自己的账号. 实现用户的管理,主要做: 用户账号的添加.删除.修改: 用户口令的管理: 用户组的管理. (一)用户的 ...

  8. Angularjs基础(一)

    (一) 模型——视图——控制器 端对端的解决方案,AngularJS 试图成为WEB 应用中的一种段对端的解决方案.AngylarJS 的出众 之处如下:数据绑定,基本模板标识符,表单验证,路由,深度 ...

  9. NEC 框架规范 css reset

    /* reset */html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,captio ...

  10. java实现简单计算器功能

    童鞋们,是不是有使用计算器的时候,还要进入运行,输入calc,太麻烦了,有时候甚至还忘记单词怎么拼写,呵呵程序员自己写代码实现,又简单,又方便啊 以下为代码(想要生成可执行工具可参考:http://w ...