讨论/题目交流/🐱 第 24 场夜喵双周赛/
🐱 第 24 场夜喵双周赛
展开讨论

题3
n = 1, 1 ~ 1 * 3
n = 2, 2 ~ 2 * 3
...
n = n, 2^(n-1) ~ 2^(n-1) * 3
所以k = 9, 由8, 1, 1组成 = c + a + b

public String getHappyString(int n, int k) {
    int val = (int)Math.pow(2, n-1);
    if ( k > val * 3 ) {
        return "";
    }
    char c = 'a';
    int step = 0;
    StringBuilder sb = new StringBuilder();
    int i = 1;
    while ( sb.length() < n ) {
        val = (int)Math.pow(2, n - i);
        step = 0;
        while ( true ) {
            if ( sb.length() > 0 && c + step == sb.charAt(sb.length() - 1) ) {
                step = (step + 1) % 3;
            }
            if (val >= k) {
                sb.append((char) (c + step));
                break;
            } else {
                k -= val;
                step = (step + 1) % 3;
            }
        }
        ++ i;
    }
    return sb.toString();
}
2
展开全部 32 讨论