package number5;
//贪心算法
import java.util.Stack;
//在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。
//给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
//返回可以通过分割得到的平衡字符串的最大数量。
//示例 1:
//输入:s = "RLRRLLRLRL"
//输出:4
//解释:s 可以分割为 "RL", "RRLL", "RL", "RL", 每个子字符串中都包含相同数量的 'L' 和 'R'。
//示例 2:
//输入:s = "RLLLLRRRLR"
//输出:3
//解释:s 可以分割为 "RL", "LLLRRR", "LR", 每个子字符串中都包含相同数量的 'L' 和 'R'。
//示例 3:
//输入:s = "LLLLRRRR"
//输出:1
//解释:s 只能保持原样 "LLLLRRRR".
//提示:
//1 <= s.length <= 1000
//s[i] = 'L' 或 'R'
//分割得到的每个字符串都必须是平衡字符串。
public class Solution1 {
public static void main(String[] args) {
String s="LLLLRLRLRLRRRRRL";
System.out.println(balancedStringSplit(s));
}
public static int balancedStringSplit(String s) {
char[] a=s.toCharArray();
int cnt = 0;
int balance = 0;
for(int i = 0; i < a.length; i++){
if(a[i] == 'L')
balance --;
if(a[i] == 'R')
balance ++;
if(balance == 0)
cnt ++;
}
return cnt;
}
}
相关推荐
基于Unity引擎利用OpenCV和MediaPipe的面部表情和人体运动捕捉系统
增加了模型的绑定,利用Mediapipe这个强大的库可以实现识别面部表情的捕捉和人体结构运动的捕捉,基于以上的特性加入到Unity的人物模型中,实现实时的面部表情和人体运行的捕捉
go去掉最后一个字符_Go语言编程中字符串切割方法小结
1.func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片import ("fmt""strings")func main() {fmt.Println(strings.Fields("hello widuu golang")) //out [hello widuu golang]}2.func FieldsF...
OpenAI Realtime API实时语音交互实战指南
实时语音交互是人机自然对话的核心能力,其本质在于构建低延迟、双向、流式协同的音频-语义闭环。它突破传统REST API的请求-响应范式,依托WebSocket实现毫秒级音频输入流(audio.delta)、模型生成流(text/audio.delta)与动态控制流(session.update)三重并行。该技术显著降低端到端延迟至300ms以内,支撑语音助手、智能客服、对话式教育等强实时场景。本文聚焦OpenAI Realtime API的工程落地,详解PCM音频流处理、VAD语音活动检测、AudioCon
mysql语句之计算字段和数据处理函数
1.SELECT Concat(vend_name,' (',vend_country, ')') FROM table_name ORDER BY vend_name; Concat()拼接串,它把多个串连接起来形成一个较长的串,其中有多个或一个指定的串,每个串中间用逗号分隔。 2.SELECT Concat(RTrim(vend_name),' (',RTrim(vend_country), ')') FROM table_name ORDER BY vend_name; RTrim()函数去掉值右
COMSOL解的继承
COMSOL解的继承1. 应用场景2. 实现方法2.1. 添加研究步骤2.2. 引用研究 根据参考文献整理而来,原文见: http://blog.sina.com.cn/s/blog_4a0a8b5d010126w5.html https://www.zhihu.com/question/371867999/answer/1309194094 个人学习笔记,如有疏漏,欢迎补充 1. 应用场景 在COMSOL模拟中,一次求解之后往往遇到以下几种情况需要用到解的继承: 1)瞬态求解的继续运算。例如初始设定求解1
Jetson Orin NX系统克隆术:如何快速备份与还原你的开发环境(附完整命令行)
本文详细介绍了Jetson Orin NX系统克隆的完整流程,这是一种超越简单备份的完整镜像方案。通过命令行操作,可实现开发环境(包括引导程序、内核、根文件系统及所有配置)的快速备份与还原,确保批量部署时环境的完全一致性与高效性,尤其适合机器人、边缘AI等项目的团队协作。
go去掉最后一个字符_GoLang 字符串处理大全
GoLang基础数据类型--->字符串处理大全作者:尹正杰版权声明:原创作品,谢绝转载!否则将追究法律责任。欢迎加入: 高级运维工程师之路 598432640任何一门语言都有它的好处,Golang的长处就是在于它的高并发能力,所以现在练习好Golang的用法,有利于你找一份好的工作哟~在学习任何一本语言,它的基本数据类型我们应该都得熟记于心,今天我们处理的主角...
保姆级教程:用CST 2023一步步复现一个1-8GHz Vivaldi天线(含参数优化与结果分析)
本文提供了一份详细的CST 2023教程,指导读者如何从零开始设计并优化1-8GHz Vivaldi天线。内容涵盖工程设置、参数化建模、仿真加速技巧以及性能优化策略,特别适合需要实现宽带天线设计的工程师和研究人员。通过实战案例,帮助用户掌握Vivaldi天线的核心设计要点和CST高级功能应用。
golang 字符串 去除最后一个字符
1.最后一个字符是 已知 情况 package main import ( "fmt" "strings" ) func main() { s := "333," strings.TrimRight(s, ",") fmt.Println(s) s = strings.TrimRight(s, ",") fmt.Println(s) } 运行结果: 333, 333 2.最后一个字符是 未知 情况 package main import "fmt" fun..
ThreeJS教程:地图案例(包围盒、正投影)
推荐:将加入你的3D工具链。
MS2107 CVBS转USB2.0 音视频采集芯片,支持1080P@30HZ输出
MS2107 是一款视频和音频采集芯片,支持CVBS转USB2.0 ;可以将 CVBS、S-Video 和音频信号通过 USB接口传送到 PC、智能手机和平板电脑上预览或采集。
#力扣 LeetCode1221. 分割平衡字符串 #在所有 Java 提交中击败了 100.00% 的用户 @FDDLC
题目描述: 1221. 分割平衡字符串 - 力扣(LeetCode) (leetcode-cn.com) Java代码: class Solution { public int balancedStringSplit(String s) { int countL=0,countR=0,answer=0; for(int i=0;i<s.length();i++){ if(s.charAt(i)=='L')countL++; .
YOLOv8改进 | 主干篇 | 利用MobileNetV2替换Backbone(轻量化网络结构)
本文给大家带来的改进机制是,其是专为移动和嵌入式视觉应用设计的轻量化网络结构。这些模型基于简化的架构,并利用深度可分离卷积构建轻量级深度神经网络,其引入了两个简单的全局超参数,用于在延迟和准确性之间进行有效的权衡。实验表明,MobileNets在资源和准确性的权衡方面表现出色,并在多种应用(如对象检测、细粒度分类、面部属性识别和大规模地理定位)中展现了其有效性,这个模型非常适合轻量化的读者来使用。适用检测目标:轻量化网络结构适合轻量化的读者⭐⭐⭐⭐。
Leetcode1221 python
隔了超级长时间又开始更新。。 还是找地方存代码 最近在刷leetcode,主要觉得自己编程水平严重不足 这个也不会记录所有的刷过的题啦 主要是记录一下我觉得有意思比较重要的 题目描述 1221.Split a String in Balanced Strings Easy Balancedstrings are those who have equal quantity of ...
深度学习工具包 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');```[](https://bitdeli.com/free "Bitdeli Badge")
lwip-2.0.3
lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。
力扣1221题:分隔平衡字符串(贪心)
这篇文章介绍了一个关于平衡字符串分割的C++解决方案。平衡字符串是指其中'L'和'R'字符数量相等的字符串。题目要求将给定的平衡字符串分割成尽可能多的子字符串,且每个子字符串也必须是平衡的。作者提供了一个名为balancedStringSplit的函数,通过遍历字符串并分别统计'L'和'R'的数量,当两者数量相等时,增加分割计数。最终返回最大分割数量。代码简洁有效,适合初学者参考。
Devexpress16.2.6破解工具
Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具
leetcode第1221题
leetcode第1221题 分割平衡字符串 贪心算法 从左到右依次匹配,匹配上的话平衡字符串就+1 class Solution { public: int balancedStringSplit(string s) { int n=s.size(); //存储字符,L加R减,当等于0的时候说明刚好有一组平衡字符串 int num=0; //存储平衡字符串的个数 int res=0; for
LeetCode | 1221. 分割平衡字符串——贪心算法
文章目录1. LeetCode第1221题.分割平衡字符串问题1)逻辑判断2)贪心算法2. 贪心算法相关介绍什么是贪心算法求解思路存在的问题实现框架 1. LeetCode第1221题.分割平衡字符串 问题 在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的。 给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。 注意:分割得到的每个字符串都必须是平衡字符串。 返回可以通过分割得到的平衡字符串的 最大数量 。 示例: 输入:s = “RLRRLLRLRL” 输出:4 解释:s 可以
【leetcode-python-23】1221. 分割平衡字符串
【leetcode-python-23】1221. 分割平衡字符串渣渣原始版(92.73%) leetcode 1221. 分割平衡字符串 渣渣原始版(92.73%) class Solution(object): def balancedStringSplit(self, s): """ :type s: str :rtype: int """ if not s: return 0
leetcode-1221:分割平衡字符串
注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的。给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。返回可以通过分割得到的平衡字符串的 最大数量。如果L和R平衡,就res++
Leetcode 1221、分割平衡字符串
Problem Source : https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/ Solution Source : https://github.com/hujingbo98/algorithm/blob/master/source/leetcode/1704_DetermineifStringHalvesAreAlike.cpp 1221、分割平衡字符串 在一个平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同.
LeetCode_1221_分割平衡字符串
题目链接 https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/ 解题思路 初始化ans = 0 利用一个变量var 记录L的数量,遍历字符串s,如何字符为L,则var++,否则var-- 当var = 0时,表明之前出现的L和R的数量必定相等,此使ans++ 遍历完字符串s后返回ans即为答案 AC代码 class Solution { public int balancedStringSplit(Strin
(力扣)1221. 分割平衡字符串
1221. 分割平衡字符串 题目要求 在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的。给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。 注意:分割得到的每个字符串都必须是平衡字符串。返回可以通过分割得到的平衡字符串的 最大数量 。 解题思路 要成为平衡字串,长度必须是偶数(range(2,a+2,2)) 遍历每个字串,s[0:2]肯定是平衡字串,要不最后的结果就是1 初始化一个变量用来存放平衡字串的数量(number) 当删去字串中的“L”,剩余的长度为原始字串的1/2时,
C++&Python描述 LeetCode 1221. 分割平衡字符串
C++&Python描述 LeetCode 1221. 分割平衡字符串 大家好,我是亓官劼(qí guān jié ),在公众号、优快云、GitHub、B站、华为开发者论坛等平台分享一些技术博文,全平台的id皆为:亓官劼(qí guān jié ),除以上平台,其他的亓官劼id都不是本人,有不少网站在冒充,注意甄别。 放弃不难,但坚持一定很酷!时光荏苒,未来可期,一起加油~ 建了个小交流群,Q群:545611263,要进微信群的话可以加我V:qiguanjie2015备注拉群即可。近期对
LeetCode1221:分割平衡字符串(JAVA解析)
题目描述 >LeetCode1221题< 在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的 给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串 注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串 返回可以通过分割得到的平衡字符串的 最大数量 示例1: 输入:s = “RLRRLLRLRL” 输出:4 解释:s 可以分割为 “RL”、“RRLL”、“RL”、“RL” ,每个子字符串中都包含相同数量的 ‘L’ 和 ‘R’ 示例2:
LeetCode知识点总结 - 1221
LeetCode 1221. Split a String in Balanced Strings 考点 难度 Greedy Easy 题目 Balanced strings are those that have an equal quantity of ‘L’ and ‘R’ characters. Given a balanced string s, split it in the maximum amount of balanced strings. Return the max
python-leetcode-1221. 分割平衡字符串
可以使用贪心算法来解决这个问题。遍历字符串s,使用计数器count记录L和R出现的次数之差:遇到Lcount增加 1;遇到Rcount减少 1;每当count变为 0,说明找到了一个平衡子字符串,计数result加 1。
1万+



被折叠的 条评论
为什么被折叠?



