LeetCode 1539.第k个缺失的正整数

2021/03/08阿里在线笔试—题目一复盘 题目原生 第一题:https://leetcode-cn.com/problems/kth-missing-positive-number/第 k 个缺失的正整数 第二题:https://leetcode-cn.com/problems/profitable-schemes/879. 盈利计划 题目一: n张卡片,无重复值,升序排列,想知道从第一张卡片的值开始第k个在卡片中未出现的正整数为多少,并进行返回 ... 阅读详情

题目链接:LeetCode 1539. 第 k 个缺失的正整数

题意:

     给你一个 严格升序排列 的正整数数组 arr 和一个整数 k 。

请你找到这个数组里第 k 个缺失的正整数。

解题思路:

   暴力模拟就可以

class Solution {

    /**
     * @param Integer[] $arr
     * @param Integer $k
     * @return Integer
     */
    function findKthPositive($arr, $k) {
        $len = count($arr);
        $j = 1;
        for($i = 0; $i < $len; ){
            if($arr[$i] != $j){
                $k--;$j++;
                if($k == 0){
                    break;
                }
            } else{
                $i++;$j++;
            }
        }
        return $j-1+$k;
    }
}
开源项目-智能算法中台管理系统 智能算法中台管理系统,系统主要有样本中心,算法中心,模型中心等模块 阅读详情

相关推荐

unity转微信小游戏:UI大小设置

unity转微信小游戏:UI大小设置

会潜水的小火龙的博客 3270

2023-05-16:给你一个 严格升序排列 的正整数数组 arr 和一个整数 k 。 请你找到这个数组里第 k 个缺失的正整数。 输入:arr = [2,3,4,7,11], k = 5。 输出:9

2023-05-16:给你一个 严格升序排列 的正整数数组 arr 和一个整数 k。请你找到这个数组里第 k 个缺失的正整数。输入:arr = [2,3,4,7,11], k = 5。输出:9。

福大大架构师每日一题 2万+

《小白入门:无人机电调 ESC 的 PWM 接口特性、主流协议与使用指南》

电调是无人机的 “动力中枢”,其 PWM 接口是入门的基础,而 SBUS、DroneCAN 等协议则代表了更高阶的控制方式。小白学习时,需先掌握 PWM 的基本特性和连接方法,再根据场景选择合适的协议,同时严格遵守使用注意事项(如电源匹配、校准步骤、安全操作)。随着实践深入,逐步理解 “协议差异” 和 “系统协同”,才能让无人机更稳定、安全地飞行。

bother3000的博客 3768

leetCode 1539. 第 k 个缺失的正整数

leetCode 1539. 第 k 个缺失的正整数

weixin_42324665的博客 293

ABB AC500 系列 PLC 与上位机iFix 的通讯配置

在 软件中,有 3 种方式可以进入 IP config tool 的配置界面 双击 IP_settings,点击 IP config tool,即可进入 IP config tool 界面点击菜单栏的 Tool→IP config如果单独安装了 ABB IP Configration,则可以通过开始→程序→ABB→ABB IPConfigration 启动 IP 配置工具启动 IP config tool 后,即可进入 IP 配置界面。

xdpcxq的专栏 4031

CASS3D实战:高效三维房屋与地形采集绘制技巧

本文详细介绍了CASS3D在三维房屋与地形采集绘制中的高效技巧,包括智能绘房功能、不规则结构处理、特殊构件快速绘制及地形数据采集策略。通过实战案例和进阶操作指南,帮助用户快速掌握CASS3D的核心功能,提升三维采集与绘制效率。

weixin_29216049的博客 867

js解leetcode(50)-简单

1.第K个缺失的正整数 题目: 给你一个严格升序排列的正整数数组arr和一个整数k。 请你找到这个数组里第k个缺失的正整数。 思路:用一个变量递增,一个变量记录缺失的整数数量,对数组遍历,如果两个数不相等说明缺失。 /** * @param {number[]} arr * @param {number} k * @return {number} */ var findKthPositive = function(arr, k) { let v = 0; let ...

weixin_42733155的博客 1037

Flutter版本升级踩坑实录:从3.27到3.35,我们是如何修复Impeller引擎导致的花屏问题的?

本文详细记录了团队在Flutter版本升级过程中遇到的Impeller引擎导致的花屏问题及解决方案。从问题诊断到最终升级至稳定版3.35,文章分享了排查步骤、升级策略和性能对比数据,为开发者提供了实用的参考经验。

weixin_26854555的博客 503

给你一个 严格升序排列 的正整数数组 arr 和一个整数 k 。 请你找到这个数组里第 k 个缺失的正整数。

class Solution { public int findKthPositive(int[] arr, int k) { HashSet<Integer> set = new HashSet<>(); List<Integer> list = new ArrayList<>(); int[] arr1 = new int[2000]; for(int i = 0;i<a

zylscxy的博客 1324

大疆mini 3 pro rtmp推流(带屏遥控器不支持)windows

大疆mini 3 pro无人机rtmp推流(带屏遥控器不支持)至windows电脑

xbsigeibwkouvfcj的博客 3430

Triton server的部署、构建、backend插件机制代码梳理、模型加载、自定义C++backend流程

Triton inference server的部署、构建、backend机制代码详细梳理、模型加载过程、自定义C++backend流程.

C/C++、嵌入式开发、深度学习算法、模型部署与推理优化 473

(力扣)LeetCode1539. 第 k 个缺失的正整数(C语言)

【C语言】(力扣)本文是 leetcode 1539题 : 第 k 个缺失的正整数,使用二分查找实现。

墨染 375

无人机视角下基于YOLOv5/v8/v10的车辆检测系统:从算法原理到UI界面实战

随着无人机技术的普及和智慧城市的发展,无人机视角下的车辆检测在交通监控、战场侦察、灾害救援等领域展现出巨大的应用价值。然而,无人机航拍图像存在目标尺度小、视角多变、背景复杂等挑战。本文深入探讨了如何利用YOLOv5、YOLOv8、YOLOv10三个代表性目标检测算法,构建一套完整的无人机车辆检测系统。文章不仅详细解析了各版本YOLO的核心技术演进,还提供了从数据集准备、模型训练、性能评估到基于PyQt5/PySide6的图形界面开发的完整代码实现。通过本文,读者将能够掌握构建无人机实时车辆检测系统的全流程技

YOLO 172

python-leetcode-1539. 第 k 个缺失的正整数

利用差值计算当前缺了多少个数。用二分法找出刚好不满足条件的位置。返回结果为left + k。需要我给你写一个具体测试样例或图解吗?

Lucy_wzw的博客 804

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【刷题之路】LeetCode 1539. 第 k 个缺失的正整数

而如果arr[arrSize - 1] - (arrSize - 1 + 1) < k的话,说明数组arr内丢失的数字还不够k个,此时就可以直接返回arr[arrSize - 1] + (k - (arr[arrSize - 1] - (arrSize - 1 + 1) ) )。由于数组时严格升序的,所以对于每个arr[i],我们都可以唯一确定到第i个元素为止缺少的数字个数为arr[i] - (i + 1) = arr[i] - i - 1。如果arr[p1]!解释:缺失的正整数包括 [5,6,7,…

kk702392702的博客 660

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

力扣1539.第k个缺失的正整数

【代码】力扣1539.第k个缺失的正整数。

Pisasama的博客 393

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode- 1539-第k个缺失的正整数

给你一个严格升序排列的正整数数组arr和一个整数k。请你找到这个数组里第k个缺失的正整数。 示例: 输入:arr = [2,3,4,7,11], k = 5 输出:9 解释:缺失的正整数包括 [1,5,6,8,9,10,12,13,...] 。第 5 个缺失的正整数为 9 。 输入:arr = [1,2,3,4], k = 2 输出:6 解释:缺失的正整数包括 [5,6,7,...] 。第 2 个缺失的正整数为 6 。 解决方法: 最开始是用了额外空间去实现,但最后执行出...

毛毛毛小新的博客 614

#力扣 LeetCode1539. 第 k 个缺失的正整数 @FDDLC

题目描述: https://leetcode-cn.com/problems/kth-missing-positive-number/ Java代码: class Solution { public int findKthPositive(int[] a, int k) { boolean[] b=new boolean[2001]; for(int e:a)b[e]=true; for(int n=1;n<b.length;n++.

LiuXingchang的博客 304

Leetcode—1539. 第 k 个缺失的正整数【简单】

运行结果 之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

Atticus的博客 427

⭐算法入门⭐《二分枚举》中等03 —— LeetCode 1539. 第 k 个缺失的正整数

《二分枚举》中等03 —— LeetCode 1539. 第 k 个缺失的正整数

英雄哪里出来 4367

力扣1539. 第 k 个缺失的正整数

力扣1539. 第 k 个缺失的正整数 二分+脑筋急转弯

2301_80219163的博客 793

LeetCode|1539.第k个缺失的正整数

LeetCode习题答案汇总 题目: 给你一个 严格升序排列 的正整数数组 arr 和一个整数 k 。 请你找到这个数组里第 k 个缺失的正整数。 示例 1: 输入:arr = [2,3,4,7,11], k = 5 输出:9 解释:缺失的正整数包括 [1,5,6,8,9,10,12,13,…] 。第 5 个缺失的正整数为 9 。 示例 2: 输入:arr = [1,2,3,4], k = 2 输出:6 解释:缺失的正整数包括 [5,6,7,…] 。第 2 个缺失的正整数为 6 。 提示: 1 <=

小黄的博客 525

LeetCode 1539. 第 k 个缺失的正整数

目录结构 1.题目 2.题解 1.题目 给你一个严格升序排列的正整数数组arr和一个整数k。 请你找到这个数组里第k个缺失的正整数。 示例: 输入:arr = [2,3,4,7,11], k = 5 输出:9 解释:缺失的正整数包括 [1,5,6,8,9,10,12,13,...] 。第 5 个缺失的正整数为 9 。 输入:arr = [1,2,3,4], k = 2 输出:6 解释:缺失的正整数包括 [5,6,7,...] 。第 2 个缺失的正整数为 6 。 提示: ...

HarvestWu的博客 391

LeetCode第1539题 - 第 k 个缺失的正整数

LeetCode第1539题 - 第 k 个缺失的正整数

流水账 205

1539. 第 k 个缺失的正整数(LeetCode-力扣-Python)

【代码】1539. 第 k 个缺失的正整数(LeetCode-力扣-Python)

zy20221209的博客 363

[leetcode] 1539. 第 k 个缺失的正整数

代码 法一:枚举法 将正确的数组与实际的数组比较,易得实际数组arr[i]大于真实数组p。 arr[i] > p时,说明存在缺失的整数,此时p++,并记录当前缺失的元素p。等待相逢。 arr[i] === p时,说明此arr[i]不是缺失的整数。 当缺失的元素p的个数达到k时,结束循环。 /** * @param {number[]} arr * @param {number} k * @return {number} */ var findKthPositive = function(a

爱酸的博文 355
上一篇: LeetCode 110.平衡二叉树
下一篇: LeetCode 1540. K 次操作转变字符串
error0318
博客等级 码龄9年 57粉丝 515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值