【LeetCode 127】Word Ladder

题目描述:

给两个单词和一个单词列表,每次转换只能改变一个字母,且只能变为在单词列表里的单词。问最少变换多少次,beginword能变为endword。

思路:

bfs。对单词的每个字母尝试26种变换,如果该单词在列表里,加入队列。
一直超时,把vector改为set以后,对于每次出现过的单词都直接删除。可以节省查找时间,然后就过了。

代码:

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        auto it = find(wordList.begin(), wordList.end(), endWord);
        if (it == wordList.end()) return 0;
        set<string> st(wordList.begin(), wordList.end());
        return bfs(beginWord, endWord, st);
    }

private:
    int bfs(string st, string ed, set<string>& words) {
        queue<string> que;
        que.push(st);
        int step = 1;
        
        while(!que.empty()) {
            int size = que.size();
            while(size--) {
                string cur = que.front();
                que.pop();
                
                for (int i=0; i<cur.length(); ++i) {
                    string ts = cur;
                    for (int j=0; j<26; ++j) {
                        char tmp = j + 'a';
                        if (tmp == cur[i]) continue;
                        ts[i] = tmp;
                        if (!words.count(ts)) continue; 
                        if (ts == ed) return step+1;
                        words.erase(ts);
                        que.push(ts);
                    }
                }
            }
            step++;
        }
        return 0;
    }
};

这周将是bfs周。
其实想做用队列和栈实现bfs和dfs的题。
#############################################
第二次写。常规想法 n*n,也能过。
又重新写了上边的实现。
有点恶心。

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        wordList.insert(wordList.begin(), beginWord);
        n = wordList.size();
        vis = vector<int> (n, 0);
        auto it = find(wordList.begin(), wordList.end(), endWord);
        int end;
        if (it == wordList.end()) return 0;
        else end = it - wordList.begin();
        return bfs(0, end, wordList);
    }
    
private:
    vector<int> vis;
    int n;
    
    int bfs(int st, int ed, vector<string>& words) {
        queue<int> que;
        que.push(st);
        int step = 1;
        while(!que.empty()) {
            int size = que.size();
            while(size--) {
                int cur = que.front();
                que.pop();
                for (int i=1; i<words.size(); ++i) {
                    if (vis[i]) continue;
                    if (dis(i, cur, words) != 1) continue;
                    if (i == ed) return step+1;
                    vis[i] = 1;
                    que.push(i);
                }
            }
            step++;
        }
        return 0;
    }
    
    int dis(int a, int b, vector<string>& words) {
        int len = words[a].length();
        int res = 0;
        for (int i=0; i<len; ++i) {
            if (words[a][i] != words[b][i]) res++;
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值