当前位置:网站首页>剑指 Offer II 109. 开密码锁
剑指 Offer II 109. 开密码锁
2022-07-28 17:22:00 【INGNIGHT】

题解:
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
std::unordered_set<std::string> table(deadends.begin(), deadends.end());
// 面向异常设计
if (table.find("0000") != table.end()) {
return -1;
}
// 面向异常设计
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;
}
};边栏推荐
- PyG搭建异质图注意力网络HAN实现DBLP节点预测
- What is the future of software testing? How to learn?
- What does real HTAP mean to users and developers?
- Learn from Li Mu in depth -softmax return
- 搜索问题与技术
- Can the training software test be employed
- How long does software testing take?
- After several twists and turns, how long can the TSDB C-bit of influxdb last?
- RTC clock: a calendar
- [data analysis] realize SVDD decision boundary visualization based on MATLAB
猜你喜欢

Pytorch GPU yolov5 reports an error

【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码

From Bayesian filter to Kalman filter (2)

After several twists and turns, how long can the TSDB C-bit of influxdb last?

Libgdx learning road 02: draw game map with tiled

VIM learning manual

What if svchost.exe of win11 system has been downloading?

使用Xilinx MIG验证硬件DDR设计

vim学习手册

【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
随机推荐
DevCon. Exe export output to the specified file
Fundamentals of software testing and development | practical development of several tools in testing and development
Is there any prospect and way out for software testing?
The login interface of modern personal blog system modstartblog v5.4.0 has been revised and the contact information has been added
ECS 5 workflow
Tikz draw Gantt chart in FJSP -trans necessary
Random finite set RFs self-study notes (6): an example of calculation with the formula of prediction step and update step
一家芯片公司倒在了B轮
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
Is the prospect of software testing dead? Has it entered the cold winter?
Mongodb database shell command execution
UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application
From Bayesian filter to Kalman filter (2)
6-20漏洞利用-proftpd测试
Application of time series database in Hydropower Station
Getting started with QT & OpenGL
Mongodb database replication table
ACM warm-up exercise 3 in 2022 summer vacation (detailed)
PyG搭建异质图注意力网络HAN实现DBLP节点预测
How to solve the problem that the win11 computer camera cannot be seen when it is turned on and the display screen is black?