adding leetcode problems

This commit is contained in:
Jidong Xiao
2023-10-20 10:04:44 -04:00
parent 627f2cfa1e
commit aeccc37209

View File

@@ -12,7 +12,7 @@ Associative Containers (STL Maps)
## 15.1 STL Maps: Associative Containers ## 15.1 STL Maps: Associative Containers
- STL maps store pairs of “associated” values. - STL maps store pairs of “associated” values.
We will see several examples today, in Lab 9, and in Lecture 15: We will see several examples today, in Lab 9, and in Lecture 16:
- An association between a string, representing a word, and an int representing the number of times that - An association between a string, representing a word, and an int representing the number of times that
word has been seen in an input file. word has been seen in an input file.
- An association between a string, representing a word, and a vector that stores the line numbers from a - An association between a string, representing a word, and a vector that stores the line numbers from a
@@ -31,7 +31,7 @@ std::pair<const key_type, value_type>
data structures are not the same. The organization, implementation, and performance is different. In a couple data structures are not the same. The organization, implementation, and performance is different. In a couple
weeks well see an STL data structure that is even more similar to the Python dictionary. weeks well see an STL data structure that is even more similar to the Python dictionary.
- Map search, insert and erase are O(log n). - Map search, insert and erase are O(log n).
First, lets see how this some of this works with a program to count the occurrences of each word in a file. Well look First, lets see how some of this works with a program to count the occurrences of each word in a file. Well look
at more details and more examples later. at more details and more examples later.
## 15.2 Counting Word Occurrences ## 15.2 Counting Word Occurrences
@@ -46,8 +46,9 @@ int main() {
std::string s; std::string s;
std::map<std::string, int> counters; // store each word and an associated counter std::map<std::string, int> counters; // store each word and an associated counter
// read the input, keeping track of each word and how often we see it // read the input, keeping track of each word and how often we see it
while (std::cin >> s) while (std::cin >> s){
++counters[s]; ++counters[s];
}
// write the words and associated counts // write the words and associated counts
std::map<std::string, int>::const_iterator it; std::map<std::string, int>::const_iterator it;
for (it = counters.begin(); it != counters.end(); ++it) { for (it = counters.begin(); it != counters.end(); ++it) {
@@ -153,3 +154,9 @@ added pair in the map and the bool value true.
up to, but not including, last. up to, but not including, last.
- size_type erase(const key_type& k) — erase the pair containing key k, returning either 0 or 1, depending - size_type erase(const key_type& k) — erase the pair containing key k, returning either 0 or 1, depending
on whether or not the key was in a pair in the map. on whether or not the key was in a pair in the map.
## 15.10 Leetcode Exercises
- [Leetcode problem 2: Two Sum](https://leetcode.com/problems/two-sum/). Solution: [p2_twosum.cpp](../../leetcode/p2_twosum.cpp).
- [Leetcode problem 290: Word Pattern](https://leetcode.com/problems/word-pattern/). Solution: [p290_word_pattern.cpp](../../leetcode/p290_word_pattern.cpp).