Leetcode-repeated DNA

Source: Internet
Author: User

About bit operators. such as <<, value << num, where num specifies the number of digits to shift the value to move, each left one bit, the higher order bits are moved out (thrown directly), and the right is filled with 0.

The truth is very simple ah, my mother will be a good high school. But I do not know how to use AH, coding level too slag, do too few problems, never wood useful. Σ (° °| | |) ︴

For example, in the Leetcode encountered a repeat DNA topic, just beginning very naturally think with substring () method, helpless everyone's DNA sequence thousands, resulting in time, space demand too big, not meet the requirements. So, Baidu, the big God door said to use the method of hash mapping. Up Posture Ah!!

As we all know, in the computer, the time of the symbol 01 string shift is always much lower than the time complexity of the string comparison, and in the DNA sequence, only the a,c,g,t four symbols appear, so you can consider the string corresponding to 01 strings, so just "00", "01", "10", "11" Can represent the above four characters, 10 characters of the comparison with a 32-bit integer can be expressed!! This compares the 10-bit string into an int integer!! Magic

However, how do different string representations correspond to integers? Some of the great Gods of the door described in a way called "dictionary", in fact, in my opinion, is nothing more than a hash and displacement, specifically described as follows:

First, put four characters ' a-0,c-1,g-2,t-3 ' in a hashmap, so that the representation in the computer is 01 strings of 2 bits.

1 New Hashmap<character, integer>(); 2 co.put (' A ', 0); 3 co.put (' B ', 1); 4 co.put (' C ', 2); 5 c0.put (' D ', 3);


Second, how does a 10-bit string correspond to a 32-bit integer? At the beginning of the best solution, in fact, it is very simple, with a for loop can be. (PS: To show it myself again, or foggy), here, first of all 10 to correspond to an integer:

1 Integer key = 0; 2  for (int i=0; i<10; i++) {3  key = (Key << 2) + Co.get (S.charat (i)); 4 }// calculate the For loop, the resulting value is 341, exactly corresponds to 00^000101010101, that is, the first 10 characters aaaaaccccc! 


Third, understand this principle, the following steps are very simple. Then use a HashMap to record 10 consecutive strings that appear only once, and if the current string appears in the map, it is repeat and placed in the final result table list.
The other problem is that when you move a char backwards, the calculation is like the calculation formula in the for loop above.

All right, ^_^, go home, break up the night.

Leetcode-repeated DNA

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.