"365 Algorithm Daily Learning Plan": 01 Punch-In

Source: Internet
Author: User

I have been thinking, how to do the training of the algorithm, because the individual in the algorithm this aspect of mastery is indeed not enough. Therefore, I now want to do a "365 Algorithm daily Learning Plan".

The main purpose of the plan:

1, want to through this way to supervise their own more efforts to learn the algorithm.

2, want to and small partners "group" together to learn the exchange of learning algorithms in the process of drip.

The main contents of the plan are:

1, the basic knowledge of data structure and algorithm is consolidated.

2, progressive step of the OJ algorithm training.

Schedule : every Wednesday and Saturday

--Say in front

"Algorithm Daily Learning" program 01 Punch-In:
Problem description
For a 01-string length of 5 bits, each bit can be 0 or 1, a total of 32 possible. The first few of them are:

00000

00001

00010

00011

00100

Please output these 32 kinds of 01 strings in order from small to large.

Input format

This question is not entered.

Output format

Output 32 rows, in order from small to large, each line is a length of 5 01 strings.

Sample output

00000

00001

00010

00011

< The following sections omit >

The thinking and realization of solving problems

If a small partner rarely touches this topic, may feel a little strange, do not know where to start, maybe at the beginning we can think of "the most stupid" method, but also feel very "entertaining" method.

System.out.println("00000")..........System.out.println("11111")

This way is not to be able to get the final result, yes, of course, no problem, but we can think of the time step, try a variety of methods to find the best solution.

This method does not seem very good, one is not flexible enough, and the second is to knock code very tired, so, improve a bit.

This is not a more flexible way to solve this problem, the solution is what we often call "brute force", all with a for loop to traverse all cases, if found to match the situation of the output, but we will find that the algorithm's time complexity is: O(n^5) , This method is better than the previous one, but it is not the best answer.

public static void main(String[] args) {        for (int i = 0; i < 32; i++) {            String result = Integer.toBinaryString(i);            int num = result.length();                for (int j = 0; j < 5 - num; j++) {                    result = "0" + result;                }                System.out.println(result);    }}

Then take a look at this approach, the idea of this approach: through the JDK method Integer.toBinaryString() to get to each number of binary, because the output is required to be the shape “11111” of the five-bit number, so we also need to be based on the length of the obtained binary number, in front of the string, add 5 - num a “0” , For example, the resulting binary is 1 (the length is 1), so the 1 front to add 5-(num=1) equals 4 0 .

is not particularly concise, and the efficiency of this method should also be good: O(n) because this is the method provided by the JDK, at the bottom is a displacement method to achieve (note: We do not recommend the use of the JDK approach to solve, we try to use their own way of thinking to solve, even if the method is "stupid", But it is also self-thinking).

Of course, if we change the angle, we can also go to another solution.

 public static void main(String args[]){         for(int i=0;i<32;i++){             String str = Integer.toBinaryString(i);             switch (str.length()) {             case 1:                 str = "0000"+str;                 break;             case 2:                 str = "000"+str;                break;            case 3:                str = "00"+str;                break;            case 4:                str = "0"+str;                break;            }                System.out.println(str);        }    }}

This solution is only solved in a switch-case way, and the idea is the same as above.

Finally, a method that does not use the JDK to solve:

The idea of this approach is not provided, leaving the small partners to think for themselves, if the small partners have their own ideas, welcome to the small partners in the message area to give your ideas or solutions.

In addition, created an "algorithm daily learning Exchange Community", if you want to join the small partner, you can sweep the following QR code plus I as a friend, I pull you into the group (note: The above several algorithms from the "algorithm daily Learning Exchange Community" of small partners).

The article is inappropriate, please correct me, you can also pay attention to my public number: 好好学java , access to high-quality learning resources.

"365 Algorithm Daily Learning Plan": 01 Punch-In

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.