Leecode刷题之旅-C语言/python-35.搜索插入位置
/*
* @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.搜索插入位置的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- geth访问公有链
同步以太坊,配置rpc地址 mkdir /opt/blockchain nohup geth --syncmode "fast" --cache=1024 --maxpeers 3 ...
- 操作系统页面置换算法之FIFO,LRU
#include<iostream> #include<unistd.h> #include<vector> #include<wait.h> #inc ...
- 把web项目改造为maven项目
如题,资料网上有一大堆,这里是实践了做个整理. [注意]maven配置 maven要运行在JDK:外部JAVA_HOME指定到JDK,eclipse中runtime指定到JDK 在eclipse中配置 ...
- 初次撸Python,踩平些小坑~
[转义字符] os.path.isdir(targetPath)总是报错,以为字符串不行而是要转成file类型,尝试了很多方法还是没解决,最后发现,windows下的路径中字符串中需要转义(例子在网上 ...
- vue v-on:事件
1.js代码 var box=new Vue({ el:'.box', data:{ msg:'hello' }, methods:{ /*方法放置区,函数*/ show:function(){ // ...
- Linux下实现普通用户免密登录并执行root权限
需求:服务器的root和密码登录都禁用,只开放普通用户登录,这时需要给普通用户配置秘钥文件,实现无密码登录 需要注意的是使用什么用户,就把秘钥文件拷入到该用户的家目录下,如果是root用户,就直接拷贝 ...
- Python 用多线程上传和下载文件
# -*- coding: utf-8 -*- __author__ = 'louis' from ftplib import FTP import multiprocessing import ti ...
- 【洛谷5294】[HNOI2019] 序列(主席树维护单调栈+二分)
点此看题面 大致题意: 给你一个长度为\(n\)的序列\(A\),每次询问修改一个元素(只对当前询问有效),然后让你找到一个不下降序列\(B\),使得这两个序列相应位置之差的平方和最小,并输出这个最小 ...
- 【遥感专题系列】微波遥感(二、合成孔径雷达SAR基础)
目前使用最广的成像雷达系统就是合成孔径雷达(Synthetic Aperture Radar:SAR),SAR几乎成为了雷达的代名词.本文从应用角度介绍SAR系统的基本知识. 本文主要包括: SAR基 ...
- Hibernate Validator数据校验框架常用注释
使用前先配置maven,加入依赖: <dependency> <groupId>org.hibernate</groupId> <artifactId> ...