当前位置:网站首页>Sword finger offer II 109. unlock the password lock
Sword finger offer II 109. unlock the password lock
2022-07-28 19:27:00 【INGNIGHT】

link : The finger of the sword Offer II 109. Unlock the code lock
Answer key :
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
std::unordered_set<std::string> table(deadends.begin(), deadends.end());
// Exception oriented design
if (table.find("0000") != table.end()) {
return -1;
}
// Exception oriented design
if (target == "0000") {
return 0;
}
std::queue<std::pair<std::string, int>> que;
que.push(make_pair("0000", 0));
std::unordered_set<std::string> visited;
visited.insert("0000");
while (!que.empty()) {
auto f = que.front();
que.pop();
for (auto& neighboard : get_neighboards(f.first)) {
if (visited.find(neighboard) != visited.end() || table.find(neighboard) != table.end()) {
continue;
}
if (neighboard == target) {
return f.second+1;
}
que.push(std::make_pair(neighboard, f.second+1));
visited.insert(neighboard);
}
}
return -1;
}
private:
std::vector<std::string> get_neighboards(std::string& cur) {
auto next = [](char a)->char {
return a == '9' ? '0' : a+1;
};
auto prev = [](char a)->char {
return a == '0' ? '9' : a-1;
};
std::vector<std::string> result;
for (int i = 0; i < cur.size(); ++i) {
char c = cur[i];
cur[i] = next(c);
result.push_back(cur);
cur[i] = prev(c);
result.push_back(cur);
cur[i] = c;
}
return result;
}
};边栏推荐
- SaltStack系统初始化
- Solve the critical path in FJSP - with Matlab source code
- Powerbi time series analysis, prediction and visualization tutorial
- ES6's new data container map
- Qt: 一个SIGNAL绑定多个SLOT
- ardupilot软件在环仿真与在线调试
- Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
- ES6 conversion of new data type set and arr set map
- RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
- 顺序线性表——课上练
猜你喜欢

Bm11 list addition (II)

RTC clock: a calendar

Application of time series database in museum environment detection

VAE: understanding and implementation of variational self encoder

GPIO port configuration of K60

SQL audit tool self introduction owls

Learn from Li Mu in depth -softmax return

SaltStack进阶

Tikz draw Gantt chart in FJSP -trans necessary

关于ASM冗余问题
随机推荐
The ever-changing pointer ----- C language
用于异常检测的Transformer - InTra《Inpainting Transformer for Anomaly Detection》
Server body 21: pre compilation processing by different compilers (a brief introduction to MSVC and GCC)
C语言循环语句强化练习题
sudo rosdep init 出现 ERROR: cannot download default
Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology
RFs self study notes (4): actual measurement model - the mixture of OK and CK, and then calculate the likelihood probability
Wechat official account custom sharing and updateappmessagesharedata are all successful. Why is it a link that is shared?
SQL audit tool self introduction owls
Jestson nano Object detection
Libgdx learning path 01: libgdx introduction and running tutorial
[image segmentation] vein segmentation based on directional valley detection with matlab code
Using CPLEX to solve small-scale energy-efficient FJSP
RFs self study notes (III): clutter model - first determine the number with Poisson distribution, and then use uniform distribution as probability distribution
VAE: understanding and implementation of variational self encoder
Validate hardware DDR design with Xilinx MIG
SaltStack之数据系统
Swing事件处理的过程是怎样的?
About ASM redundancy
R language text mining and natural language processing tutorial