虽然并没参加笔试,但看第一题挺有意思就做了做。
思路是递归,有兴趣可以看看。
public class TX_Q1 { public static void main(String[] args) { String str = "HG[3|B[1|CA]]F[3|B[1|CA]]BB"; System.out.println(decrypt(str)); } public static String decrypt(String str) { if(str.indexOf('[') < 0) return str; String res = ""; for(int i = 0; i < str.length(); i ++) { char ch = str.charAt(i); if(ch == '[') { int count = 1; int nStart = i + 1; while(str.charAt(i) != '|') i ++; int num = Integer.valueOf(str.substring(nStart, i)); int dStart = i + 1; while(count > 0) { i ++; if(str.charAt(i) == '[') count ++; if(str.charAt(i) == ']') count --; } String ds = decrypt(str.substring(dStart, i)); while(num -- > 0) res += ds; } else { res += ch; } } return res; } }