Java 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;}
由于method g使用method f来遍历可能的void列表,直到找到一个等于所输入的void的方法,才能计算出结果,因此的字节f计数了两次(因为在此挑战下,两种方法都应该能够在没有其他方法的情况下运行)。
说明:
通常,方法f仅循环所有整数的二进制字符串表示形式,并在每次找到有效的计数器时增加一个计数器。针对此挑战的有效二进制字符串符合以下规则:它们以a开头,以a 1结尾0。它们具有相等的1和0。每次删除第一个1并0再次验证剩下的内容时,这两个规则仍然适用。计数器等于输入后,二进制字符串转换为字符串空隙列表,更换所有1与[所有0带]。
至于method g:我们从"[]"(代表void-list 0)开始,然后f在增加整数的同时继续使用method ,直到它与input-String匹配为止。
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 <-> [[[][]][[[][]]]]