当前位置:网站首页>[usual practice] explore the insertion position
[usual practice] explore the insertion position
2022-06-11 05:46:00 【swindler.】
Given a n The elements are ordered ( Ascending ) integer array nums And a target value target , Write a function search nums Medium target, If the target value has a return subscript , Otherwise return to -1.
Example 1:
Input : nums = [-1,0,3,5,9,12], target = 9
Output : 4
explain : 9 Appear in the nums And the subscript is 4
Example 2:
Input : nums = [-1,0,3,5,9,12], target = 2
Output : -1
explain : 2 non-existent nums So back to -1
Tips :
You can assume nums All elements in are not repeated .
n Will be in [1, 10000] Between .
nums Each element of will be in [-9999, 9999] Between .
class Solution {
func searchInsert(_ nums: [Int], _ target: Int) -> Int {
if nums.count == 1 {
return target > nums[0] ? 1 : 0
}
if target > nums[nums.count - 1] {
return nums.count
}
for i in 0...(nums.count - 1) {
if nums[i] >= target {
return i
}
}
return 0
}
}
Their thinking :① First judge two special situations —— Only one element and target Value is greater than all array elements ;② Traversing array , And with target Comparison of values ;
边栏推荐
- NDK learning notes (IX) POSIX sockect connection oriented communication
- NFC Development -- the method of using NFC mobile phones as access control cards (II)
- Reading the registry using batch
- 1. use alicloud object OSS (basic)
- String sorting times --- bubble sorting deformation
- In the future, how long will robots or AI have human creativity?
- Recursively process data accumulation
- 微信自定义组件---样式--插槽
- 數組部分方法
- What is a thread pool?
猜你喜欢

深度学习分布式训练

All questions and answers of database SQL practice niuke.com

NDK R21 compiles ffmpeg 4.2.2+x264 and converts video files using ffmpeg

Control your phone with genymotion scratch

NDK learning notes (VII) system configuration, users and groups
![Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset](/img/54/f6a3e0ef1f77901506642784e6d3b7.png)
Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset

Getting started with kotlin

es-ik 安装报错

Wechat applet learning record
![[元数据]LinkedIn-DataHub](/img/6a/247ee77dfc219d26651e5d93b04c98.png)
[元数据]LinkedIn-DataHub
随机推荐
Wechat applet, automatic line feed for purchasing commodity attributes, fixed number of divs, automatic line feed for excess parts
Multi threading tutorial (XXIV) cas+volatile
修复【无 Internet, 安全】问题
我们真的需要会议耳机吗?
When the recyclerview plays more videos, the problem of refreshing the current video is solved.
[go deep into kotlin] - flow advanced
Wxparse parsing iframe playing video
In the future, how long will robots or AI have human creativity?
[元数据]LinkedIn-DataHub
Multithreading tutorial (XXVII) CPU cache and pseudo sharing
初步了解多任务学习
Altiumdesigner2020 import 3D body SolidWorks 3D model
Wechat applet learning record
Combing route - Compaction Technology
Deep learning distributed training
es-ik 安装报错
Manually splicing dynamic JSON strings
配置Rust编译环境
(15) Infrared communication
How to deal with message blackout and message sending failure of Aurora im