题目
https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=190&rp=1&ru=%2Fta%2Fjob-code-high-rd&qru=%2Fta%2Fjob-code-high-rd%2Fquestion-ranking
import java.util.*;
public class Solution {
public ArrayList<String> list=new ArrayList<>();
public ArrayList<String> Permutation(String str) {
if(str.length()==1)
{
list.add(String.valueOf(str));
return list;
}
if(str.length()!=0)
{
getAllString(str.toCharArray(),0);
}
Collections.sort(list);
return list;
}
public void getAllString(char[] str,int index)
{
if(str.length-1<=index)
{
String str1=String.valueOf(str);
if(!list.contains(str1))
{
list.add(str1);
}
}
else
{
for(int i = index;i<str.length;i++)
{
swap(str,i,index);
getAllString(str,index+1);
swap(str,i,index);
}
}
}
public void swap(char[] str,int i ,int j)
{
char temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
全部评论
(0) 回帖