/*
* @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. How to reference two table when lack reference column.

    Question:How to reference two table when lack reference column. Example: 1.Create two tables the one ...

  2. Hello World, S/4HANA for Customer Management 1.0

    SAP CRM的前世今生 在我之前的微信公众号文章 SAP的这三款CRM解决方案,您能区分清楚么我曾经提到过我作为成都SAP研究院CRM产品开发团队的一员工作过一段时间. 我向在SAP德国总部工作的德 ...

  3. Python 操作Redis 转载篇

    Python操作Redis数据库 连接数据库 StrictRedis from redis import StrictRedis # 使用默认方式连接到数据库 redis = StrictRedis( ...

  4. python接口测试-项目实践(六) 实际结果与预期结果对比之 数据源与数据库对比

    六 与数据库对比 import pymssql def compare_expected_vs_db(): diff_list = [] # 存储不一致的代码 with pymssql.connect ...

  5. Android(java)学习笔记56:Android InputMethodManager输入法简介

    参见博客: http://blog.csdn.net/pi9nc/article/details/9196779

  6. 项目Alpha冲刺(团队7/10)

    项目Alpha冲刺(团队7/10) 团队名称: 云打印 作业要求: 项目Alpha冲刺(团队) 作业目标: 完成项目Alpha版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 ...

  7. POJ Air Raid 【DAG的最小不相交路径覆盖】

    传送门:http://poj.org/problem?id=1422 Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  8. 如何对Project Proffesional设置预警灯

    Project Proffesional没法一目了然地看到,为了实时看到任务延迟情况,我们必须设置预警灯. 1.添加两个新列“文本1”.“文本2”,重命名为“完成预警”.“进度预警”. 2.右键点击“ ...

  9. JVM垃圾回收补充知识点

    1. 分代 虚拟机中的共划分为三个代: 年轻代(Young Gen):eden和survivor-8:1:1 年老代(Old Gen):存储大对象,由survivor晋升 永久代(perm Gen): ...

  10. Kong Api 初体验

    请查看原文: https://www.fangzhipeng.com/nginx/kong/2016/07/11/kong-api-gateway/ Kong是一个可扩展的开源API层(也称为API网 ...