LeetCode(String)1768. Merge Strings Alternately
1.问题
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
Example 1:
Input: word1 = “abc”, word2 = “pqr”
Output: “apbqcr”
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
Example 2:
Input: word1 = “ab”, word2 = “pqrs”
Output: “apbqrs”
Explanation: Notice that as word2 is longer, “rs” is appended to the end.
word1: a b
word2: p q r s
merged: a p b q r s
Example 3:
Input: word1 = “abcd”, word2 = “pq”
Output: “apbqcd”
Explanation: Notice that as word1 is longer, “cd” is appended to the end.
word1: a b c d
word2: p q
merged: a p b q c d
Constraints:
1 <= word1.length, word2.length <= 100
word1 and word2 consist of lowercase English letters.
2. 解题思路
方法1:
1.计算字符串 word1 和 word2 的长度
2.定义 StringBuffer 类型的结果字符串 s
3. 定义指针 i,初始值为 0
4.当两个字符串的长度均未变为 0 时,依次取出一个字符,将其拼接到结果字符串中
5.返回最终的结果字符串
方法2:
使用双指针,从两个字符串的第一个字符开始,依次取出一个字符,将其拼接到结果字符串中。然后移动指针,直到其中一个字符串的指针到达字符串末尾。
方法3:
- 定义空字符串作为结果
- 循环遍历长度
- 如果word1长度大于i,则把第i个字符加入结果字符串中
- 如果word2长度大于i,则把第i个字符加入结果字符串中
- 返回结果字符串
3. 代码
代码1:
class Solution {public String mergeAlternately(String word1, String word2) {// 计算字符串 word1 和 word2 的长度int len1 = word1.length();int len2 = word2.length();// 定义 StringBuffer 类型的结果字符串 sStringBuffer s = new StringBuffer();// 定义指针 i,初始值为 0int i = 0;// 当两个字符串的长度均未变为 0 时,依次取出一个字符,将其拼接到结果字符串中while (len1 != 0 || len2 != 0) {if (len1 != 0) {s.append(word1.charAt(i));len1--;}if (len2 != 0) {s.append(word2.charAt(i));len2--;}i++;}// 返回最终的结果字符串return s.toString();}
}
代码2:
class Solution {public String mergeAlternately(String word1, String word2) {// 计算字符串 word1 和 word2 的长度int n1 = word1.length(), n2 = word2.length();// 定义 StringBuilder 类型的结果字符串 resStringBuilder res = new StringBuilder();// 定义双指针 i 和 j,初始值均为 0int i = 0, j = 0;// 当两个指针均未到达字符串末尾时,依次取出一个字符,将其拼接到结果字符串中while (i < n1 && j < n2) {res.append(word1.charAt(i++));res.append(word2.charAt(j++));}// 当其中一个字符串的指针到达字符串末尾时,将另一个字符串的剩余部分全部拼接到结果字符串中if (i < n1) {res.append(word1.substring(i));}if (j < n2) {res.append(word2.substring(j));}// 返回最终的结果字符串return res.toString();}
}
解题思路相同
class Solution {public String mergeAlternately(String w1, String w2) {int n = w1.length(), m = w2.length(), i = 0, j = 0;StringBuilder res = new StringBuilder(); // 定义 StringBuilder 类型的结果字符串 reswhile (i < n || j < m) { // 只要 i 或 j 指针未到达字符串末尾,就继续拼接字符串if (i < w1.length()) // 如果 i 指针未到达 w1 的末尾,就取出当前字符,拼接到结果字符串中res.append(w1.charAt(i++));if (j < w2.length()) // 如果 j 指针未到达 w2 的末尾,就取出当前字符,拼接到结果字符串中res.append(w2.charAt(j++));}return res.toString(); // 返回最终的结果字符串}
}
class Solution {public String mergeAlternately(String word1, String word2) {StringBuilder result = new StringBuilder(); // 定义 StringBuilder 类型的结果字符串int i = 0;while (i < word1.length() || i < word2.length()) { // 只要 i 指针未到达 word1 和 word2 的末尾,就继续拼接字符串if (i < word1.length()) { // 如果 i 指针未到达 word1 的末尾,就取出当前字符,拼接到结果字符串中result.append(word1.charAt(i));}if (i < word2.length()) { // 如果 i 指针未到达 word2 的末尾,就取出当前字符,拼接到结果字符串中result.append(word2.charAt(i));}i++; // 指针向后移动一位}return result.toString(); // 返回最终的结果字符串}
}
代码3:
class Solution {public String mergeAlternately(String word1, String word2) {String ans = ""; // 定义空字符串作为结果int len = Math.max(word1.length(), word2.length()); // 计算两个字符串的最大长度for (int i = 0; i < len; i++) { // 循环遍历长度if (i < word1.length()) ans += "" + word1.charAt(i); // 如果word1长度大于i,则把第i个字符加入结果字符串中if (i < word2.length()) ans += "" + word2.charAt(i); // 如果word2长度大于i,则把第i个字符加入结果字符串中}return ans; // 返回结果字符串}
}
LeetCode(String)1768. Merge Strings Alternately相关推荐
- leetcode 1768. Merge Strings Alternately(交替合并字符串)
把word1和word2中的字母交替地整合成一个字符串. 思路: 很简单,主要是看什么时候放word1, 什么时候放word2, 可用一个0,1决定,每次异或1. public String merg ...
- LeetCode(String) 2325. Decode the Message
1.问题 You are given the strings key and message, which represent a cipher key and a secret message, r ...
- LeetCode(String) 2011. Final Value of Variable After Performing Operations
1.问题 There is a programming language with only four operations and one variable X: ++X and X++ incre ...
- LeetCode (12.整数转罗马数字)JAVA StringBuffer
LeetCode (12.整数转罗马数字)JAVA StringBuffer 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 1 ...
- LeetCode(13.罗马数字转整数) JAVA Hashmap
LeetCode(13.罗马数字转整数) JAVA Hashmap 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...
- Leetcode(51)——N 皇后
Leetcode(51)--N 皇后 题目 按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子. n n n 皇后问题 研究的是如何将 n n n 个皇后放置在 n × n n ...
- LeetCode(1.俩数之和)JAVA
LeetCode(1.俩数之和) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...
- LeetCode(9.回文数)JAVA
LeetCode(9.回文数) 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输 ...
- scala 时间格式转换(String、Long、Date)
1)scala 时间格式转换(String.Long.Date) 1.时间字符类型转Date类型 [java] view plain copy import java.text.SimpleDateF ...
最新文章
- UVA12299 线段树水水水,但别乱开空间= =
- pre_forum_thread 主题表跟pre_forum_thread内容表是用哪个字段对应起来的?
- mysql myisam存储引擎_MySQL浅谈MyISAM存储引擎
- CDH HUE集成MySQL
- table ADR6 引起的equipment download error
- SecureCRT 设置和修改
- 什么?面试官问我Java内存模型!这不得给我加薪?
- 消息队列NetMQ 原理分析2-IO线程和完成端口
- 月入过万的副业你要不要?不需要编程知识,不限男女,不限学历
- Java案例:统计文本中所有整数之和
- L1-037 A除以B (10 分)—团体程序设计天梯赛
- pyautogui的两天坑moveto图像识别
- android 左移动画_android 动画Animation之TranslateAnimation移动
- 魅族u20怎么刷Android,魅族魅蓝U20/U10一键Root权限获取+USB驱动安装
- Jo-SRC: A Contrastive Approach for Combating Noisy Labels
- “中国年龄最小的黑客”汪正扬资料介绍 写编程代码曾敲坏电脑
- 苹果开放降级_苹果官方为什么不开放 iOS 降级验证通道?
- 一文搞懂 deconvolution、transposed convolution、sub-pixel or fractional convolution
- 计算机视觉之目标检测(object detection)《1》
- c++实现种子填充算法与扫描线算法
热门文章
- java环境搭好 软件无法运城_Javaweb开发环境搭建常用工具类型
- cocos2d-x 基本样条动作
- linux centos 安装rar文件,在centos下安装rar解压.rar压缩包
- 大彩CAN通讯串口屏如何实现串口更新菜单选项
- java计算机毕业设计花田音乐网站源码+mysql数据库+系统+lw文档+部署
- Netsuite本地化解决方案
- Wondershare UniConverter for Mac(全能视频格式转换器)
- Tesseract-OCR中文语言包缺失的解决办法
- 光纤激光切割机功率下降的原因分析
- python电文加密_电文加密