Tencent algorithm interview question algorithm and code

Source: Internet
Author: User

The question is as follows: give you 10 minutes, and give you ten numbers based on the upper row, fill in the corresponding 10 numbers in the lower row. Each number in the lower row is the number of times that the corresponding number in the previous upper row appears in the next row. The code for the top ten rows is as follows: /*** // *** http://yydcj.iteye.com/*/public class Test {public static void main (String [] args) {NumberTB nTB = new NumberTB (18 ); int [] result = nTB. getBottom (); for (int I = 0; I <result. length; I ++) {System. out. print (result [I] + "") ;}} static class NumberTB {private int [] top; private int [] bottom; private int len; private boolean success; // please Into len> = 4 public NumberTB (int len) {this. len = len <= 4? 4: len; this. success = false; this. top = new int [this. len]; this. bottom = new int [this. len]; // format top for (int I = 0; I <this. len; I ++) {this. top [I] = I ;}} public int [] getBottom () {int I = 0; while (! This. success) {I ++; setNextBottom ();} System. out. println ("executed:" + I + "the result of the next loop"); return this. bottom;} // set next bottom private void setNextBottom () {boolean reB = true; for (int I = 0; I <this. len; I ++) {int frequecy = getFrequecy (I); if (this. bottom [I]! = Frequecy) {this. bottom [I] = frequecy; reB = false ;}} this. success = reB;} // get frequency in bottom private int getFrequecy (int num) {int count = 0; for (int I = 0; I <this. len; I ++) {if (this. bottom [I] = num) count ++;} return count ;}} below is a more general conclusion: This is a regular rule, not only 0 ~ 9 has a unique solution, 0 ~ N has only a unique solution. The key is the last side of a 1 it can move around, 1 and 2 under the number is always 2 and 1, 0 under the corresponding number of n-3 (n> = 3 ), the number of n-3 under the corresponding number of 1, the number of other rows should be 0 on the OK. With this general conclusion, the answer can be given immediately after a large number. Www.2cto.com, for example, 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12 12 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 please verify, this algorithm can be used in the data compression field.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.