- How to convert an int to a string?
string s = to_string(x)
- How to check if a
key
exists inunordered_map<> m
?- `if (unordered_map.count(key) == 0)' then key doesn't exist
unordered_map.count(key)
can return at most 1 due to it is a dictionary.
- How to sort a container, say
string
?sort(s.begin(), s.end())
- What to do first when solving linked list problems?
- Use a dummy node. At the end, return
dummy->next
- Use a dummy node. At the end, return
- Given an unsorted array, how to remove duplicates and sort?
- A. insert each element into a set, since set is ordered.
for (int &num : arr) s.insert(num);
. The reason we use&
is because we do not want to make a copy ofnum
inarr
. But be careful, if we use reference, any changes tonum
will be reflected inarr
.
- B.
sort(array)
first, thenarray.erase()
to remove duplicates- Before doing the above, we should always create a copy of the data:
std::vector<int> sortedUniqueNumbers = arr;
- Sort first, due to the behavior of erase
std::sort(sortedUniqueNumbers.begin(), sortedUniqueNumbers.end());
- Then erase:
sortedUniqueNumbers.erase(std::unique(sortedUniqueNumbers.begin(), sortedUniqueNumbers.end()), sortedUniqueNumbers.end());
vector.erase(v.begin(), v.end())
: this function takes in two parameters that are iterators.
std::unique(vector)
: this will remove all the extra consecutive numbers in the vector.- e.g.
{ 1, 1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8 }
->{ 1, 3, 10, 1, 3, 7, 8 }
- This is why we need to sort the vector first, then call
unique()
function
- e.g.
- Return value of
std::unique(vector)
: an iterator pointing to the end of the de-duplicated array, and we treat that iterator as the begin() of the index that we want to erase.
- Before doing the above, we should always create a copy of the data:
- A. insert each element into a set, since set is ordered.
- How to obtain a substring of a
string s
?- Use
s.substr(start_idx, length)
.
- Use