java数据结构与算法刷题-----LeetCode485. 最大连续 1 的个数

LeetCode-485. 最大连续1的个数 题目 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 解题 使用一个变量max记录连续1的最大个数 遍历数次, 同时使用一个临时数组, 记录遍历过程中连续1的最大个数, 当... 阅读详情
java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846

在这里插入图片描述

1. 法一,双指针

  1. 首先用left指针,定位到一个含有1的子区间左边界,然后让right从left位置尝试找到这个子区间的右边界
  2. 如果已经完成了一个子区间的统计,数组还没有遍历完,让left从right的位置,继续。
  3. 类似一个滑动窗格
代码:时间复杂度O(n).空间复杂度O(1)

在这里插入图片描述

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int left=0,right = 0;//双指针
        int n = nums.length;//数组长度
        int ans = 0;//保存结果
        for(left = 0;left<n;left++){//左指针先遍历
            if(nums[left]==1){//如果左指针找到1
                for(right = left;right<n&&nums[right]==1;right++);//右指针从左指针位置找第一个不是1的,或者无元素可遍历
                ans = Math.max(right-left,ans);//保存本次左右指针所指区间长度和已经保存的区间长度,大的那个
                left = right;//左指针从右指针的位置继续
            }
        }
        return ans;//返回结果
    }
}

2. 法二:变量计数

和双指针类似,当我们发现一个1后,用count变量开始统计个数。如果一个区间统计完成后,数组没有遍历完成,那么记录当前count和已经记录的count谁大,然后count归0重新开始统计下一个区间。

代码:时间复杂度O(n).空间复杂度O(1)

在这里插入图片描述

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int ans = 0, count = 0;//ans保存结果,count保存本次区间的1的个数
        int n = nums.length;//数组长度
        for(int i = 0;i<n;i++){//依次遍历
            if(nums[i]==1) count++;//如果发现1,count开始累加
            else{//如果发现当前元素不是1了,可能区间中断,或者当前本身就不是1的区间
                ans = Math.max(ans,count);//无论如何,保存已经保存的区间1个数,和当前1的个数,选择大的
                count = 0;//count重新开始累加
            }
        }
        //如果数组某区间,遍历完成后,没有发现不是1的,上面for循环的ans统计,会少掉末尾区间的
        //在这里补上
        ans = Math.max(ans,count);//选择大的
        return ans;//返回结果
    }
}
AG32VF407直流电流采集仪设计:从1uA到10A宽范围采样与FPGA加速方案 本文详细介绍了基于AG32VF407芯片的直流电流采集仪设计方案,实现了从1uA到10A的宽范围采样。方案采用低噪声电源、精密运放调理、高速比较器自动档位切换及高精度ADC同步采样,并重点阐述了利用AG32VF407片内FPGA实现硬件加速以提升性能的架构思路,为精密电流测量提供了完整的硬件与FPGA加速解决方案。 阅读详情

相关推荐

5G中的RedCap

5G中的redcap

大家好,我是码农老gou,一名专注于编程技术的程序员。在这个博客中,我将与大家分享我在工作和学习中的经验、见解以及一些实用的技术干货。 2532

Java实现 LeetCode 485 最大连续1的个数

485. 最大连续1的个数 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 class Solution { public int findMaxConsecutiv...

南 墙 1万+

关于深度可分离卷积

关于深度学习中的深度可分离卷积

qq_45670407的博客 1801

【leetcode刷题】52.最大连续 1 的个数——Java版

水题

公号:一条coding 1万+

STM32F103C8T6+HX711实战:从零打造智能体重秤(附OLED显示与语音播报)

本文详细介绍了基于STM32F103C8T6微控制器和HX711称重模块,从零构建智能体重秤的完整实战教程。内容涵盖硬件选型、系统架构、驱动编写、数据滤波校准算法,并实现了OLED显示与语音播报功能。该项目是嵌入式学习的综合性实践,尤其适合作为毕业设计课题,帮助开发者掌握传感器应用、数据处理及人机交互等核心技能。

weixin_28285943的博客 523

<硬件设计> 阻抗设计(二) 使用Si9000计算阻抗

阻抗设计(二) 使用Si9000计算阻抗

杰克拉力船长个人博客 1万+

【LeetCode每日一题】——485.最大连续 1 的个数

给定一个二进制数组 nums , 计算其中最大连续 1 的个数。

IronmanJay 1246

超级详细!!!新手使用AutoDL跑深度学习(MixVPR项目)的学习记录

新手第一次在AutoDL上跑深度学习的实验,遇到一些问题,简单记录一下。如果大家发现问题,欢迎和我讨论。

weixin_45152283的博客 5万+

LeetCode(力扣)数组篇 JAVA刷题详解

是不是有许多小伙伴在刷力扣的时候感觉无从下手?从头按顺序开始刷的童鞋们可能会比较有感触,为什么才第四题就感觉很难了?没关系,本文将对力扣的 1-500 题中不需要会员的数据结构与算法题目(数据库与 shell 除外)进行分类,并推荐一个刷题的顺序。完全零基础可以刷题吗?不能,至少要基本掌握一门计算机语言的语法。但现在在网上随便搜一下就能搜到许多关于计算机语言的教程。当然,最好还是上一下正规的课程。刷题顺序很重要吗?重要。按照题目类别结构化地刷题的速度不仅更快,而且可以在刷完一类题之后进行总结。

weixin_61779644的博客 2539

从零手搓大模型(一)文本如何变成 LLM 的输入

本文介绍了从原始文本到LLM输入向量的完整处理流程。主要内容包括: 文本预处理:通过正则表达式将原始文本分割成token列表,处理标点和空格。 词表构建:创建token到ID的映射字典(vocabulary),这是文本数字化的关键步骤。 简单tokenizer实现:封装了编码(encode)和解码(decode)功能,完成文本与token ID的相互转换。 处理流程:原始文本 → 分词 → token ID → 嵌入表示 → 最终输入向量。 数据形状变化:从字符串逐步转换为(batch_size, cont

echo的博客 1008

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

AI人人懂 257

leetcode - 485 最大连续1的个数 -java版

文章目录题目代码 题目 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 代码 /* * @lc app=leetcode.cn id=485 lang=java * * [...

lingle1的博客 575

STM32H743的FDCAN使用方法(1):STM32CubeMX初始化代码生成

STM32H743的FDCAN使用方法(1):STM32CubeMX初始化代码生成。

kevin1499的博客 2572

LeetCode刷题485-简单-最大连续1的个数

算法作为极其重要的一点,是大学生毕业找工作的核心竞争力,所以为了不落后与人,开始刷力扣算法题!

布小禅的博客 1299

深度学习工具包 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")

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode 485. 最大连续1的个数

1. 题目 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-...

Michael是个半路程序员 1181

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

2. leetCode--485最大连续1的个数

1. leetCode–485最大连续1的个数 Java遍历集合的几种方法分析(实现原理、算法性能、适用场合) 题目连接:https://leetcode-cn.com/problems/max-consecutive-ones 题目类型: 简单数组 1.1 问题描述 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3。 注意: 输入的数组只包含 0 和

weixin_45267102的博客 256

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Leetcode 485. 最大连续1的个数

Leetcode 485. 最大连续1的个数 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 常规遍历 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int count = 0;

EIUC_Thread的博客 285

LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)

LeetCode 485:连续最大1的个数 Max Consecutive Ones 给定一个二进制数组, 计算其中最大连续1的个数。 Given a binary array, find the maximum number of consecutive 1s in this array. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以...

爱写Bug 329

从0开始刷力扣

🎉🎉🎉写在前面: 博主主页:🌹🌹🌹戳一戳,欢迎大佬指点! 博主秋秋:QQ:1477649017 欢迎志同道合的朋友一起加油喔💪 目标梦想:进大厂,立志成为一个牛掰的Java程序猿,虽然现在还是一个小菜鸟嘿嘿 -----------------------------谢谢你这么帅气美丽还给我点赞!比个心----------------------------- 从0开始刷力扣一,数组类1.1,数组的遍历 推荐一篇规划刷题顺序的文章,如果有小伙伴和我一样不知道咋刷题,可以去康康哦! 戳戳这里&gt..

qq_61688804的博客 1049

力扣第485题:最大连续1的个数

学习目标: 力扣485. 最大连续1的个数 学习内容: 给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-consecutive-ones 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 学习时间: 2020年10月23日 学习产出:

童一的博客 238

力扣刷题日记之最大的连续1的个数

今天继续给大家分享一道力扣的做题心得今天这道题目是 485.最大的连续1的个数题目如下,题目链接:https://leetcode.cn/problems/max-consecutive-ones 这是一道很简单的题目,题目给了一个数组,其中全部都是1或者0这个两个元素,我使用了直接进行遍历所给数组然后记录最大的连续的个数的方式来直接解题下面是我的解题代码 下面是力扣官方题解 官方解题思路:一次遍历 为了得到数组中最大连续 1 的个数,需要遍历数组,并记录最大的连续 1

ymr1843762281的博客 422

LeetCode:485.最大连续1的个数——简单题简单做

我们可以发现我们目标返回值为3.count在遍历过程中是一个记录清理再记录的过程。而maxCount只保持最大的count数。这个通过Java的Math类库的max方法实现。我们定义一个指针i,从前到后的遍历目标数组,同时设置两个遍历——count和maxCount,开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.count变量在指针i遍历到元素0时清零。, 计算其中最大连续。

2301_78566776的博客 692

485. 最大连续1的个数——Java

给定一个二进制数组, 计算其中最大连续1的个数。 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/max-consecutive-ones 方法一:一次遍历 用一个计数器 tmp 记录 1 的数量,另一个计数器 count 记录当前

lz_Shawn的博客 376

​LeetCode刷题实战485:最大连续 1 的个数

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !...

程序IT圈 333

[LeetCode] 485.最大连续 1 的个数(Java)

给定一个二进制数组nums, 计算其中最大连续1的个数。

是猛男|二次元|玩原神|含盐量极高|不是GAY|不玩币|不接单 1578
上一篇: java数据结构与算法刷题-----LeetCode566. 重塑矩阵
下一篇: java数据结构与算法刷题-----LeetCode240. 搜索二维矩阵 II
ydenergy_殷志鹏
博客等级 码龄7年 5895粉丝 543原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ydenergy_殷志鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址