자바 7, 725 바이트
f(int)( 325 바이트 ) :
String f(int i){String s="";for(int j=0,e=0;e<i;e+=v(s))s=Integer.toBinaryString(j++);return"["+s.replace("1","[").replace("0","]")+"]";}int v(String s){for(;!s.isEmpty();s=s.replaceFirst("1","").replaceFirst("0",""))if(s.replace("1","").length()!=s.replace("0","").length()|s.charAt(0)<49|s.endsWith("1"))return 0;return 1;}
g(String)( 75 + 325 바이트 ) :
int g(String s){int r=0;for(String i="10";!i.equals(s);i=f(++r));return r;}
메소드 g는 메소드 f를 사용 하여 입력 된 것과 동일한 것을 발견 할 때까지 가능한 void-list를 반복하여 결과를 계산하므로 바이트 수는 f두 번 계산됩니다 (두 가지 방법 모두이 문제에 대해 다른 방법없이 실행될 수 있기 때문에).
설명:
일반적으로, 메소드 f는 정수의 모든 이진 문자열 표현을 단순히 반복하고 유효한 것을 찾을 때마다 카운터를 증가시킵니다. 이 챌린지에 유효한 이진 문자열은 다음 규칙을 따릅니다. 규칙은으로 시작 1하고 0;으로 끝납니다 . 그것들은 1과 0의 같은 수를 가지고; 모든 시간은 첫 번째를 제거 1하고 0하고,이 두 가지 규칙이 여전히 적용 것을 다시 남아 확인합니다. 카운터는 입력과 동일하면, 모든 대체함으로써, 문자열 공극 목록이 이진수 문자열로 변환 1하여 [, 모든0 을] .
method g: 우리는 "[]"(void-list를 나타내는 0)로 시작한 다음 finput-String과 일치 할 때까지 정수를 늘리면서 method를 계속 사용 합니다.
String f(int i){ // Method `f` with integer parameter and String return-type
String s=""; // Start with an empty String
for(int j=0,e=0;e<i; // Loop as long as `e` does not equal the input
e+=v(s)) // And append increase integer `e` if String `s` is valid
s=Integer.toBinaryString(j++);
// Change `s` to the next byte-String of integer `j`
// End of loop (implicit / single-line body)
return"["+ // Return the result String encapsulated in "[" and "]"
s.replace("1","[").replace("0","]")+"]";
// after we've replaced all 1s with "[" and all 0s with "]"
} // End of method `f`
int v(String s){ // Separate method with String parameter and integer return-type
for(;!s.isEmpty(); // Loop as long as String `s` isn't empty
s=s.replaceFirst("1","").replaceFirst("0",""))
// After each iteration: Remove the first "1" and "0"
if(s.replace("1","").length()!=s.replace("0","").length()
// If there isn't an equal amount of 1s and 0s
|s.charAt(0)<49 // or the String doesn't start with a 1
|s.endsWith("1")) // or the String doesn't end with a 0
return 0; // Return 0 (String is not valid)
// End of loop (implicit / single-line body)
return 1; // Return 1 (String is valid)
} // End of separate method
int g(String s){ // Method `g` with String parameter and integer return-type
int r=0; // Result integer
for(String i="[]";!i.equals(s);
// Loop as long as `i` does not equal the input String
i=f(++r)); // After each iteration: Set `i` to the next String in line
return r; // Return the result integer
} // End of method `g`
입력 및 출력 사례 예 :
여기에서 시도하십시오. (참고 : 지난 몇 가지 테스트 사례의 경우 속도가 매우 느립니다. 모든 경우 10-15 초 정도 걸립니다.)
0 <-> []
1 <-> [[]]
2 <-> [[][]]
3 <-> [[[]]]
4 <-> [[][][]]
5 <-> [[][[]]]
6 <-> [[[]][]]
7 <-> [[[][]]]
8 <-> [[[[]]]]
9 <-> [[][][][]]
10 <-> [[][][[]]]
11 <-> [[][[]][]]
12 <-> [[][[][]]]
13 <-> [[][[[]]]]
14 <-> [[[]][][]]
50 <-> [[[][[[]]]]]
383 <-> [[[][]][[[][]]]]