Main 类中所有的方法和变量全部定义为 static 类型,因为主函数是 static 类型。 新定义一个类时(如 ListNode),将其与 Main 类并列,并且不能定义为 public 类型。
import java. util. * ;
class ListNode {
int val;
ListNode next;
public ListNode ( ) { }
public ListNode ( int val) {
this . val = val;
this . next = null ;
}
}
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
}
}
import java. util. * ;
public class Main {
private static Set < String > res;
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
while ( in. hasNext ( ) ) {
String s = in. nextLine ( ) ;
res = new HashSet < > ( ) ;
permutation ( s) ;
for ( String temp : res) {
System . out. print ( temp + " " ) ;
}
System . out. println ( ) ;
}
}
public static void permutation ( String s) {
if ( s == null ) return ;
char [ ] str = s. toCharArray ( ) ;
String track = "" ;
boolean [ ] visited = new boolean [ str. length] ;
backtrack ( str, track, visited) ;
}
public static void backtrack ( char [ ] str, String track, boolean [ ] visited) {
if ( track. length ( ) == str. length) {
res. add ( track) ;
return ;
}
for ( int i = 0 ; i < str. length; i++ ) {
if ( visited[ i] ) continue ;
visited[ i] = true ;
backtrack ( str, track + str[ i] , visited) ;
visited[ i] = false ;
}
}
}