1,11,21,1211,111221 Algorithm interview questions explained

Posted May 25, 20202 min read

The first number is:1;

The second number is:11;

The third number is:21;

The fourth number is:1211;

The fifth number is:111221;

The twenty-seventh number has a total of 2012;

How many digits are there in the twelfth number?

The law is:

Number 2:It is the interpretation of the first number:1 ---- 11, 1 1.

Number 3:It is the interpretation of the second number:11 ---- 21, 2 1.

The fourth number:is the explanation of the third number:21 ---- 1211, one 2, two 1.

The fifth number:is the interpretation of the fourth number:1211 ----> 111221, 1 one, 1 2, 2 1.

Number 6:is the interpretation of the fifth number:111221-"312211, 3 1, 2, 2 1, 1.

Number 7:is the interpretation of the sixth number:312211 ---- "13112221, 1 3, 1 1, 2 2, 2 1.

The eighth number:is the interpretation of the seventh number:13112221 ---- "1113213211, one 1, one 3, two 1, three 2, one 1.

The 9th number:is the interpretation of the eighth number:1113213211 ---- "31131211131221, 3 1, 1, 3, 1, 2, 1, 1, 3, 1, 2, 2. 1.

Remove the "number" and "," to get the next number

Number 10:31131211131221 ----> 13211311123113112211

The 11th number:13213311123113112211 ---- "11131221133112132113212221

The 12th number:11131221133112132113212221 ---- "3113112221232112111312211312113211 Total 34

Answer 34.

Next please look at the algorithm program ~

    String []args = new String [count];
    args [0]= "1";
    args [1]= "11";
    for(int i = 2; i <count; i ++) {
        int j = 1;
        String result = "";
        String arg = args [i-1];
        for(int n = 0; n <arg.length(); n ++) {
            if(n <arg.length()-1) {
                if(arg.charAt(n) == arg.charAt(n + 1)) {
                    j ++;
                } else {
                    result + = String.valueOf(j) + arg.charAt(n);
                    j = 1;
                }
            } else {
                result + = String.valueOf(j) + arg.charAt(n);
                j = 1;
                args [i]= result;
            }
        }
    }


Line 1:1 bit
1
Line 2:2 digits
11
Line 3:2 digits
twenty one
Line 4:4 digits
1211
Line 5:6 digits
111221
Line 6:6 digits
312211
Line 7:8 bits
13112221
Line 8:10 digits
1113213211
Line 9:14 digits
31131211131221
Line 10:20 digits
13211311123113112211
Line 11:26
11131221133112132113212221
Line 12:34
3113112221232112111312211312113211
Line 13:46
1321132132111213122112311311222113111221131221
Line 14:62
11131221131211131231121113112221121321132132211331222113112211
Line 15:78
311311222113111231131112132112311321322112111312211312111322212311322113212221
Line 16:102 bits
132113213221133112132113311211131221121321131131131132221123113112221131112311332111213211322211312113211
Line 17:134
11131221131211132221232112111312212321123113112221121113122113111231133221121321132132211331121321231231121113122113322113111221131221
Line 18:176
31131122211311123113321112131221123113112211121312211213211311322122112311311222113311213212322211211131221131211132221232112111312111213111213211231131122212322211331222113112211
Line 19:226th
1321132132211331121321231231121113113111121121321132122311211131122211211131221131211132221121321132132212321121113113112112133221123113112221131112311332111111122122311311123112111331121113122112132113213211121332212311322113212221
Line 20:302
111312211312111322212321121113121112131112132112311311321322112111312211312112213211231132132132211231131122211311123113322112111312211312111322111213122112311311123112112322211213211321322113311213212312311211131122211213211331121121123123211231131122211211131131131111311312311211232211 211132

Process finished with exit code 0 ~~~~