给定一个数字输入字符串,找出所有可以用相同顺序的数字组成的数字组合。 例如:
null
Input : 123 Output :1 2 3 1 23 12 3 123Input : 1234Output : 1 2 3 4 1 2 34 1 23 4 1 234 12 3 4 12 34 123 4 1234
这个问题可以用递归来解决。到目前为止,我们跟踪给定输入字符串中的当前索引和输出字符串的长度。在每次调用函数时,如果输入字符串中没有剩余数字,则打印当前输出字符串并返回。否则,将当前数字复制到输出。从这里打两个电话,一个认为下一个数字是下一个号码的一部分(包括输出字符串中的空格),另一个认为下一个数字是当前号码的一部分(不包括空格)。如果当前数字后没有剩余数字,则忽略对函数的第二次调用,因为尾随空格不算作新的组合。
C++
// CPP program to find all combination of numbers // from a given string of digits #include <iostream> #include <cstring> using namespace std; // function to print combinations of numbers // in given input string void printCombinations( char * input, int index, char * output, int outLength) { // no more digits left in input string if (input[index] == ' ' ) { // print output string & return output[outLength] = ' ' ; cout << output << endl; return ; } // place current digit in input string output[outLength] = input[index]; // separate next digit with a space output[outLength + 1] = ' ' ; printCombinations(input, index + 1, output, outLength + 2); // if next digit exists make a // call without including space if (input[index + 1] != ' ' ) printCombinations(input, index + 1, output, outLength + 1); } // driver function to test above function int main() { char input[] = "1214" ; char *output = new char [100]; // initialize output with empty string output[0] = ' ' ; printCombinations(input, 0, output, 0); return 0; } |
JAVA
// Java program to find all combinations // of numbers from a given string of digits class GFG { // function to print combinations of numbers // in given input string static void printCombinations( char [] input, int index, char [] output, int outLength) { // no more digits left in input string if (input.length == index) { // print output string & return System.out.println(String.valueOf(output)); return ; } // place current digit in input string output[outLength] = input[index]; // separate next digit with a space output[outLength + 1 ] = ' ' ; printCombinations(input, index + 1 , output, outLength + 2 ); // if next digit exists make a // call without including space if (input.length!=index + 1 ) printCombinations(input, index + 1 , output, outLength + 1 ); } // Driver Code public static void main(String[] args) { char input[] = "1214" .toCharArray(); char []output = new char [ 100 ]; printCombinations(input, 0 , output, 0 ); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to find all combination of numbers # from a given string of digits # function to print combinations of numbers # in given input string def printCombinations( input , index, output, outLength): # no more digits left in input string if ( len ( input ) = = index): # print output string & return output[outLength] = ' ' print ( * output[:outLength], sep = "") return # place current digit in input string output[outLength] = input [index] # separate next digit with a space output[outLength + 1 ] = ' ' printCombinations( input , index + 1 , output, outLength + 2 ) # if next digit exists make a # call without including space if ( len ( input ) ! = (index + 1 )): printCombinations( input , index + 1 , output, outLength + 1 ) # Driver code input = "1214" output = [ 0 ] * 100 # initialize output with empty string output[ 0 ] = ' ' printCombinations( input , 0 , output, 0 ) # This code is contributed by SHUBHAMSINGH10 |
C#
// C# program to find all combinations // of numbers from a given string of digits using System; class GFG { // function to print combinations of numbers // in given input string static void printCombinations( char [] input, int index, char [] output, int outLength) { // no more digits left in input string if (input.Length == index) { // print output string & return Console.WriteLine(String.Join( "" , output)); return ; } // place current digit in input string output[outLength] = input[index]; // separate next digit with a space output[outLength + 1] = ' ' ; printCombinations(input, index + 1, output, outLength + 2); // if next digit exists make a // call without including space if (input.Length!=index + 1) printCombinations(input, index + 1, output, outLength + 1); } // Driver Code public static void Main(String[] args) { char []input = "1214" .ToCharArray(); char []output = new char [100]; printCombinations(input, 0, output, 0); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to find all combinations // of numbers from a given string of digits // function to print combinations of numbers // in given input string function printCombinations(input,index,output,outLength) { // no more digits left in input string if (input.length == index) { // print output string & return document.write(output.join( "" )+ "<br>" ); return ; } // place current digit in input string output[outLength] = input[index]; // separate next digit with a space output[outLength + 1] = ' ' ; printCombinations(input, index + 1, output, outLength + 2); // if next digit exists make a // call without including space if (input.length != index + 1) printCombinations(input, index + 1, output, outLength + 1); } // Driver Code let input = "1214" .split( "" ); let output = new Array(100); printCombinations(input, 0, output, 0); // This code is contributed by avanitrachhadiya2155 </script> |
输出:
1 2 1 41 2 141 21 41 21412 1 412 14121 41214
替代解决方案:
C++
// CPP program to find all combination of // numbers from a given string of digits // using bit algorithm used same logic // as to print power set of string #include <bits/stdc++.h> using namespace std; // function to print combinations of // numbers in given input string void printCombinations( char s[]){ // find length of char array int l = strlen (s); // we can give space between characters // ex. ('1' & '2') or ('2' & '3') or // ('3' & '4') or ('3' & '4') or all // that`s why here we have maximum // space length - 1 for ( int i = 0; i < pow (2, l - 1); i++){ int k = i, x = 0; // first character will be printed // as well cout << s[x]; x++; for ( int j = 0; j < strlen (s) - 1; j++){ // if bit is set, means provide // space if (k & 1) cout << " " ; k = k >> 1; cout << s[x]; // always increment index of // input string x++; } cout << "" ; } } // driver code int main() { char input[] = "1214" ; printCombinations(input); return 0; } // This code is contributed by PRINCE Gupta 2 |
JAVA
// Java program to find all combination of // numbers from a given string of digits // using bit algorithm used same logic // as to print power set of string import java.util.*; class GFG { // function to print combinations of // numbers in given input string static void printCombinations( char s[]) { // find length of char array int l = s.length; // we can give space between characters // ex. ('1' & '2') or ('2' & '3') or // ('3' & '4') or ('3' & '4') or all // that`s why here we have maximum // space length - 1 for ( int i = 0 ; i < Math.pow( 2 , l - 1 ); i++) { int k = i, x = 0 ; // first character will be printed // as well System.out.print(s[x]); x++; for ( int j = 0 ; j < s.length - 1 ; j++) { // if bit is set, means provide // space if (k % 2 == 1 ) System.out.print( " " ); k = k >> 1 ; System.out.print(s[x]); // always increment index of // input string x++; } System.out.print( "" ); } } // Driver Code public static void main(String[] args) { char input[] = "1214" .toCharArray(); printCombinations(input); } } // This code is contributed by PrinciRaj1992 |
Python3
# Python 3 program to find all # combination of numbers from # a given string of digits using # bit algorithm used same logic # as to print power set of string # Function to print combinations of # numbers in given input string def printCombinations(s): # find length of char array l = len (s); # we can give space between # characters ex. ('1' & '2') # or ('2' & '3') or ('3' & '4') # or ('3' & '4') or all that`s # why here we have maximum # space length - 1 for i in range ( pow ( 2 , l - 1 )): k = i x = 0 # first character will # be printed as well print (s[x], end = "") x + = 1 for j in range ( len (s) - 1 ): # if bit is set, means # provide space if (k & 1 ): print ( " " , end = "") k = k >> 1 print (s[x], end = "") # always increment index of # input string x + = 1 print () # Driver code if __name__ = = "__main__" : inp = "1214" ; printCombinations(inp); # This code is contributed by Chitranayal |
C#
// C# program to find all combination of // numbers from a given string of digits // using bit algorithm used same logic // as to print power set of string using System; class GFG { // function to print combinations of // numbers in given input string static void printCombinations( char []s) { // find length of char array int l = s.Length; // we can give space between characters // ex. ('1' & '2') or ('2' & '3') or // ('3' & '4') or ('3' & '4') or all // that`s why here we have maximum // space length - 1 for ( int i = 0; i < Math.Pow(2, l - 1); i++) { int k = i, x = 0; // first character will be printed // as well Console.Write(s[x]); x++; for ( int j = 0; j < s.Length - 1; j++) { // if bit is set, means provide // space if (k % 2 == 1) Console.Write( " " ); k = k >> 1; Console.Write(s[x]); // always increment index of // input string x++; } Console.Write( "" ); } } // Driver Code public static void Main(String[] args) { char []input = "1214" .ToCharArray(); printCombinations(input); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // Javascript program to find all combination of // numbers from a given string of digits // using bit algorithm used same logic // as to print power set of string // function to print combinations of // numbers in given input string function printCombinations(s) { // find length of char array let l = s.length; // we can give space between characters // ex. ('1' & '2') or ('2' & '3') or // ('3' & '4') or ('3' & '4') or all // that`s why here we have maximum // space length - 1 for (let i = 0; i < Math.pow(2, l - 1); i++) { let k = i, x = 0; // first character will be printed // as well document.write(s[x]); x++; for (let j = 0; j < s.length - 1; j++) { // if bit is set, means provide // space if (k % 2 == 1) document.write( " " ); k = k >> 1; document.write(s[x]); // always increment index of // input string x++; } document.write( "<br>" ); } } // Driver Code let input= "1214" .split( "" ); printCombinations(input); // This code is contributed by rag2127 </script> |
输出:
12141 21412 141 2 14121 41 21 412 1 41 2 1 4
本文由 阿迪蒂·夏尔马2号 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END