Minimum-genetic-mutation

Source: Internet
Author: User

The topic is still very good, providing a new direction of thinking.

Details, the beginning of my judgment conditions with the dict, not very good, then debug good.

In addition, note the method of initializing Set with an array:set<string> dict = new HashSet (arrays.aslist (bank));

and set<string> a = new hash<> (); The reason that the front <> inside of the string is extracted is directly a string, followed by <> refers to the acceptance of various types. The non-additive <> indicates that the type is not differentiated and should be equivalent to <>.

Https//leetcode.com/problems/minimum-genetic-mutation///This method is particularly good// Https://discuss.leetcode.com/topic/63959/c-two-end-bfs-solution-exactly-same-as-127-word-ladder//mentioned withhttps://leetcode.com/problems/word-ladder/127 Word Ladder The exact same question.//Look, yes, I did. Public classSolution {//This topic uses both sides of the BFs method, and then through the substitution of strings, to reduce the priority//before another topic Word ladder inside, with one end to the other end of the//Now this is a bit more subtle, because finding results from a set is more efficient than traversing the contents of a set.     Public intminmutation (string start, String end, string[] bank) {Set<String> dict =NewHashSet (arrays.aslist (bank)); if(!dict.contains (end)) {            return-1; } Set<String> Lessset =NewHashSet (); Set<String> Moreset =NewHashSet (); Char[] chlist = {' A ', ' C ', ' G ', ' T '};        Lessset.add (start);        Moreset.add (end); intStep = 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 =NewHashSet ();  while(Iter.hasnext ()) {String str=Iter.next ();  for(inti=0; I<str.length (); i++) {StringBuilder sb=NewStringBuilder (str);  for(intj=0; j<4; J + +) {                        if(Str.charat (i) = =Chlist[j]) {                            Continue;                        } sb.setcharat (I, chlist[j]); if(Moreset.contains (sb.tostring ())) {returnstep; }                        if(Dict.contains (sb.tostring ())) {Dict.remove (sb.tostring ());                        Newset.add (Sb.tostring ()); } }}} Lessset=Newset; }    }    Private voidPrintset (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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.