【LintCode】数组的全排序(1)
来源:互联网 发布:js ajax视频教程 编辑:程序博客网 时间:2024/06/08 06:35
给定一个数组,返回其所有可能的排列。
注意事项
你可以假设没有重复数字。
样例
给出一个数组abc,
其全排列为:
abc
acb
bac
bca
cba
cab
Java:
public class Solution{ private static void print(char[] arr){ if(arr == null || arr.length == 0){ return; } for(int i = 0; i < arr.length; i++){ System.out.print(arr[i]); } System.out.println(); } private static void swap(char[] ch, int i, int k){ char temp = ch[i]; ch[i] = ch[k]; ch[k] = temp; } private static void permutation(char[] arr, int k){ //递归:f(n) = k·f(n-1),将字符串中每一个元素都放到第k位(假设就是首位,即k设为0),剩下的n-1个进行全排列(递归) // (k <= arr.length-1) if(k == arr.length - 1){ print(arr); return; } for(int i = k; i < arr.length; i++) { swap(arr,i,k); permutation(arr,k+1); swap(arr,i,k); } } public static void main(String[] args){ char[] ch = {'a','b','c'}; permutation(ch,0); }}
阅读全文
2 0
- 【LintCode】数组的全排序(1)
- LintCode 两个排序数组的中位数
- lintcode, 两个排序数组的中位数
- Lintcode 两个排序数组的中位数
- lintcode两个排序数组的中位数
- Lintcode删除排序数组的重复数字
- 两排序数组的中位数(LintCode)
- LintCode 合并排序数组
- LintCode: 合并排序数组
- lintcode ----合并排序数组
- LintCode:合并排序数组
- lintcode--合并排序数组
- Lintcode合并排序数组
- Lintcode:合并排序数组
- LintCode-搜索旋转排序数组
- LintCode 合并排序数组 II
- LintCode : 搜索旋转排序数组
- 合并排序数组(LintCode)
- python基础之模块之os模块
- PostgreSQL新手入门
- 转载资深程序员的总结经验
- selenium之unittest批量执行测试用例、生成html报告
- 嵌入式-1 uboot模式烧写镜像文件
- 【LintCode】数组的全排序(1)
- 如何限制checkbox最多勾选个数
- OpenCv--Mat矩阵(图像容器)的创建及CV_8UC1,CV_8UC2等参数详解
- 带空格或逗号的字符串和基础数据类型的互相转换
- Quartz框架学习之一-快速入门
- Mac brew 升级gradle 后降级
- MySQL的保留关键字,使用时尽量避免
- Redis主从复制
- 移动WebApp开发-jQuery Mobile入门