交替合并字符串 Java

【华为OD技术面试手撕真题】183、交替合并字符串 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS)(0ms) 【华为OD技术面试手撕真题】183、交替合并字符串 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS)(0ms) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2026e卷,od机试,2026E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,20 阅读详情

交替合并字符串

题目描述

请添加图片描述

难度:简单


解决方法

方法:双指针

  • 使用三个索引变量i、j和k来跟踪每个输入字符串的字符位置,然后将它们交替插入到一个新的字符数组word3中,最终将word3转换为字符串并返回。
  • 使用一个循环,该循环将持续,直到i或j达到各自字符串的末尾。在每次循环迭代中,如果i小于len1,则将word1中的字符复制到word3中的下一个位置k,然后递增i和k。同样,如果j小于len2,则将word2中的字符复制到word3中的下一个位置k,然后递增j和k。
  • 处理可能剩余的字符:如果i小于len1,则将word1中剩余的字符复制到word3中。如果j小于len2,则将word2中剩余的字符复制到word3中。
class Solution {
    public String mergeAlternately(String word1, String word2) {
        int i=0,j=0,k=0;
        int len1 = word1.length();
        int len2 = word2.length();
        char[] word3 = new char[len1+len2];
        while(i<len1&&j<len2){
            word3[k++] = word1.charAt(i++);
            word3[k++] = word2.charAt(j++);
        }
        while(i<len1){
            word3[k++] = word1.charAt(i++);
        }
        while(j<len2){
            word3[k++] = word2.charAt(j++);
        }
        return String.valueOf(word3);
    }
}

加油!

基于Python的IMF经济数据爬取与分析:从入门到实战 本文详细介绍了如何使用Python爬取国际货币基金组织(IMF)的经济数据,包括最新的爬虫技术、反反爬策略、数据解析与存储方法。文章从IMF数据源分析开始,逐步讲解如何构建一个完整的爬虫系统,并提供了详细的代码实现和优化建议。通过本文,读者将掌握爬取复杂国际组织数据的关键技术,并能将这些技术应用于其他类似的数据获取场景。关键词:Python爬虫、IMF数据、经济数据获取、数据清洗、反爬策略IMF Data (data.imf.org) - 主要数据门户。 阅读详情

相关推荐

超过4.2万个AOI(地理围栏),深圳市2025年2月15日更新的全类别AOI,矢量图层(全国22个超大特大城市AOI更新系列)

超过4.2万个AOI(地理围栏),深圳市2025年2月15日更新的全类别AOI,矢量图层(全国22个超大特大城市AOI更新系列)

1070

算法:交替合并字符串

leetcode 交替合并字符串 两个字符串,交替加入到一个字符串中,如果一个字符串较长,直接加在后面。 代码: char * mergeAlternately(char * word1, char * word2){ int l1 = strlen(word1), l2 = strlen(word2); char *result = malloc(sizeof(char) * (l1 + l2 + 1)); memset(result, 0, l1 + l2 + 1);

weixin_46091073的博客 1887

别再为镜面物体检测发愁了!手把手教你用PMD相位偏折术增强2.5D形状图细节(附Python代码)

本文详细解析了PMD相位偏折术在工业视觉中镜面物体检测的应用,通过2.5D成像方案增强形状图细节。文章提供了Python代码示例,介绍了一种计算高效的局部特征增强算法,帮助解决产线环境中的噪声问题,提升缺陷识别准确率。

weixin_29299807的博客 346

交替合并字符串(打卡第一天)

交替合并字符串(打卡第一天)

weixin_51186539的博客 548

招行收款通SM2加密解密全流程解析:PHP开发者必看

本文详细解析了招商银行收款通服务中SM2加密解密的完整流程,特别针对PHP开发者提供了从密钥管理到签名验签的实战指南。涵盖国密算法基础、PHP环境配置、密钥安全存储及生产环境优化方案,帮助开发者快速实现高安全性的支付系统集成。

weixin_30632089的博客 651

手把手教你玩转CPU微架构专栏--​​​​第二章:流水线——微架构的“效率引擎”​​​​2.4 超标量与超线程:如何突破“单指令流”的限制?

例如,Intel酷睿i5-13600K拥有6个性能核(P核,支持超线程,共12线程)和8个能效核(E核,无超线程,共8线程),兼顾多线程性能与能效比。通过引入“物理寄存器”替代“逻辑寄存器”(如x86的RAX、RBX),消除数据冒险中的“假依赖”(False Dependency)。引入“混合架构”(Performance Core+Efficient Core),大核(P核)专注高性能任务,小核(E核)处理后台任务;​:6发射超标量,IPC≈3.5(通过3D V-Cache优化缓存,进一步提升效率)。

xiaoheshang_123的博客 954

【LeetCode 75】【学习笔记】交替合并字符串(Merge Strings Alternately)

给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。 返回合并后的字符串。

Yo! This is Swordup. 812

用 ADT 连接 SAP S/4HANA Public Cloud 开发租户的完整落地指南

摘要:在SAP S/4HANA公有云中进行ABAP开发时,必须使用Development Tenant(客户端080)而非Customizing Tenant(客户端100)连接ADT工具。开发用户需具备SAP_BR_DEVELOPER角色且写权限不受限,开发对象应创建在结构包的子包中。连接前需确认正确的ABAP服务URL(通常为Launchpad URL去掉"/ui"),并通过运行控制台程序验证全链路功能。典型开发场景如通过BAdI实现业务校验时,需遵循Clean Core原则在指定包

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 336

交替合并字符串

给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。

quite123_的博客 471

基于opencv人脸识别实现课堂抬头率检测系统 完整代码 可直接运行 作为毕设

基于opencv人脸识别实现课堂抬头率检测系统 完整代码 可直接运行 作为毕设

机器学习深度学习业余选手 1146

YOLO26改进策略【YOLO和Mamba】| 2024 VM-UNet,高效的特征提取模块VSS block 二次创新提高精度

VM-UNet模型设计的出发点是解决现有CNN-based和模型在医学图像分割中的局限性,利用(SSMs)的优势,提出一种更有效的医学图像分割模型。具体来说,CNN-based模型在捕捉长距离信息方面存在不足,而模型由于自注意力机制的二次复杂度导致计算负担较重。SSMs模型如Mamba不仅在建模长距离依赖方面表现出色,还具有线性计算复杂度,这为VM-UNet的设计提供了理论基础。

Limiiiing的博客 213

1768. 交替合并字符串 (Python3)

Problem: 1768. 交替合并字符串 文章目录思路解题方法Code 思路 和21. 合并两个有序链表在思路上具有一定的相似性; 学会创建dummy变量方便合并。 解题方法 创建dummy变量作为合并的基础; 在循环中利用合适的字符串操作方法交替合并; 实现两种方法,区别在于循环语句条件:一种选择在某个字符串迭代结束后变停止循环,将另一字符串的剩余内容直接合并;一种选择在循环中实现所有字符的合并。 Code class Solution: def mergeAlternately(

Tongcheng_98的博客 778

TIA protal与SCL从入门到精通(4)——软件单元使用

平台: TIA portal 15.1 1.添加软件单元 添加软件单元如下图: 关于软件单元使用基本限制,看如下文章即可 TIA protal与SCL从入门到精通(3)——软件单元基本介绍 每个软件单元是一个独立结构,所以每个软件单元的运行方式与实际PLC类似,所以首先添加主函数块: 注意:无论系统是否是具有并发性(这里不要混淆并行与并发的概念),在PLC所有主函数执行总是按主函数依次完成...

苦走一口田 7375

JAVA 算法-字符串交替合并

1. 为什么需要Java 中String的不可变性Java 的String对象是不可变的(immutable),每次修改(如拼接、替换)都会生成新的String对象。// 实际会创建一个新的 String 对象,而非修改原对象的优势内部维护一个可动态扩容的字符数组,直接在原数组上修改字符,避免频繁创建新对象,适合以下场景:循环中多次拼接字符串(如示例代码中的交替合并)。需要动态构建长字符串(如生成 JSON、XML 数据)。

2403_87072953的博客 1094

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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

力扣刷题笔记:交替合并字符串 (LeetCode1768) — Java

简单来说,这道题就是要求我们将两个字符串穿插在一起,就像我们上学时期站队一样,原本男生一列,女生一列,可是现在要求我们男女穿插合并成一列,在这个过程中会存在有一列队伍人数比另一列多的情况,多的人自然排在后面就好了。2.就像刚刚说的那样,两个队伍合并之后会成为一个新的队伍,所以两个字符串合并之后成为一个新的字符串,所以我们需要提前创建一个结果字符串,将字符一个一个放进结果字符串。开始,通过交替添加字母来合并字符串。1.因为交替合并是需要一个一个穿插的,在这里我们想到肯定要遍历字符串中的每个字符,所以。

2501_90563889的博客 1037

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

1768. 交替合并字符串(Java)

给你两个字符串 word1 和 word2。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。返回 合并后的字符串。

jungleirim的博客 575

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LeetCode 1768 交替合并字符串(java)

class Solution { public String mergeAlternately(String word1, String word2) { StringBuilder sb = new StringBuilder(); int len = Math.max(word1.length(), word2.length()); for(int i = 0; i< len; i++) { if(.

weixin_43170297的博客 503

LeetCode1768 交替合并字符串(Java)(StringBuilder练习)

【代码】LeetCode1768 交替合并字符串(Java)(StringBuilder练习)

weixin_40968325的博客 164

LeetCode ----- 1768. 交替合并字符串(java版)

LeetCode每日一题 ----- 1768. 交替合并字符串(java版)

qq_41536556的博客 365

力扣1768.交替合并字符串

从0-1提高Java代码编程能力,本文描述了一个字符串交替合并算法:给定两个字符串word1和word2,从word1开始交替取字符进行合并。若一个字符串更长,剩余字符直接追加到结果末尾。例如"abc"和"pqr"合并为"apbqcr"。算法需要处理不同长度字符串的情况,示例展示了三种典型场景。输入字符串长度限制在1到100之间,且均为小写字母组成。该问题考察字符串处理的遍历和拼接能力。

AffectionGF的博客 833

Java基础学完,继续深耕01.1(0423)力扣1768.交替合并字符串

遍历之后,肯定要实现拼接,拼接后返回的对象是要呈现出来的结果。拼接用的是append()方法(方便之处)。我是这样写while的。我大概用了40min(大佬别喷我,真是纯小白)写了三种方法(其实都差不多哈哈哈哈)字符串写完后,我又想到,如果将字符串转换为字符数组会怎么样。这个纯纯缝合怪,但是惊奇的发现他在力扣上的时间复杂度居然是!但是我不知道这两方法的优缺点,我再一次咨询了ai(哈哈哈哈哈)看了他的提示(双指针,然后字符串拼接),这才小有思路。此时机智的我想到了结合一下吧。类的一个方法,它的作用是。

m0_61213907的博客 902

力扣 1768. 交替合并字符串

如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。解释:注意,word2 比 word1 长,“rs” 需要追加到合并后字符串的末尾。解释:注意,word1 比 word2 长,“cd” 需要追加到合并后字符串的末尾。输入:word1 = “abc”, word2 = “pqr”输入:word1 = “ab”, word2 = “pqrs”输入:word1 = “abcd”, word2 = “pq”合并后: a p b q r s。合并后: a p b q c r。

(ง •_•)ง没有困难的工作,只有勇敢的狗狗 173

力扣:1768. 交替合并字符串

力扣:1768. 交替合并字符串

a_100100的博客 342

力扣1768.交替合并字符串(java模拟法)

给你两个字符串 word1 和 word2。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。之所以选择StringBuilder去作为结果集,是因为其先对于String和StringBuffer效率更高。我们可以直接选择利用双指针对问题进行模拟。1.循环控制条件为:两个指针只要有其一小于其字符串禅读即可。2.利用两个if分支判断控制依次“插入”返回 合并后的字符串。

LNsupermali的博客 402

力扣day02--交替合并字符串

每日coding+1

樱桃气质丸的博客 1288

【LeetCode算法】1768. 交替合并字符串

给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。

熊熊饲养员的优快云! 390
上一篇: 无重复字符的最长子串 Java
下一篇: 反转字符串中的元音字母 Java
噬~~网
博客等级 码龄7年 132粉丝 19原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值