字符串的排列组合(组合需无重复字母)
来源:互联网 发布:淘宝卖的烟台苹果亏本 编辑:程序博客网 时间:2024/05/21 09:33
import java.util.Arrays;import java.util.Iterator;import java.util.Scanner;import java.util.Stack;//字符串的排列组合public class Combination {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner scan = new Scanner(System.in);String string = scan.next();Stack<Character> result = new Stack<Character>();System.out.println("字符串的全部组合方法0");Allcombine(string, result);System.out.println("字符串的全部组合方法1");for (int i = 1; i <= string.length(); i++) {Allcombineone(string, i, result);// 字符串的全部组合}System.out.println("字符串的全部组合方法2");Allcombinetwo(string);System.out.println("字符串的全部排列");permutation(string.toCharArray(), 0);char[] a = new char[5];Arrays.fill(a, 'a');System.out.println(a);}public static void Allcombine(String string, Stack<Character> result) {if (string.length() == 0) {Iterator<Character> it = result.iterator();while (it.hasNext()) {System.out.print(it.next());}System.out.println();return;}// if (string.length() == 0 || string == null) {// return;// }result.push(string.charAt(0));Allcombine(string.substring(1, string.length()), result);result.pop();//System.out.println("字符串长度" + string.length());Allcombine(string.substring(1, string.length()), result);}public static void Allcombineone(String string, int number,Stack<Character> result) {if (number == 0) {Iterator<Character> it = result.iterator();while (it.hasNext()) {System.out.print(it.next());}System.out.println();return;}if (string.length() == 0 || string == null) {return;}result.push(string.charAt(0));Allcombineone(string.substring(1, string.length()), number - 1, result);result.pop();Allcombineone(string.substring(1, string.length()), number, result);}// 将1到N-1用二进制表示,有1的输出该位置的字母,方法非常好,主要用到移位操作public static void Allcombinetwo(String str) {int N = str.length();int num = (int) Math.pow(2.0, N);// Cn0+Cn1+Cn2+......+Cnn=2的n次方for (int i = 1; i < num; i++) {for (int j = 0; j < N; j++) {if (((i >> j) & 1) != 0)System.out.print(str.charAt(j));}System.out.println();}}// 全排列public static void permutation(char[] str, int start) {if (str == null || str.length == 0) {return;}if (start == str.length) {System.out.println(str);return;}for (int i = start; i < str.length; i++) {if (isExist(str, start, i)) {//加上这个条件变成有重复字母下的全排列char temp1 = str[i];str[i] = str[start];str[start] = temp1;permutation(str, start + 1);char temp2 = str[i];str[i] = str[start];str[start] = temp2;}}}//判断str[i]在(start到i-1)的范围内是否出现过public static boolean isExist(char[] str , int start , int i){for(int j =start;j<i;j++ ){if(str[j]==str[i])return false;}return true;}}
0 0
- 字符串的排列组合(组合需无重复字母)
- 求【无重复字母】字符串的所有子串
- lua实现字母可重复组合有多少种(不可重复的看前面文章排列组合)
- java 数字组合代码 按顺序,每组无重复,形成一个无重复的字符串
- 字符串组合问题(无重复)
- [算法]寻找字母重复组合的单词
- 求一个字符串的最长无重复字母的连续子串
- 求字符串的全部组合(字符串中无重复字符)
- C# 字符串字母的大小写组合
- 找出字符串中重复最多的字母
- 三字母字符串组合
- 排列组合之字符串的全排列和组合算法
- 字符串的全排列组合(去重复)的相关问题
- 字符串去除重复字母
- 给定一个字符串,找出其所有不重复的排列组合
- java生成10位不重复的字母+数字组合
- [组合算法] 生成不重复的字符串
- 字符串的所有不重复组合
- PowerDesigner简介
- 关于OpenSSL heartbleed漏洞
- ARC Rules
- 程序员之痛:诊断下你会不会突然间瘁死
- poj_1032_Parliament(规律)
- 字符串的排列组合(组合需无重复字母)
- 关于application/x-www-form-urlencoded等字符编码的解释说明
- 如何将默认的串口控制台屏蔽
- scipy: 科学计算
- linux学习好书
- Android开发之onClick事件的三种写法
- Servlet 使用入门
- iOS7.1企业证书问题
- OpenCV编程->cvCamShift函数