Home > Back-end >  Java permutation and combination
Java permutation and combination

Time:11-19

Example: 1, 2, 3, a, b,
After the permutation and combination, 1 a, 1 b, 2 a, 2 b, 3 a, 3 b,
How to set up, only 1 and a combination,
The results as follows: 1 a, 2 a, 2 b, 3 a, 3 b,
Have to understand the great spirit of trouble can provide a train of thought or code post is too good, thank you very much,

CodePudding user response:

Similar to this?
Public List Strs1 longestCommonPrefix (String [], String [] strs2, int a, int b) {
List Result=new LinkedList<> (a);
If (strs1. Length==0 | | strs2. Length==0) {
Return the result.
}
for (int i=0; I{if (I==a)
Result. The add (strs1 strs2 [I] + [b]);
} else {
For (int j=0; jResult. The add (strs1 strs2 [I] + [j]);
}
}
}
return result;
}

CodePudding user response:

reference 1/f, xixi water bank reply:
like this?
Public List Strs1 longestCommonPrefix (String [], String [] strs2, int a, int b) {
List Result=new LinkedList<> (a);
If (strs1. Length==0 | | strs2. Length==0) {
Return the result.
}
for (int i=0; I{if (I==a)
Result. The add (strs1 strs2 [I] + [b]);
} else {
For (int j=0; jResult. The add (strs1 strs2 [I] + [j]);
}
}
}
return result;
}

Hello, do you have a complete code? Can you give me to send it to me?

CodePudding user response:

 class Solution {
2 1,//parameter strs1 array strs2 array, array specified position, 1 b and the value of the combination of specified values in the array 1 in 2 array subscript
Public List Strs1 longestCommonPrefix (String [], String [] strs2, int a, int b) {
List Result=new LinkedList<> (a);
If (strs1. Length==0 | | strs2. Length==0) {//free judgment array directly returns an empty collection
Return the result.
}
for (int i=0; IIf (I==a) {//judgment array 1 specified values just two specified values in combination with an array of
Result. The add (strs1 strs2 [I] + [b]);
} else {//1 2 values in the array and the rest of the values in the array are combined
For (int j=0; jResult. The add (strs1 strs2 [I] + [j]);
}
}
}
return result;
}

Public static void main (String [] args) {
Solution Solution=new Solution ();
Aaa=new String String [] [] {" 1 ", "2", "3", "4"};//array 1
BBB=new String String [] [] {" a ", "b", "c", "d"};//array 2
List the STR=solution. LongestCommonPrefix (aaa, BBB, 0, 0);
System.out.println(str);
}
}

CodePudding user response:

In fact you can also write better code, the take out 1, directly to the other elements in the two set, then the cartesian product
  • Related