面试:如何找出字符串的字典序全排列的第N种
1.题目
如何找出字符串的字典序全排列的第N种?(字符串全排列的变种)
2.思路
主要想通过这题,介绍一下康托展开式。基于康托展开式可以解决这个问题。
一般的解法:①求出所有全排列 ②按照字典序排个序 ③取第N个
3.康托展开与逆展开
康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩。 康托展开的实质是计算当前排列在所有由小到大全排列中的顺序,因此是可逆的。(引用)
3.1公式
X=a[n]*(n-1)!+a[n-1]*(n-2)!+…+a[i]*(i-1)!+…+a[1]*0!
其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。
a[i]的意义参见举例中的解释部分
3.2举例
例如,3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释:
排列的第一位是3,比3小的数有两个,以这样的数开始的排列有8!个,因此第一项为2*8!
排列的第二位是5,比5小的数有1、2、3、4,由于3已经出现,因此共有3个比5小的数,这样的排列有7!个,因此第二项为3*7!
以此类推,直至0*0!
伪代码实现
① Cantor(A, n) 求一个字符数组的康托值
Cantor(A, n)
for i← to n-
result ← result + Less(A[i]) * F[i]
return result
定义:
- A:待求康托值的字符数组
- n:字符数组长度,如公式中的n
- F:阶乘的结果集,如公式中(n-1)!、i!、2!、1!、0!
- Less:函数,求比自己小的数的个数,如公式中的a[i]的意义
②Less(n, set) 求比自己小的数的个数,公式中a[i]
Less(n, set)
for(m : set )
if m < n
count ← count+
add(set, n)
return n - count -
定义:
- n:待求比自己小的数
- set:存放已经出现过的数
- count:比3小的数有1,2,如果1,2在set中出现了,count就计数这个。
- 返回值:-1的目的是为了得到a[i]
3.3用途
显然,n位(0~n-1)全排列后,其康托展开唯一且最大约为n!,因此可以由更小的空间来储存这些排列。由公式可将X逆推出对应的全排列。
3.4康托展开的逆运算
既然康托展开是一个双射,那么一定可以通过康托展开值求出原排列,即可以求出n的全排列中第x大排列。
如n=5,x=96时:
- 首先用96-1得到95,说明x之前有95个排列.(将此数本身减去!)
- 用95去除4! 得到3余23,说明有3个数比第1位小,所以第一位是4.
- 用23去除3! 得到3余5,说明有3个数比第2位小,所以是4,但是4已出现过,因此是5.
- 用5去除2!得到2余1,类似地,这一位是3.
- 用1去除1!得到1余0,这一位是2.
- 最后一位只能是1.
- 所以这个数是45321.
伪代码实现
①ResolveCantor(A, X, n):给第X种,求该全排列n的字符串
ResolveCantor(A, X, n)
for i← to n-
a ← X div F[i]
b ← X mod F[i]
A[i] ← Solve(a, visit)
X ← b
return A
定义:
- A:存储字符串的结果
- X:字典序全排列的X种(0,1,2,3,...),这个值是康托值
- n:字符数组长度,如康托公式中的n
- F:阶乘的结果集,如公式中(n-1)!、i!、2!、1!、0!
- Solve:函数,求某个输出字符
②Solve(a, visit):求某个输出字符
Solve(a, visit)
while a is visited
a← a+
see a is visited or not
return a +
定义:
- a:康托公式中的a[i]
- visit:boolean数组,visit[a]表示a是否已经出现过了。
- 返回值:+1 为了构建出输出字符
如果用这个算法去求字符串的全排列,时间复杂度是O(n3),优于递归算法的O(n!)。
3.5 Java代码实现
为了实现简单一些,实现部分采用的是int数组。
import java.util.HashSet;
import java.util.Set; public class Cantor { public static final int LEN = 3;
private static int[] f = new int[LEN];
private static Set<Integer> set = new HashSet<Integer>();
private static boolean[] visit = new boolean[LEN]; static {
int re = 1;
for (int i = 1; i < LEN; i++) {
re *= i;
f[LEN - 1 - i] = re;
}
f[LEN - 1] = 1;
for (int i = 0; i < LEN; i++) {
visit[i] = false;
}
System.out.println("F[0]: " + f[0]);
} public static void main(String[] args) {
int[] a = { 2, 1, 3 };
int n = a.length;
int x = cantor(a, n);
String str = "";
for (int i = 0; i < n; i++) {
str += "" + a[i];
}
System.out.println("src String: " + str);
System.out.println("cantor value: " + x);
int[] b = new int[LEN];
resolveCantor(b, x, n);
str = "";
for (int i = 0; i < n; i++) {
str += "" + b[i];
}
System.out.println("resolve cantor str: " + str);
} static int cantor(int[] a, int n) {
int result = 0;
for (int i = 0; i < n; i++) {
result += less(a[i]) * f[i];
}
return result;
} private static int less(int n) {
int count = 0;
for (Integer m : set) {
if (m < n)
count++;
}
set.add(n);
return n - count - 1;
} static int[] resolveCantor(int[] arr, int x, int n) {
int a, b;
for (int i = 0; i < n; i++) {
a = x / f[i];
b = x % f[i];
arr[i] = solve(a);
System.out.println("a: " + a + " b: " + b + " arr[i]: " + arr[i]);
x = b;
}
return arr;
} private static int solve(int a) {
boolean flag = true;
while (flag) {
if (visit[a] == false) {
visit[a] = true;
flag = false;
} else {
a++;
}
}
return a + 1;
}
}
面试:如何找出字符串的字典序全排列的第N种的更多相关文章
- 35-面试:如何找出字符串的字典序全排列的第N种
http://www.cnblogs.com/byrhuangqiang/p/3994499.html
- 剑指Offer 找出字符串中第一个只出现一次的字符
题目描述 找出字符串中第一个只出现一次的字符 如果无此字符 请输出'.' 输入描述: 输入一串字符,由小写字母组成 输出描述: 输出一个字符 输入例子: asdfasdfo 输出例子: o 思路:数组 ...
- 找出字符串中第一个不重复的字符(JavaScript实现)
如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...
- (1) 一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串
/** * 有一个字符串,根据输入参数m,找出字符串的m个字符的所有字符串 例如: String str ="abc", m=2 得到结果是 "ab" &quo ...
- js常会问的问题:找出字符串中出现次数最多的字符。
一.循环obj let testStr = 'asdasddsfdsfadsfdghdadsdfdgdasd'; function getMax(str) { let obj = {}; for(le ...
- C/C+面试题一:找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)
已知字符串"aabbbcddddeeffffghijklmnopqrst"编程找出出现最多的字符和次数,要求时间复杂度小于O(n^2) /********************* ...
- [LeetCode] Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
- 【easy】438.Find All Anagrams in a String 找出字符串中所有的变位词
Input: s: "abab" p: "ab" Output: [0, 1, 2] Explanation: The substring with start ...
- [LeetCode] 438. Find All Anagrams in a String 找出字符串中所有的变位词
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...
随机推荐
- android 5.0 创建多用户 双开多开应用(1)
Andriod5.0多用户 双开应用 android多用户是5.0之后有的,类似windows的账户系统 不过官方还没有完全确认,API大都是hide状态 我这里提供一种方式并不适用所有的,由于我们有 ...
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- mybatis缓存清除方法
String cacheName = IWenshiduDao.class.getName(); Ehcache cache = CacheManager.create().getEhcache(ca ...
- JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
var Api = { ajax:{ // 添加项目 旧! add_project : function(pro_name, html, css, js,callback) { $.post(&quo ...
- MYSQL批处理
待更新 版权声明:本文为博主原创文章,未经博主允许不得转载.
- 一个简单的web服务器例子
一个简单的web容器小例子,功能十分简单,只能访问静态资源,对于新手来说还是有一定的意义.主要分三个类 1.server类:主要功能开启socketServer,阻塞server,接收socket访问 ...
- Java之循环输出等腰三角形
public class aaa{ public static void main(String[] args) { int max=5; for(int i=1;i<=5;i++){//控制行 ...
- node初步二 小爬虫
小爬拉勾网 获取想要的信息: 一.分三步 1 获得数据 2 处理数据 3展示数据 二 .代码 :创建文件reptile.js;写入 var http=require('http'); var chee ...
- 微信菜单开发:使用PHP数组来定义微信菜单
目前使用数组的方式来定义,然后在通过json_encode函数来转JSON $menuJson=array(); $menuJson['button'][]=array('name'=>urle ...
- JavaScript学习心得(八)
Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...