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

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

class Solution {	//Split the problem in three steps:	//	//1.build the adjacency list	//2.do a BFS to get a vector
> prev array. For example, prev[1] = [2, 3, 0] means we can go from (2 to 1) or (3 to 1) or (0 to 1). //3.construct the paths using prev. //A simple implement of the first step is O(n^2) and cannot pass the large test. So I use an O(nL26) method, where L is the length of each word. The idea is to try changing each letter in a word from 'a' to 'z', if that results to a valid word, we add the changed word to a adjacency list of the origin word. // //Preforming a BFS is O(n) since we already has the adjacency list. vector
vdict; vector
> adj; void build(unordered_set
&dict) { int i, j; vdict.clear(); unordered_map
ids; for(auto it=dict.begin(); it != dict.end(); it++) { ids[*it] = vdict.size(); vdict.push_back(*it); } adj.clear(); adj.resize(vdict.size()); for(int i = 0; i < vdict.size(); i++) { string w = vdict[i]; for(int j = 0; j < vdict[i].size(); j++) { for(char c = 'a'; c <= 'z'; c++) if(c != vdict[i][j]) { w[j] = c; if(ids.count(w)) { adj[i].push_back(ids[w]); } w[j] = vdict[i][j]; } } } } void gen(int v1, int v2, vector
>& prev, vector
& path, vector
>&ans) { path.push_back(v2); if(v2 == v1 && path.size() > 1) { ans.push_back(vector
()); vector
::reverse_iterator rit; for(rit = path.rbegin(); rit != path.rend(); rit++) ans.back().push_back(vdict[*rit]); } else { int i; for(i = 0; i < prev[v2].size(); i++) gen(v1, prev[v2][i], prev, path, ans); } path.pop_back(); }public: vector
> findLadders(string start, string end, unordered_set
&dict) { // Start typing your C/C++ solution below // DO NOT write int main() function dict.insert(start); dict.insert(end); build(dict); queue
que; vector
> prev(vdict.size()); vector
distance(vdict.size()); int v, v1, v2; for(v1 = 0; vdict[v1] != start; v1++); for(v2 = 0; vdict[v2] != end; v2++); for(int i = 0; i < adj[v1].size(); i++) { v = adj[v1][i]; que.push(v); prev[v].push_back(v1); distance[v] = 1; } while(!que.empty()){ int v1 = que.front(); que.pop(); if(v1 == v2) break; int d = distance[v1] + 1; for(int i = 0; i < adj[v1].size(); i++) { v = adj[v1][i]; if(prev[v].size() == 0) { prev[v].clear(); prev[v].push_back(v1); distance[v] = d; que.push(v); } else if(distance[v] == d) { prev[v].push_back(v1); } } } vector
> ans; vector
path; gen(v1, v2, prev, path, ans); return ans; }};

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

你可能感兴趣的文章
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Simple Guide to use the gdb tool in Android environment
查看>>
Netconsole to capture the log
查看>>
Build GingerBread on 32 bit machine.
查看>>
How to make SD Card world wide writable
查看>>
Detecting Memory Leaks in Kernel
查看>>
Linux initial RAM disk (initrd) overview
查看>>
Timestamping Linux kernel printk output in dmesg for fun and profit
查看>>
There's Much More than Intel/AMD Inside
查看>>
CentOS7 安装MySQL 5.6.43
查看>>
使用Java 导入/导出 Excel ----Jakarta POI
查看>>
本地tomcat 服务器内存不足
查看>>
IntelliJ IDAE 2018.2 汉化
查看>>
基于S5PV210的uboot移植中遇到的若干问题记录(一)DM9000网卡移植
查看>>
Openwrt源码下载与编译
查看>>
我和ip_conntrack不得不说的一些事
查看>>
Linux 查看端口使用情况
查看>>
文件隐藏
查看>>
两个linux内核rootkit--之二:adore-ng
查看>>
两个linux内核rootkit--之一:enyelkm
查看>>