/*
* 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:
原始字符串是"abc",打印得到下列所有组合情况:
"a" "b" "c"
"ab" "bc" "ca" "ba" "cb" "ac"
"abc" "acb" "bac" "bca" "cab" "cba"- package com.itheima;
- public class Test8{
- public static void main(String[] args)
- {
- String a=new String("abc");
- String b=new String();
- //双循环获取所有组合
- for(int i=0;i<a.length();i++)
- {
- for(int j=i;j<a.length();j++)
- {
- b=converse(a,i,j);
- sop(b.substring(i,j+1),"");
-
- }
- }
- }
- //获取从1到length长度子串的所有组合,并输出
- public static void sop(String s,String n)
- {
- if (s.length() == 0)
- {
- System.out.println(n);
- } else {
- for (int i = 0; i < s.length(); ++i)
- {
- sop(s.substring(1), n + s.charAt(0));
- s = s.substring(1) + s.charAt(0);
- }
- }
-
- }
- //自定义函数,用于交换字符串中相应字符的位置
- public static String converse(String a,int i,int j)
- {
- char[] b=a.toCharArray();
- char temp;
- temp=b[i];
- b[i]=b[j];
- b[j]=temp;
- return new String(b);
- }
- }
复制代码
* */
|
|