Interview preparation-array and string

Source: Internet
Author: User

1. Hash

There are a lot of things about hashmap. I found the STL source code to check whether it was actually a cainiao. Let's look at java. It's better to see this.

Bytes

Why do we look at hashmap instead of hashset? Because hashset is generated using hashmap, It is effort-saving.

Why hash?

Array search is fast, index processing is done directly, adding and deleting are too slow, and large pieces of content are moved. Addition and deletion of linked lists are fast, and searching is too slow, and next is performed one by one. Therefore, the combination of the two becomes the simplest hash. Of course, this is also true for Java hashmap.

For more information about how to add a hash, refer to encrypt.

If this position has one element, do you need to move the element like an array? If you don't need the source code, just put an empty linked list and add it next time. It's a bit of space for time change. Actually, it's like a bucket. You can set a fixed number of buckets, then we put it in. The only difference is that the hashmap bucket may need to be resize ();

Both of these are vivid explanations of the hashmap source code.

Why hash? What about Frequently Asked Questions? Reference http://blog.csdn.net/v_JULY_v/article/details/6256463

In addition to 8-12, how does MAP ensure that the key is unique? The hashmap cannot be found, and the map looks like there is

   public abstract Set keySet();

Save the key value and use the uniqueness of the set. How can the set be guaranteed? It is too difficult to find a hashset. It is ensured by the unique key of hashmap. Treeset also uses treemap. Copyonwritearrayset is not so pitfall, But it seems troublesome to use a list to store and write the equals Method for every traversal. According to the query results, the methods for implementing different elements in a set vary greatly. There are roughly three methods: equals, hashcode, and compareto. If hash is used, as long as there is no hash conflict, it is certainly the most efficient. It seems that this problem has been solved.

2. String

1) The design algorithm determines that the characters in the string are unique. What if the extra data structure is not applicable?

In normal cases (except for encoding), there are less than 256 characters. You can use hash to create an int array of up to 256 characters, traverse the characters in the string, and obtain the ASCII code, map to the array. If it is 0, add 1. If it is already 1, you will know that there are duplicates. In fact, it is only a test of 0 and 1, and you can even use bit to do it, if test (I) is true, the same element is returned.

2) implement common C-style string Inversion

void reverse(char *str){char *end =  str;while (*end){end++;}end--;while (str < end){char temp = *str;*str++ = *end;*end-- = temp;}}

3) design an algorithm to remove repeated characters from the string without any additional buffering. Test Cases for Algorithm Design

If extra buffering is used, the simplest method is to apply for a set, scan it directly, and add it. Now the project is doing this, but the efficiency is not necessarily high (how can a set ensure uniqueness? How can we ensure the uniqueness of the key in the map by using the key uniqueness of the map? Looking back)

No extra buffer is used. It is a little troublesome. You need to scan each element to see if they are the same. If yes, you can skip them. If not, you can save them. SRC and des are in the same memory zone, fortunately, it is high to low storage, or it will be troublesome. the time complexity is O (n ^ 2), which is similar to that of bubble storage (I feel that I can't just talk about the idea, writing code at a time is actually quite difficult and requires practice)

The solution given by cracking is to scan existing data for comparison, and the efficiency should be similar. The Stop Condition always exists in the same way. The difference is that in the result, the first is to save the last one with the same character, and the second is to save the first encounter with the same character.

4) Write a function to determine whether two strings contain the same characters.

Note that if a string is only a letter, you can use an array of 52 to map it to the past. For details, refer

int isEqualStr(string left, string right){sort(left.begin(),left.end());sort(right.begin(), right.end());return strcmp(left.c_str(), right.c_str());}

5) write the code to replace the space in the string with '% 20'
6) give an image, which is expressed as nxn. Each pixel is 4 bytes. Write a function to rotate the image 90 °.
7) implementation algorithm: In an mxn matrix, if an element is 0, the row and column are set to zero.
8) Suppose you already have a function for issubstring (S1, S2) to determine whether string S1 is a substring of string S2. Now, we will give you a string S1 and S2, so that you can determine whether S1 is obtained by S2 Cyclic Displacement. You can only call issubstring once in your algorithm (for example, you can get "erbottlewat" by "waterbottle ").

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.