题目
腾讯2020-1
腾讯2020-1
package demoProj
;
import java
.util
.Collections
;
import java
.util
.Scanner
;
public class TencentXZ2020_1 {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
String next
= scanner
.next();
scanner
.close();
System
.out
.println(decode(next
));
}
public static String
decode(String words
) {
while (words
.contains("]")) {
int right
= words
.indexOf("]");
int left
= words
.lastIndexOf("[", right
);
String repeatStr
= words
.substring(left
+ 1, right
);
String
[] split
= repeatStr
.split("\\|");
words
= words
.replace("[" + repeatStr
+ "]",
String
.join("", Collections
.nCopies(Integer
.parseInt(split
[0]), split
[1])));
}
return words
;
}
}
笔记:
words.indexOf() indexOf()方法 words.substring() substring()方法 repeatStr.split() 根据符号分隔字符串;“ \\ ”为转义符。words.replace() 新字符替换旧字符String.join()Collections.nCopies() public static List nCopies(int n, T o) n:返回列表中元素的数量。 o:重复出现在返回列表中的元素。