我想要创建一个程序来显示n对括号的所有有效安排。输出应该是一个按ASCII值的升序排序的数组。下面是需要使用的函数。
fun solution(n: Array<String>): Array<String> {
}我试过用这个,但不能使它符合上面的功能,
fun balancedBracket(result: String,
size: Int,
open: Int,
close: Int): Unit
{
if (close == size)
{
// When get the result of parentheses in given size
println(result);
return;
}
if (open < size)
{
// Add open parentheses
this.balancedBracket(result + "(",
size, open + 1, close);
}
if (open > close)
{
// Add close parentheses
this.balancedBracket(result + ")",
size, open, close + 1);
}
}输入:n=3输出:“(())”、"()(())“、”()“
请帮帮忙。
发布于 2022-06-22 10:54:17
fun printParentheses(size: Int) {
fun inner(chars: String, size: Int, open: Int, close: Int) {
if (close == size) {
println(chars)
return
}
if (open < size) {
inner("$chars(", size, open + 1, close)
}
if (open > close) {
inner("$chars)", size, open, close + 1)
}
}
if (size > 0) {
inner("", size, 0, 0)
}
}
printParentheses(3)https://stackoverflow.com/questions/72710641
复制相似问题