Objective
It is said that an excellent program ape often have such experience, the day encountered a brain-racking problem can not be solved, night after sleep, in the middle of the dream will be inspired, immediately get up, open the computer, one go. The next day if you don't read the notes, you have no idea that you have found such ingenious solutions.
Lying in bed last night, accidentally thought of a problem, suddenly inspired by the idea of a clever solution, almost want to get up and write down, just the bedroom has been powered down, nothing. When I got up in the morning and didn't wash my face, I knocked the article first.
Problem
A very simple question, the length is 100 and 101 ab two arrays, the value range is 0-99, contains duplicates, find out to come out of a number.
Ideas
The first thought was to sort it out, and then a trip to find out the difference. Sort by the beginning of the natural thinking of the standard fast row, for numbers, there are cardinality and counting.
The thought of counting, suddenly found that there is no need to sort, three-time traversal can be solved.
- Iterate through a array, counting with the array count;
- Traverse B Array, count[b[i]]--;
- Traverse count, the value is 1 of the subscript is the request;
Extrapolate
Then the thought is divergent, further thinking
- Length of 100 and 100+n, find out the number of n;
- Find the same n, that is, a subset;
- For three arrays, a subset (subscript with a count array value of 3 is);
- For two arrays, find out the similarities and differences between the elements
Lift Three evils N
- For n arrays, ask for a subset (count is N)
- For n arrays, the similarities and differences of the elements are obtained
Lifting N anti-n^n
If the topic does not give a range, or is not a number, the amount of big data to do,
- An array of two elements, for example, to find one of the different elements
If the element value is unique, hash, the second traversal, the hash conflict is the request
Conclusion
。。。
The result was two or three O ' night in the morning before falling asleep, the conclusion is no longer think, divergent thinking is too terrible t^t.
Program Ape easy to brain over, night or good rest, do not think too much, of course, the dream of sudden inspiration or to immediately climb up to write down, after all, sometimes 1% of the inspiration is more important ...
[Pre-sleep inspiration and Divergent thinking] by a simple array comparison problem thought of