博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Word Ladder
阅读量:4151 次
发布时间:2019-05-25

本文共 854 字,大约阅读时间需要 2 分钟。

class Solution {public:	int ladderLength(string start, string end, unordered_set
&dict) { // Start typing your C/C++ solution below // DO NOT write int main() function //BFS(because all edge is 1) to find the minimum path //O(n*len*26) //shortest path O(n^2) will be TLE queue
> q; unordered_set
visited; q.push(make_pair(start, 1)); visited.insert(start); while (!q.empty()) { string curStr = q.front().first; int curStep = q.front().second; q.pop(); for (int i = 0; i < curStr.size(); ++i) { string tmp = curStr; for (int j = 0; j < 26; ++j) { tmp[i] = j+'a'; if(tmp == end) return curStep+1; if(visited.find(tmp) == visited.end() && dict.find(tmp) != dict.end()) { q.push(make_pair(tmp, curStep+1)); visited.insert(tmp); } } } } return 0; }};

转载地址:http://ohxti.baihongyu.com/

你可能感兴趣的文章
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
一篇搞懂Java反射机制
查看>>
Single Number II --出现一次的数(重)
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
Mysql中下划线问题
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>