우리는이 :몇 개의 유효한 괄호 조합입니까?
n1
{}
브라켓의 수,()
브래킷의n2
번호,[]
브래킷의n3
번호,
우리가 가질 수있는이 브래킷 중 얼마나 많은 다른 유효한 조합이 있습니까?
내가 생각하는 것 : 나는
(코드는 일반적인 경우를 위해, 그것은 가능한 최악의 솔루션을 알고, (다음에 오는) 자바의 무력 코드를 작성하고 가능한 모든 조합을 계산하는 우리는 다른 종류의 괄호를 가질 수 있습니다.)
어떤 수학적 접근 방식입니까?
주 1 : 유효한 조합은 평소와 같이 정의됩니다. {{()}}
: 유효한, {(}){}
: 유효하지 않은
주 2 :의 우리가 {}
2 쌍, ()
1 쌍 []
1 쌍의 유효한 조합의 수는 168이 될 것이며, 가능한 모든 수를 가지고 있다고 가정하자 (유효 & 무효) 조합은 840
static void paranthesis_combination(char[] open , char[] close , int[] arr){
int l = 0;
for (int i = 0 ; i < arr.length ; i++)
l += arr[i];
l *= 2;
paranthesis_combination_sub(open , close , arr , new int[arr.length] , new int[arr.length], new StringBuilder(), l);
System.out.println(paran_count + " : " + valid_paran_count);
return;
}
static void paranthesis_combination_sub(char[] open , char[] close, int[] arr , int[] open_so_far , int[] close_so_far, StringBuilder strbld , int l){
if (strbld.length() == l && valid_paran(open , close , strbld)){
System.out.println(new String(strbld));
valid_paran_count++;
return;
}
for (int i = 0 ; i < open.length ; i++){
if (open_so_far[i] < arr[i]){
strbld.append(open[i]);
open_so_far[i]++;
paranthesis_combination_sub(open , close, arr , open_so_far , close_so_far, strbld , l);
open_so_far[i]--;
strbld.deleteCharAt(strbld.length() -1);
}
}
for (int i = 0 ; i < open.length ; i++){
if (close_so_far[i] < open_so_far[i]){
strbld.append(close[i]);
close_so_far[i]++;
paranthesis_combination_sub(open , close, arr , open_so_far , close_so_far, strbld , l);
close_so_far[i]--;
strbld.deleteCharAt(strbld.length() -1);
}
}
return;
}
유효한 조합을 어떻게 정의합니까? 파싱 규칙에 따라이 질문에 대한 여러 답변이있을 수 있습니다. –
질문 수정되었습니다. –
카탈로니아 어 번호를 찾으십시오. ({})이 유효합니까? – Joni