-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1768.java
More file actions
34 lines (31 loc) · 1.05 KB
/
LeetCode1768.java
File metadata and controls
34 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class LeetCode1768 {
public static void main(String[] args) {
// 输入:word1 = "abc", word2 = "pqr"
// 输出:"apbqcr"
System.out.println(new Solution1768().mergeAlternately("abc", "pqr"));
// 输入:word1 = "ab", word2 = "pqrs"
// 输出:"apbqrs"
System.out.println(new Solution1768().mergeAlternately("ab", "pqr"));
// 输入:word1 = "abcd", word2 = "pq"
// 输出:"apbqcd"
System.out.println(new Solution1768().mergeAlternately("abcd", "pq"));
}
}
class Solution1768 {
public String mergeAlternately(String word1, String word2) {
int p1 = 0;
int p2 = 0;
StringBuilder sb = new StringBuilder();
while (p1 != word1.length() || p2 != word2.length()) {
if (p1 != word1.length()) {
sb.append(word1.charAt(p1));
p1++;
}
if (p2 != word2.length()) {
sb.append(word2.charAt(p2));
p2++;
}
}
return sb.toString();
}
}