zoukankan      html  css  js  c++  java
  • 全排列Permutations

    描述

    Given a collection of numbers, return all possible permutations.
    For example,
    [1,2,3] have the following permutations:
    [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]

    代码

    package com.lilei.myes.es.pack1107;
    
    public class quanpailie {
    
    	public static void main(String[] args) {
    		char[] cs = new char[] { 'a', 'b', 'c','d' };
    
    		pailie(cs, 0);
    
    	}
    
    	public static void pailie(char[] cs, int e) {
    
    		if (e == cs.length) {
    			System.out.println(new String(cs));
    		} else {
    
    			for (int i = e; i < cs.length; i++) {
    				swap(cs, i, e);
    				pailie(cs,  e + 1);
    				swap(cs, i, e);
    
    			}
    
    		}
    	}
    
    	static void swap(char[] cs, int a, int b) {
    		char tmp = cs[a];
    		cs[a] = cs[b];
    		cs[b] = tmp;
    	}
    
    }
    

      

  • 相关阅读:
    散列表
    5月19日
    5月17日阅读笔记3
    5月15日 阅读笔记2
    5月14日 阅读笔记1
    团队5
    团队博客2
    团队博客4
    团队博客3
    团队博客1
  • 原文地址:https://www.cnblogs.com/lilei2blog/p/7802612.html
Copyright © 2011-2022 走看看