Java algorithms: 1. skip steps; 2. Calculate the number of 1 in binary; 3. Calculate the number of 1 in decimal.

Source: Internet
Author: User

Some small algorithms are of the Java version, and a large number of questions on the network are aimed at C ++. Therefore, Java implementation is rare, but they are the basis of the test,

 

The implementation is the same. You can broaden your thinking and be helpful.

 

/**
* 1 hop steps
* Question: there are N levels in a step. If you can skip 1 level at a time, you can also skip 2 levels.
* Calculate the total number of hops in total and analyze the time complexity of the algorithm.
* It is a combination question, which is completely correct and difficult,
* The total number of M steps N is 2.
* O (m-N) * (2n-1 )))
*/
Public class jumpfootstep {

Public static void main (string [] ARGs ){
Jumpstepway (10 );
}

Private Static void jumpstepway (int n ){
Int twostep = 1;
While (2 * twostep <= N ){
Printleftjoin (twostep, N );
For (INT I = 1; I <= twostep; I ++ ){
Int onestep = N-2 * twostep;
List <integer> ls = new arraylist <integer> ();

For (Int J = 1; j <= onestep; j ++ ){
For (int K = 0; k <(twostep-I); k ++ ){
Ls. Add (2 );
}
For (INT m = 0; m <j; m ++ ){
Ls. Add (1 );
}
For (INT m = 0; m <I; m ++ ){
Ls. Add (2 );
}
For (INT m = 0; m <onestep-J; m ++ ){
Ls. Add (1 );
}
Print (LS );
Ls. Clear ();
}
If (I! = Twostep ){
For (Int J = 1; j <= onestep; j ++ ){

For (INT m = 0; m <j; m ++ ){
Ls. Add (1 );
}
For (INT m = 0; m <I; m ++ ){
Ls. Add (2 );
}
For (INT m = 0; m <onestep-J; m ++ ){
Ls. Add (1 );
}
For (int K = 0; k <(twostep-I); k ++ ){
Ls. Add (2 );
}

Print (LS );
Ls. Clear ();
}
}

}
Twostep ++;
}
}
Private Static void printleftjoin (INT twostep, int N ){
For (INT I = 1; I <= twostep; I ++ ){
System. Out. Print (2 );
}
For (INT I = 1; I <= N-2 * twostep; I ++ ){
System. Out. Print (1 );
}
System. Out. println ();
}
Private Static void print (list <integer> ls ){
For (INT m = 0; m <ls. Size (); m ++ ){
System. Out. Print (LS. Get (m ));
}
System. Out. println ();
}
}

 

There are repeated outputs in the answer to the step, which is a question to be resolved!

 

/**
* The binary value of 2 integers indicates the number of values in 1.
* Question: enter an integer to calculate the number of 1 in the Binary Expression of this integer.
* For example, input 10. Because the binary value is 1010 and there are two 1 s, Output 2.
*
* @ Author wangjichen
*
*/
Public class binaryonecount {

Public static void main (string [] ARGs ){

Long B = 1101;
Long I = 1;
Int COUNT = 0;
Int time = 0;
While (I> 0 ){
Long F = I & B;
F >>=time;
If (F = 1 ){
Count ++;
}
I <= 1;
Time ++;
}
System. Out. println (count );
}
}

 

Do not move the value of B to the right. If it is compared with I = 1, an endless loop occurs. Think about why...

/**
* 3 number of occurrences of 1 in a positive number from 1 to n
* Question: enter an integer n to calculate the number of times 1 appears in the decimal representation of the N integers from 1 to n.
* For example, input 12. integers from 1 to 12 contain numbers 1, 10, 11, and 12, and 1 appear 5 times in total. Analysis: This is a widely used Google interview question.
*
* @ Author wangjichen
*
*/
Public class decimalonecount {

Public static void main (string [] ARGs ){
Judge (111 );
}

Private Static void judge (int n ){
Int result = 0;
For (INT I = N; I> = 0; I --){
Result + = eachjudge (I );
}
System. Out. println (result );
}
Private Static int eachjudge (int n ){
Int COUNT = 0;
While (n> 0 ){
Int F = n % 10;
If (F = 1 ){
Count ++;
}
N/= 10;
}
Return count;
}
}

 

The answer to the second question is that when B is a negative number, it is an endless loop.

 

I don't know if the examiner wants to take this test, but the general idea is that the moving between the left and right is the dividing line of recruitment.

 

Be cautious about gold!

Related Article

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.