LeetCode算法题-Positions of Large Groups(Java实现)

逆向实战:TikTok X-Gorgon算法解析与Unidg补环境技巧 本文详细解析了TikTok X-Gorgon算法的逆向工程全流程,包括参数定位、变种RC4算法还原及Unidbg补环境技巧。通过动态调试与静态分析相结合,揭示了X-Gorgon的生成逻辑和加密细节,为移动安全研究提供了实用指南。 阅读详情

这是悦乐书的第323次更新,第346篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第193题(顺位题号是830)。在由小写字母组成的字符串S中,那些相同的连续字符会组成集合。例如,诸如S =“abbxxxxzyy”的字符串具有集合“a”,“bb”,“xxxx”,“z”和“yy”。

如果集合有3个或更多字符,称之为大集合,要求找到每个大集合的起点和终点,以数组形式返回,以从前往后顺序。例如:


输入:“abbxxxxzzy”

输出:[[3,6]]

说明:“xxxx”是具有起始位置3和结束位置6的单个大集合。


输入:“abc”

输出:[]

说明:我们有“a”,“b”和“c”,但没有大集合。


输入:“abcdddeeeeaabbbcd”

输出:[[3,5],[6,9],[12,14]]


注意:1 <= S.length <= 1000

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 解题

题目的意思很明确,要求找到字母连续出现3次及以上且字母相同的子串,取其起始索引,作为数组返回,并且只能是从前往后的顺序。在遍历字符串中的字母时,还需要记数,判断出现次数,满足条件就取开始索引、结束索引存入List中。

第一步,初始化结果数组,S的长度n。

第二步,遍历S中的字符,定义两个变量,count统计出现次数,index记录开始索引。使用一个循环,从当前位置开始往后遍历查找,遇到相同字符,count加1,直到遇上不相等的字符或者遍历完右边剩下的字符。接着判断,如果count大于等于3,说明符合题目条件,新创建一个List,将起始索引add进去,再将List添加进结果数组中去。

第三步,返回结果数组。

public List<List<Integer>> largeGroupPositions(String S) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    int n = S.length();
    for (int i=0; i<n-1; i++) {
        int count = 1;
        int index = i;
        while (i+1 < n && S.charAt(i) == S.charAt(i+1)) {
            count++;
            i++;        
        }
        if (count >= 3) {
            List<Integer> list = new ArrayList<Integer>();
            list.add(index);
            list.add(i);
            result.add(list);
        }
    } 
    return result;
}

03 小结

算法专题目前已日更超过五个月,算法题文章193+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

知识的定义与分类体系详解 - 从零基础到专业理解 阅读详情

相关推荐

给STM32供电,别只接VDD和GND!VBAT、VDDA这些引脚不接会怎样?

本文深入解析STM32电源设计中VBAT与VDDA引脚的关键作用,揭示仅连接VDD和GND可能导致的ADC精度下降、RTC功能异常等问题。通过实测数据对比不同连接方案,提供包括滤波电路设计、PCB布局布线和双电源自动切换在内的实用解决方案,帮助开发者优化系统稳定性与性能。

weixin_30521161的博客 634

Leetcode830.Positions of Large Groups较大分组的位置

在一个由小写字母构成的字符串S中,包含由一些连续的相同字符所构成的分组。 例如,在字符串S = "abbxxxxzyy"中,就含有"a","bb","xxxx","z"和"yy"这样的一些分组。 我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。 最终结果按照字典顺序输出。 示例1:...

Ha12312的博客 265

YOLOv8n行人检测实战:从数据集准备到模型训练

本文详细介绍了使用YOLOv8n进行行人检测的完整流程。首先阐述了YOLOv8的优势:高速推理(300+FPS)、更高精度、易用性和轻量化(仅6MB)。接着通过Docker搭建GPU训练环境,配置32GB共享内存避免数据加载问题,并安装必要的框架和OpenCV组件。数据集采用COCO2017标注格式,包含归一化坐标的YOLO标签文件。通过可视化脚本验证标注准确性后,提出多数据集融合训练策略以增强模型泛化能力。最后使用Ultralytics框架启动训练,展现了从环境搭建到模型训练的完整技术路径。该方案兼顾检测

Hi20240217的博客 1097

830. Positions of Large Groups - C语言

830. Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", ...

Shirleylinyuer的专栏 335

用LTspice仿真CMOS反相器功耗?这份避坑指南能省你3天调试时间

本文详细介绍了如何使用LTspice准确仿真CMOS反相器的功耗,包括动态功耗、短路功耗和静态功耗的测量技巧。通过工程实践指南,帮助电子工程师避开常见陷阱,优化仿真参数设置,提升仿真结果的可靠性,节省调试时间。

w7x8y9z的博客 190

Zemax实战:手把手教你设计一个40倍、NA0.65的显微物镜(附初始结构文件)

本文详细介绍了使用Zemax设计40倍、NA0.65显微物镜的完整流程,从初始结构搭建到优化验证,涵盖光学设计的关键技巧和实战经验。通过分阶段优化策略和公差分析,帮助工程师快速掌握高倍显微物镜的设计方法,提升光学系统性能。

weixin_42525369的博客 345

NUC与RealSense D435机载视觉平台环境搭建实战

视觉感知是无人机自主飞行的核心技术,而深度相机与机载计算平台的协同工作决定了感知系统的可靠性。Intel RealSense D435通过双目红外结构光原理获取高帧率深度图,NUC作为x86架构机载电脑,则提供了与桌面开发环境完全一致的算力与生态支持。要构建一套可用的无人机视觉系统,需依次打通系统层、依赖层与应用层,包括Ubuntu配置、Python虚拟环境、串口权限、MAVLink通信、librealsense SDK编译及YOLOv8推理部署。环境搭建虽不显眼,却直接影响相机出图稳定性、算法运行效率与整

weixin_34406086的博客 391

基于ESP32与PCM5102的WiFi无损音频传输系统设计与实现

本文详细介绍基于ESP32与PCM5102的WiFi无损音频传输系统设计与实现。通过I2S接口直接传输PCM数据,避免SRC重采样,实现真正的HIFI级音质。涵盖硬件连接、软件架构、网络传输优化及实际音质对比,为音频爱好者提供高性价比的无损无线音频解决方案。

rnn9storyteller的博客 950

leetcode 830. 较大分组的位置(Positions of Large Groups)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。 例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", "bb", "xxxx", ...

weixin_30532759的博客 186

会话管理技巧——如何优化OpenClaw的上下文理解能力(2026进阶版)

本文深入探讨了OpenClaw系统的会话管理技术,重点介绍了会话定义、生命周期管理和上下文优化策略。会话管理包含会话ID、用户信息、状态等核心要素,以及创建、维护、终止和恢复四个生命周期阶段。上下文管理通过ContextManager类实现,支持创建、更新、消息添加和变量管理等功能,并采用历史记录机制确保会话连续性。ContextOptimizer类则负责应用优化规则,通过消息和变量压缩等技术提升系统性能。这些技术共同构建了OpenClaw强大的会话管理能力,为AI助手提供了高质量的交互体验基础。

博客 495

【tessy】软件环境配置:Keil C51 Keil uVision UVSC (Default)

本文详细说明了tessy软件如何配置【Keil C51 Keil uVision UVSC (Default)】环境,具体操作步骤和注意事项

qq_37282365的博客 602

【python3】leetcode 830. Positions of Large Groups(easy)

 830. Positions of Large Groups(easy)  In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = "abbxxxxzyy" has the groups "a...

momottyy的专栏 382

ROS机器人未知环境自主探索功能包explore_lite最全源码详细解析(二)

本系列文章主要针对ROS机器人常使用的未知环境自主探索功能包explore_lite展开全源码的详细解析,并进行概括总结。 本系列文章共包含六篇文章,前五篇文章主要介绍explore_lite功能包中 explore.cpp、costmap_tools.h、frontier_search.cpp、costmap_client.cpp等源码实现文件中全部函数的详细介绍,第六篇文章进行概括总结,包含自主探索功能包explore_lite的简介,实现未知环境自主探索功能的原理及流程总结,效果演示,使用功能包exp

慕羽★的博客 1648

830. Positions of Large Groups。

In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”. Call a gro...

繁城落叶 429

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

LeetCode 830. Positions of Large Groups

题目: In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”. Call...

spring_3_shine的博客 721

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

830. Positions of Large Groups - LeetCode

Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeeeaabbbcd end start end-start a 0 0 b 1 0,1 1-0=1 c 2 1,2 2-1=1 d 3 2,3...

weixin_30436101的博客 92

leetcode 830. Positions of Large Groups

遍历一遍,统计相同的次数并且更新。 class Solution { public List<List<Integer>> largeGroupPositions(String S) { List<List<Integer>> res = new ArrayList<>(); char[] s = ...

xiaobailaji的博客 117

LeetCode—830. 较大分组的位置(Positions of Large Groups)——分析及代码(Java)

LeetCode—830. 较大分组的位置[Positions of Large Groups]——分析及代码[Java]一、题目二、分析及代码1. 直接求解(1)思路(2)代码(3)结果三、其他 一、题目 在一个由小写字母构成的字符串 s 中,包含由一些连续的相同字符所构成的分组。 例如,在字符串 s = “abbxxxxzyy” 中,就含有 “a”, “bb”, “xxxx”, “z” 和 “yy” 这样的一些分组。 分组可以用区间 [start, end] 表示,其中 start 和 end 分别表示

江南土豆的博客 160

【LeetCode】830. Positions of Large Groups 较大分组的位置(Easy)(JAVA)每日一题

【LeetCode】830. Positions of Large Groups 较大分组的位置(Easy)(JAVA) 题目地址: https://leetcode.com/problems/positions-of-large-groups/ 题目描述: In a string s of lowercase letters, these letters form consecutive groups of the same character. For example, a string li

qq_16927853的博客 1057

leetcode (Positions of Large Groups)

Title:Positions of Large Groups   830 Difficulty:Easy 原题leetcode地址:https://leetcode.com/problems/positions-of-large-groups/   1.  采用双指针,注意最后一个子串 时间复杂度:O(n),一次一层while循环,需要遍历整个数组。 空间复杂度:O(n),申请额外的...

hsx1612727380的专栏 282

【Leetcode】830. Positions of Large Groups

题目地址: https://leetcode.com/problems/positions-of-large-groups/ 给定一个长nnn的字符串sss,返回其所有长度不少于333的最长相同字母组成的子串的所有左右端点的数对。 代码如下: import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Solution { public List<List<Intege

数学、算法爱好者的博客 445

【leetcode】Leetcode:830. Positions of Large Groups

问题描述:In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = “abbxxxxzyy” has the groups “a”, “bb”, “xxxx”, “z” and “yy”. Call...

Felaim的博客 690

LeetCode-Positions of Large Groups

LeetCode Java Positions of Large Groups

坟头草一米七五 151

【Array-easy】830. Positions of Large Groups 找到数组中连续字符出现次数大于三的字符起始下标和结束下标

1. 题目原址 https://leetcode.com/problems/positions-of-large-groups/ 2. 题目描述 3. 题目大意 给定一个字符串,找到数组中连续字符出现次数大于三的字符起始下标和结束下标 4. 解题思路 定义两个变量,一个变量count,用来记录连续的重复字符出现的次数,定义一个变量 j 表示当前的字符的位置 使用变量 j 来进行while循环...

想当厨子的程序媛 301

830. Positions of Large Groups

In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". Call a...

weixin_30265103的博客 130

[Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30945039的博客 145
上一篇: LeetCode算法题-Goat Latin Easy(Java实现)
下一篇: LeetCode算法题-Flipping an Image(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值