原始字符串是"abc",打印得到下列所有组合情况java如何实现要有注释,谢谢

tanshionjob 2014-07-03 02:43:50
编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 原始字符串是"abc",打印得到下列所有组合情况: "a" "b" "c"
* "ab" "bc" "ca" "ba" "cb" "ac" "abc" "acb" "bac" "bca" "cab" "cba"
...全文
531 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
whos2002110 2014-09-19
  • 打赏
  • 举报
回复
引用 8 楼 u011443763 的回复:
[quote=引用 6 楼 whos2002110 的回复:]

public static void main(String[] args) {
		String str = "abcde";
		
		List<String> strAss = new ArrayList<String>();
		for (int i = 0; i < str.length(); i++) {
			for (int j = i + 1; j <= str.length(); j++) {
				String sub = str.substring(i, j);
				
				int r = 0;
				while (r++ < sub.length()) {
					strAss.add(sub);
					sub = sub.substring(1) + sub.substring(0, 1);
				}
			}
		}
		
		for (String a : strAss) {
			System.out.println(a);
		}
	}
输出:

a
ab
ba
abc
bca
cab
abcd
bcda
cdab
dabc
abcde
bcdea
cdeab
deabc
eabcd
b
bc
cb
bcd
cdb
dbc
bcde
cdeb
debc
ebcd
c
cd
dc
cde
dec
ecd
d
de
ed
e

还是错[/quote] 嗯, 是错的, 我当时写完就发现了
classjavaonline 2014-09-19
  • 打赏
  • 举报
回复
引用 6 楼 whos2002110 的回复:

public static void main(String[] args) {
		String str = "abcde";
		
		List<String> strAss = new ArrayList<String>();
		for (int i = 0; i < str.length(); i++) {
			for (int j = i + 1; j <= str.length(); j++) {
				String sub = str.substring(i, j);
				
				int r = 0;
				while (r++ < sub.length()) {
					strAss.add(sub);
					sub = sub.substring(1) + sub.substring(0, 1);
				}
			}
		}
		
		for (String a : strAss) {
			System.out.println(a);
		}
	}
输出:

a
ab
ba
abc
bca
cab
abcd
bcda
cdab
dabc
abcde
bcdea
cdeab
deabc
eabcd
b
bc
cb
bcd
cdb
dbc
bcde
cdeb
debc
ebcd
c
cd
dc
cde
dec
ecd
d
de
ed
e

还是错
百八烦恼风 2014-07-18
  • 打赏
  • 举报
回复
public class Test0718 {
	static char[] chars="abcd".toCharArray();
	public static void main(String[] args) {
		for(int i=0;i<chars.length;i++){
			//取得每一个字符
			List<Integer> list=new ArrayList<Integer>();
			list.add(i);
			play(list);
		}
	}
	//使用递归,每次加上列表中不存在的一个字符
	private static void play(List<Integer> list){
		print(list);
		for(int i=0;i<chars.length;i++){
			if(!list.contains(i)){
				List<Integer> temp=new ArrayList<Integer>(list);
				temp.add(i);
				play(temp);
			}
		}
	}
	//打印列表内容
	private static void print(List<Integer> list){
		for(Integer i:list)
			System.out.print(chars[i]+"");
		System.out.println();
	}
}
没有判断重复字母,判断重复的话就要把这些放到set里,输出:
a
ab
abc
abcd
abd
abdc
ac
acb
acbd
acd
acdb
ad
adb
adbc
adc
adcb
b
ba
bac
bacd
bad
badc
bc
bca
bcad
bcd
bcda
bd
bda
bdac
bdc
bdca
c
ca
cab
cabd
cad
cadb
cb
cba
cbad
cbd
cbda
cd
cda
cdab
cdb
cdba
d
da
dab
dabc
dac
dacb
db
dba
dbac
dbc
dbca
dc
dca
dcab
dcb
dcba
whos2002110 2014-07-03
  • 打赏
  • 举报
回复

public static void main(String[] args) {
		String str = "abcde";
		
		List<String> strAss = new ArrayList<String>();
		for (int i = 0; i < str.length(); i++) {
			for (int j = i + 1; j <= str.length(); j++) {
				String sub = str.substring(i, j);
				
				int r = 0;
				while (r++ < sub.length()) {
					strAss.add(sub);
					sub = sub.substring(1) + sub.substring(0, 1);
				}
			}
		}
		
		for (String a : strAss) {
			System.out.println(a);
		}
	}
输出:

a
ab
ba
abc
bca
cab
abcd
bcda
cdab
dabc
abcde
bcdea
cdeab
deabc
eabcd
b
bc
cb
bcd
cdb
dbc
bcde
cdeb
debc
ebcd
c
cd
dc
cde
dec
ecd
d
de
ed
e

whos2002110 2014-07-03
  • 打赏
  • 举报
回复
错了, 楼主无视我吧
whos2002110 2014-07-03
  • 打赏
  • 举报
回复

String str = "abcde";
		
		List<String> strAss = new ArrayList<String>();
		for (int i = 1; i <= str.length(); i++) {
			String sub = str.substring(0, i);
			
			int j = 0;
			while (j++ < sub.length()) {
				strAss.add(sub);
				sub = sub.substring(1) + sub.substring(0, 1);
			}
		}
		
		for (String a : strAss) {
			System.out.println(a);
		}
输出:

a
ab
ba
abc
bca
cab
abcd
bcda
cdab
dabc
abcde
bcdea
cdeab
deabc
eabcd
shine333 2014-07-03
  • 打赏
  • 举报
回复
好吧,貌似没看清题意
shine333 2014-07-03
  • 打赏
  • 举报
回复
用二进制判断,

    String str = "abc";
    char[] chars = str.toCharArray();
    // 0001 二进制左移 3 位,得到 1000
    int max = 1 << chars.length;

    // 遍历i从二进制001(1)到二进制111(7)
    for (int i = 1; i < max; i++) {
      // 遍历所有字符是否出现
      for (int j = 0; j < chars.length; j++) {
        // 当前字符在右面开始第几位bit
        int mask = (1 << j);
        if ((i & mask) != 0) {
          // 如果当前bit为1,则该字符应当出现
          System.out.print(chars[j]);
        }
      }
      System.out.println();
    }

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧