字符串全排列算法

  List permu(String s){
        List result=new ArrayList();
        if("".equals(s)){
            result.add("");
            return result;
        }
        String c = s.substring(0, 1);
        List res = permu(s.substring(1));
        for(int i=0; i<res.size(); ++i){
            String t = (String) res.get(i);
            for(int j=0; j<=t.length(); ++j){
                StringBuffer  u = new StringBuffer(t);
                u.insert(j, c);
                result.add(u.toString());
            }
        }
        return result;
    }
    
    @Test
    public void testPermu() throws Exception {
        System.out.println(permu("abc"));
    }

猜你喜欢

转载自daybyday.iteye.com/blog/2314992