我认为逻辑可能如下:
维护一个字符列表/数组,例如
char[] chars = {'A','B', 'C', 'D','E'};
- 创建3个索引变量,begin = 0,end = chars.length-1,index = 0;
为每个范围打印实现 2 个嵌套的 for 循环,
for begin =0 to end
collect chars[beign]
for indx end -begin to end
collect chars[indx]
我认为这会做到这一点。
一个示例逻辑如下:
char[] chars = {'A','B', 'C', 'D','E'};
List<Character> charList = new ArrayList<Character>();
for (int begin=0; begin <chars.length; begin++){
for (int index=begin; index <chars.length; index++){
charList.add(chars[begin]);
for (int indx1=chars.length-index; indx1 <chars.length-begin; indx1++){
charList.add(chars[indx1+begin]);
}
System.out.println(charList);
charList.clear();
}
}
应该将结果打印为:
[A]
[A, E]
[A, D, E]
[A, C, D, E]
[A, B, C, D, E]
[B]
[B, E]
[B, D, E]
[B, C, D, E]
[C]
[C, E]
[C, D, E]
[D]
[D, E]
[E]