minimum-genetic-mutation

來源:互聯網
上載者:User

標籤:bfs   lis   ext   div   under   分類   tco   初始化   pre   

題目還是很好的,提供了一種新的思路方向。

細節方面,開始我的判斷條件用的dict,不太好,後來debug好了。

另外,注意其中用數組初始化Set的方法:Set<String> dict = new HashSet(Arrays.asList(bank));

還有 Set<String> a = new Hash<>(); 當中前面<>裡面加String的原因是提取出來的直接就是String,後面<>指的是接受各種類型。不加<>表示不區分類型,應該和<>是等價的效果。

https://leetcode.com/problems/minimum-genetic-mutation/// 這個方法特別好// https://discuss.leetcode.com/topic/63959/c-two-end-bfs-solution-exactly-same-as-127-word-ladder// 提到與 https://leetcode.com/problems/word-ladder/ 127. Word Ladder 題目其實是一模一樣的// 看了一下,的確是的public class Solution {    // 這個題目裡面用到了兩面BFS的方法,然後通過對字串的替換,來降低優先順序    // 之前另一個題目 Word Ladder裡面,用的是一端到另一端的    // 現在這個更巧妙一些,因為從set裡面找結果,比遍曆set的內容要更高效一些    public int minMutation(String start, String end, String[] bank) {        Set<String> dict = new HashSet(Arrays.asList(bank));        if (!dict.contains(end)) {            return -1;        }        Set<String> lessSet = new HashSet();        Set<String> moreSet = new HashSet();        char[] chList = {‘A‘, ‘C‘, ‘G‘, ‘T‘};        lessSet.add(start);        moreSet.add(end);        int step = 0;        while (true) {            // wrong: if dict is empty, no need to check more            // right: check lessSet            if (lessSet.isEmpty()) {                return -1;            }            if (moreSet.size() < lessSet.size()) {                Set<String> tmp = lessSet;                lessSet = moreSet;                moreSet = tmp;            }            //printSet(lessSet, moreSet, dict);            step++;            Iterator<String> iter = lessSet.iterator();            Set<String> newSet = new HashSet();            while(iter.hasNext()) {                String str = iter.next();                for (int i=0; i<str.length(); i++) {                    StringBuilder sb =new StringBuilder(str);                    for (int j=0; j<4; j++) {                        if (str.charAt(i) == chList[j]) {                            continue;                        }                        sb.setCharAt(i, chList[j]);                        if (moreSet.contains(sb.toString())) {                            return step;                        }                        if (dict.contains(sb.toString())) {                            dict.remove(sb.toString());                            newSet.add(sb.toString());                        }                    }                }            }            lessSet = newSet;        }    }    private void printSet(Set<String> lessSet, Set<String> moreSet, Set<String>dict) {        System.out.printf("Here is lessSet:");        Iterator<String> iter = lessSet.iterator();        while (iter.hasNext()) {            System.out.printf("%s,", iter.next());        }        System.out.println();        System.out.printf("Here is moreSet:");        iter = moreSet.iterator();        while (iter.hasNext()) {            System.out.printf("%s,", iter.next());        }        System.out.println();        System.out.printf("Here is dict:");        iter = dict.iterator();        while (iter.hasNext()) {            System.out.printf("%s,", iter.next());        }        System.out.println();        System.out.println("################");    }}

 

minimum-genetic-mutation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.