首页 > 其他分享 >LeetCode75 1768.交替合并字符串

LeetCode75 1768.交替合并字符串

时间:2024-03-06 12:55:05浏览次数:16  
标签:String LeetCode75 stringBuilder len1 1768 word1 word2 字符串 append

1768.交替合并字符串
https://leetcode.cn/problems/merge-strings-alternately/description/?envType=study-plan-v2&envId=leetcode-75

public String mergeAlternately(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        StringBuilder stringBuilder = new StringBuilder();
        int i = 0, j = 0;
        while (i < len1 && j < len2){
            stringBuilder.append(word1.charAt(i)).append(word2.charAt(j));
            i++;
            j++;
        }
        if (i == len1 && j == len2) return stringBuilder.toString();
        if (i == len1){
            stringBuilder.append(word2.substring(j,len2));
        }else {
            stringBuilder.append(word1.substring(i,len1));
        }
        return stringBuilder.toString();
    }

总结:这种构造新字符串的使用StringBuilder类很方便 ,String类的substring方法是左闭右开的方法。

标签:String,LeetCode75,stringBuilder,len1,1768,word1,word2,字符串,append
From: https://www.cnblogs.com/jeasonGo/p/18056282

相关文章

  • JS字符串、数组 常用方法
    字符串字符串增:1、+拼接2、concat()例:leta='hello'  letb=a.concat('word')  console.log(b) // "helloworld" 字符串删:1、slice(star,end)  从原始字符串中提取一个子字符串,并返回一个新的字符串,而不改变原字符串。start(必需):起始位置。如果是正数,则......
  • day 05-3 数据类型(字符串)
    3.3公共功能1.字符串相加v1="linzai"+"是个好人"print(v1)#linzai是个好人2.字符串相乘v1="linzai"*3print(v1)#linzailinzailinzai3.计算字符串的长度v1="linzai"data=len(v1)#计算字符串的长度print(data)#64.获取字符串的字符,索引字符......
  • snappy压缩格式下使用数字与字符串不等于比较,hiveSQL和sparkSQL表现不一致的行为记录
    Hive版本:2.3.4Spark版本:2.4.0当时用Snappy格式对表进行压缩时,时用<>符号将字符串与数字进行比较会产生不一致的结果。SparkSQL结果并非预期结果。DROPTABLEIFEXISTStest.zero_test;CREATETABLEtest.zero_testTBLPROPERTIES("orc.compress"="SNAPPY")ASSELECT......
  • 往 netty Channel中写入字符串
    示例代码:EventLoopGroupgroup=newNioEventLoopGroup();Bootstrapbootstrap=newBootstrap();bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE,true)......
  • Python 生成随机字符串
    0x00吐槽最近让项目坑的没办法,老写一些脚本来协助工作,刚好在测试python生成word的时候遇到需要随机字符串来命名文档名,简单写点东西记录一下0x01一班的童靴其实随机字符串这个东西在任何语言里都经常会用到,而且解决方法也简单首先定义一个字符串,随机字符串就从这里面取,然......
  • python数据类型与字符串常用方法
    int-py2中有:int/long;py3中有int。-强制转换:int(''76"")-除法:py2(多加一行代码)和py3(正常)boolTrue/False(其他语言:true/false)特殊为False的其他类型:0和""str独有功能upper/lowerreplacestrip/lstrip/rstripisdigitsplit/r......
  • c语言进行时——字符串
    字符串定义字符串用的函数......
  • 使用setmetatable和__call元方法来实现根据字符串名字调用对应的函数
    cc.lualocalCMD={}--定义两个函数functionCMD.func1()print("Function1called")endfunctionCMD.func2()print("Function2called")endreturnCMD test.lualocalfunctions=require"cc"--设置表的元表和__call元方法se......
  • JSON.parse解析字符串报错-SyntaxError: Unexpected token ‘ in JSON at position 报
    “SyntaxError:Unexpectedtoken’inJSONatposition”报错原因是因为解析的字符串对象中,JSON.parse无法识别;JSON.parse可以将标准的json类型数据转换为JavaScript对象,如果数据不是正确的json类型的数据则会控制台报错,可能会阻断代码的正常运行我们可以写一个函数来......
  • 11_C# 中字符串 string.Empty,"",null 三者的区别
    C#中字符串string.Empty、""和null三者的区别1.string.Emptystring.Empty是一个表示空字符串的静态字段。它实际上等同于""(空字符串),但使用string.Empty可以更加清晰地表示我们需要一个空字符串。2.""(空字符串)双引号中没有任何字符的字符串被称为空字符串。它......