After brushing the power button question, I also changed a hair out of control?

Time:2020-6-28

As a rookie in the field of programmers, it can be said that, from a large number of crud businesses, we have learned the skill of copy and paste.

Because we are not all saying: program = Copy + Baidu, a little more advanced is copy + Google. (I can’t help thinking, is science advanced on the Internet?)

Anyway, it’s all about fetching. The code can run and it’s finished. It’s really not good. It’s easy to delete the library and run^_ ^。

After brushing the power button question, I also changed a hair out of control?

However, due to the constant outflow of “35 year old programmer crisis” topic on the Internet, I have to start to seriously think about how far away I am from 35 year old young people running for three years.

Plus, everyone around me is talking about the importance of algorithms. It seems that I really need to reconsider the definition of “program”. Look at the official definition of the serious version…

Program = algorithm + data structure

As a result, I began to pay attention to the importance of algorithms and data structures. It’s time to take out the collection plates lying on the net and have a sun exposure.

However, if we only look at theory and don’t make questions, isn’t that a hooligan. I’m also a serious and honest young man. I can’t fail to live up to leetcode’s kindness to the majority of programmers of the right age.

I have to apply for an account number first when I start from the flat ground? But I forgot all about it. How to brush questions? It’s hard again. Fortunately, I have Baidu, Google (cough, no search engine).

At this time, I found out that I saw a particularly interesting sentence in somewhere, saying that there was a friend who started the algorithm from the “sum of two numbers”, and then it became irremediable. When I became general manager and CEO, I married a rich woman. It’s not white, rich and beautiful. I’m on the top of my life.

I’m lovely. Is it so magical. I don’t believe it. I also saw the four big words “sum of two numbers” on the home page. How can I have no desire to click at all.

In order to feel the change personally, I decided to explore it. It doesn’t matter if I don’t look at it. After I look at it, I can’t deal with it. I can’t help feeling it. Oh, this question is quite interesting?!

The original question is as follows:

Given an integer array nums and a target value target, please find the two integers with and as the target value in the array and return their array subscripts.

You can assume that each input corresponds to only one answer. However, you cannot reuse the same elements in this array.

Example:

Given nums = [2, 7, 11, 15], target = 9

Because nums [0] + nums [1] = 2 + 7 = 9
So return [0, 1]

Seeing this problem, maybe many people will say, what’s the difficulty? Two for loops will be solved. Look at my solution. (I set target to 13)

public class SumTest {
    public static void main(String[] args) {
        int[] arr =  {2, 7, 11, 15};

        int[] result = sumTwo(arr , 13);
        System.out.println(Arrays.toString(result));

    }

    private static int[] sumTwo(int[] nums, int target) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }

}

The idea is that the outer loop starts from the first element of the array and records the loop variable as I, then the inner loop J starts from I + 1. Finally, if the sum of the two elements is equal to the target value, we can get the corresponding array subscript.

That’s right, but it doesn’t take time complexity into account. We know that the superposition of two layers of loops will change the time complexity to o (n ^ 2). When the data volume is small, it is not obvious. When the data volume is large, the execution efficiency will drop (oh no, it should be the square order).

I really can’t think of any good way. Fortunately, there is a discussion area and a solution area. You can watch the solutions of all kinds of gods and official solutions.

Sure enough, a better algorithm was found. Look,

private static int[] sumTwo(int[] nums, int target) {

    HashMap map = new HashMap<>();

    for (int i = 0; i < nums.length; i++) {
        if(map.containsKey(target-nums[i])){
            return new int[]{map.get(target-nums[i]),i};
        }

        map.put(nums[i],i);
    }
    return null;
}

The above time complexity directly becomes o (n). What kind of thinking. It is to use hash table to match the elements in the array with its subscript as kV pair. When looping, put them in the HashMap in turn. Then judge whether there is a key such as “target value minus current key value” in the map, and return if there is one.

What do you mean? For example, if a key I saved in the map is “2”, I just need to find a key that is (13 – 2 = 11), which just means their sum is 13. These two numbers are the numbers I am looking for. Just return their subscripts.

This idea is really amazing. I exclaimed in my heart. It’s really a Sao operation. Only you can’t imagine it. No God can’t do it.

Let’s take a look at the procedure steps in this way. (take and as 13 for example)

Array: [2, 7, 11, 15], target = 13

step1:
key = 2,value = 0,
13 - 2 = 11. If the key does not exist, put (2,0) in the map

step2:
key = 7,value = 1,
13 - 7 = 6. If the key does not exist, put (7,1) in the map

step3:
key = 11, value = 2,
13 - 11 = 2. You can see that the key 2 already exists in the map.
Therefore, the subscripts 0 of 2 and 2 of 11 are returned.

Isn’t it amazing? First of all, this code looks much higher than the double-layer loop above. Secondly, the time complexity has changed from O (n ^ 2) to o (n).

I seem to open a magic door of algorithm. So, by the way, I also did one question every day, and got 10 points, which is beautiful and Zizi.

On the first day, I hope I can stick to it!

It’s easy to start one thing, but it’s hard to stick to one thing. Hold on, you will surpass a lot of people!

If this article is useful to you, please like, comment and forward.

Learning is boring and interesting. I am “the rain and the sky”. Welcome to follow me. I can receive the article push as soon as possible.