全排列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]
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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; } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步