LC 752 Open the Lock (unfinished)

Due to this problem involves a lot of knowledge, such as data structures inside the hash table, c ++ iterators, therefore, we need to break one by one for every doubt.

Problem Description

 

 

Answers

 1 class Solution {
 2 public:
 3     int openLock(vector<string>& deadends, string target) {
 4         unordered_set<string> deadlock(deadends.begin(), deadends.end());
 5         if (deadlock.count("0000")) return -1;
 6         int res = 0;
 7         unordered_set<string> visited{{"0000"}};
 8         queue<string> q{{"0000"}};
 9         while (!q.empty()) {
10             ++res;
11             for (int k = q.size(); k > 0; --k) {
12                 auto t = q.front(); q.pop();
13                 for (int i = 0; i < t.size(); ++i) {
14                     for (int j = -1; j <= 1; ++j) {
15                         if (j == 0) continue;
16                         string str = t;
17                         str[i] = ((t[i] - '0') + 10 + j) % 10 + '0';
18                         if (str == target) return res;
19                         if (!visited.count(str) && !deadlock.count(str)) q.push(str);        
20                         visited.insert(str);
21                     }
22                 }
23             }
24         }
25         return -1;
26     }
27 };

 

Additional knowledge

Iterator

As we know, the program contains a large number of iterations, these iterations are carried out in a loop, such as for.

Iteration (the Iteration) : is a kind of behavior , for a particular vessel, traverse behavior.

Iterables (the Iterable) : is a container , such as set, list, vector, etc., mounted on the inside a pile of data, and this container may be iterative operations.

What is the iterator (iterator) it?

Iterator is like a can for device the object of the operation, which contains the data, and a button may operate (e.g., c ++ in the begin, end, etc. [1] ), and we can easily use and operation packaged data.

There is another problem, such as the following example [2]:

std::vector<int> v;
for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
    // do someting with iter
}

v can understand, it is a vector. vector <int> iterator type can also understand, but what is v.begin () ?

Dictionary [1] is the iterator starts its return. what the fxxk? What is the beginning of an iterator? Is not the iterator can manipulate objects it? I checked a lot of Chinese website and blog, it does not seem very satisfactory explanation. Later found its English interpretation: the begin () function IS AN Used to return Iterator pointing to at The First Element of the Vector at The Container.

 

This clearly better, v.begin () is an iterator particular, it is an iterator to the first element of the container .

Note also that v.end (), which is also an iterator, but it is an iterator to the next position of the last element of the container.

Examples of the use of [3] :

#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // declaration of vector container 
    vector<int> myvector{ 1, 2, 3, 4, 5 }; 
  
    // using end() to print vector 
    for (auto it = myvector.begin() ; it != myvector.end(); ++it) 
        cout << ' ' << *it; 
    return 0; 
} 

 

The output is:

1 2 3 4 5

In addition, do not bother to consider the type of iteator, you can directly use auto

 

insert & find

The benefits of using iterators, is that you can use to provide a bunch of good function, this example can be found in insert and find.
As shown in the answer mode, may be detailed with reference to: http://c.biancheng.net/view/570.html
 
 

 

[1]: http://c.biancheng.net/view/409.html

[2]:https://qinglinmao8315.github.io/c++/2018/03/07/how-std-vector-end-work.html

[3]: https://www.geeksforgeeks.org/vectorbegin-vectorend-c-stl/

Guess you like

Origin www.cnblogs.com/kykai/p/11421708.html