banner
Lowerce

Lowerce

github
twitter

Some summaries of using C++ to write Leetcode algorithms.

This text is written based on the valid information and experience I obtained while using C++ to solve various Leetcode problems.

P.S. It may emphasize more on the use of C++ rather than on various advanced algorithm solutions.

map#

In general, map is a very useful thing. I still use map to solve many problems that can be solved using set, and it feels very comfortable.

Creation#

When creating, you need to specify the type map or unordered_map < type , type > variable_name, and it will be empty after creation. Let's take unordered_map<string,int> maps as an example.

Insertion and Deletion#

For counting applications (since I do a lot of problem-solving), which means the second type is int, it is very simple to use. Just use maps["hello"]++, which creates the corresponding key value and increments it by 1. It is worth noting that when the value of the key is reduced to 0, it does not mean that the key disappears. You need to use maps.erase("hello") to completely remove the corresponding key value.

For other applications, you need to use maps.insert(pair<string,string>("hello","world")) to insert.

Searching#

As a hash table, searching is the fundamental reason for its use.

To check if a key is in the table, use if(maps.find("hello")!=maps.end()). If it returns true, it means the key is in the table. The return value here is an iterator. To use the iterator, use maps.find("hello")->first, which is the value of the key (i.e., "hello"), and maps.find("hello")->second, which is the value corresponding to "hello".

To iterate through the map, you also need to use an iterator. First create an iterator, unordered_map<string,int>::iterator itr, and then use it in a for loop similar to a normal iteration. for(itr=maps.begin();itr!=maps.end();itr++) This way, you can iterate through it.

string#

There are various ways to manipulate strings, and many of them are almost equivalent.

assign#

The basic usage is s1.assign(s,index,num), which assigns the characters of the s string starting from the index to the s1 string for a specified number of characters.

It is similar to substr.

substr#

s1=s.substr(index,num), this operation is equivalent to using assign.

for#

I haven't used some tricks for automatic iteration in for loops before.

For example, for(auto i:nums) or for(auto &i:maps), the latter can be accessed using t.first and t.second. You can also use for(auto &[key,value]:maps) to directly access the key and value.

reverse#

This seems to be used to reverse many data structures supported by C++. It generally takes two input parameters and the usage is like this: reverse(starting position, starting position + number of elements to be reversed). From this perspective, its input parameters are in a left-closed right-open interval. For example, reverse(string.begin(),string.end())...

The first time I used it, I was a bit confused. It is highly likely that many standard C++ functions use a similar input format, so be careful.

resize#

It works well for strings. Just use resize(new size), and it can directly expand the string to the specified length. The excess part should be empty.

sort#

Use sort(begin(),end()), and it can directly sort in ascending order. The starting and ending points are the same as in reverse, so the input method is exactly the same. This confirms that the previous guess was correct 😄.

It seems that you can also use a custom function as the third input parameter to determine the sorting criteria. I will write about it later when I encounter it while solving problems. I won't write about things I haven't used before 😋.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.