java - More efficient way to find all combinations? -
Say that you have Some sample code: The time for performance is badly damaged because the size of the original Is there a more efficient way to do this? You Edit: list 's
strings or whatever , And you want to create another
list which will include every possible combination of two
strings from the original
list (together Presented), is there any more efficient way to combine the string with all others to do this besides using the nested
for loop?
for
(string s: bytes) {for (string a: bytes) {if (! (Bytes. Index (a) == Bytes .indexOf (s)) {if (s.concat (a) .length () == targetLength) {string combination = s.concat (a); ValidSolutions.add (combination); }}}}
list of
strings increases.
I! By setting J = j condition
j = i + 1 . Also,
bytes. Things like length () are evaluated on each iteration of both ends - save it to a value and reuse. Calling inside the loop
a.length () often asks for the length of the same string - you can also save some runtime on it. These are the updates:
< Code> int len = bytes.length (); Int aLength; String A, B; {A = bytes [i] for (int i = 0; i & lt; len; i ++); AL & D = Aline (); For (int j = i; j & lt; lane; j ++) {b = bytes [ja]; If (b long () + alndt == target lang) {legitimate solutions.ed (b. Conct (a)); ValidSolutions.add (a.concat (b)); }}}
j = i Because you want to consider combining the string with yourself; In addition, you will need to add
a.concat (b) since this combination is never considered in the loop, but it is a valid string
Comments
Post a Comment