28 字符串的排列
来源:互联网 发布:mac xlsx文件怎么打开 编辑:程序博客网 时间:2024/06/07 03:11
描述:
实现代码:
package question28;import java.util.ArrayList;import java.util.List;public class StringPermutation {staticArrayList<String> list=new ArrayList<String>();public static void main(String[] args) {// TODO Auto-generated method stub String str="abc"; char pStr[]=str.toCharArray(); Permutation(pStr); System.out.println(list); }private static void Permutation(char[] pStr) {if (pStr==null||pStr.length==0) {return;}Permutation(pStr,0);}private static void Permutation(char[] pStr,int index) { //index 代表待处理的字符数组的开始位置// TODO Auto-generated method stubif (index==pStr.length-1) { StringBuffer sb=new StringBuffer();for (char c : pStr) {sb.append(String.valueOf(c));}if (!list.contains(sb.toString())) { // 防止加入重复的 例如输入字符串"aa" ,只需要输出["aa"],而不是["aa","aa"] list.add(sb.toString());} }else {for (int i=index; i<pStr.length; i++) {// 交换char temp=pStr[index];pStr[index]=pStr[i];pStr[i]=temp;Permutation(pStr, index+1); // 交换回来temp=pStr[index];pStr[index]=pStr[i];pStr[i]=temp;}}}}
测试结果:
0 0
- 28 字符串的排列
- 28-字符串的排列
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 面试题28 字符串的全排列
- 剑指offer 28- 字符串的排列
- 28 - 字符串的全排列和组合
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 剑指offer-28字符串的排列
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 面试题28字符串的排列
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 面试题28:字符串的排列
- 面试题28—字符串的排列
- IOS6屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
- Moving Average of An Input Stream
- 设计模式之责任链模式
- JSP续
- Status Bar变换
- 28 字符串的排列
- 素数距离题目
- ccv的安装以及测试
- JavaScript中注册时间处理程序的方式
- iOS实现水波纹
- HDU 4730-We Love MOE Girls
- 百度编辑器上传大容量视频报HTTP错误
- docker最初版本源码分析
- Eclipse运行时概述一