s*n
2 楼
string permutation,怎么处理重复字母?
r*y
3 楼
amazon的好 还是nook GW 的好??
NOOK的有打折 貌似70多拿下了
NOOK的有打折 貌似70多拿下了
n*y
5 楼
都差不多。我喜欢nook glowlight,我认识的所有别人都喜欢kindle, paperwhite.
w*s
6 楼
Two implementation. one use sort and one use hash.
//pre: str is sorted.
void PermutationWithDuplicate(char *str, int position)
{
if (position == strlen(str)) {
printf("%s", str);
return;
}
char lastChar = '\0';
for (int i = position; i {
//skip those character which is duplicated. Since the string is
sorted, it's easy.
if (lastChar == str[i] ) continue;
lastChar = str[i];
swap(str[position], str[i]);
PermutationWithDuplicate(str,position+1);
swap(str[i] , str[position]);
}
}
//pre: str is not sorted and str can't be changed. We can use hash to do
this.
void PermutationWithDuplicateHash(char *str, int position)
{
int HashTable[128] = {0}; //assume ASCII
if (position == strlen(str)) {
printf("%s", str);
return;
}
for (int i = position; i {
if (HashTable[str[i]) > 0) continue;
else{
HashTable[str[i]] = 1;
}
swap(str[position], str[i]);
PermutationWithDuplicateHash(str,position+1);
swap(str[i] , str[position]);
}
}
main()
{
char str[] = "hello world";
// If duplicated character is allowed, let's sort the string first.
sort(str);
PermutationWithDuplicate(str, 0)
//method 2. Using Hashtable to make a judgement whether it's duplicated
PermutationWithDuplicateHash(str, 0)
}
//pre: str is sorted.
void PermutationWithDuplicate(char *str, int position)
{
if (position == strlen(str)) {
printf("%s", str);
return;
}
char lastChar = '\0';
for (int i = position; i
//skip those character which is duplicated. Since the string is
sorted, it's easy.
if (lastChar == str[i] ) continue;
lastChar = str[i];
swap(str[position], str[i]);
PermutationWithDuplicate(str,position+1);
swap(str[i] , str[position]);
}
}
//pre: str is not sorted and str can't be changed. We can use hash to do
this.
void PermutationWithDuplicateHash(char *str, int position)
{
int HashTable[128] = {0}; //assume ASCII
if (position == strlen(str)) {
printf("%s", str);
return;
}
for (int i = position; i
if (HashTable[str[i]) > 0) continue;
else{
HashTable[str[i]] = 1;
}
swap(str[position], str[i]);
PermutationWithDuplicateHash(str,position+1);
swap(str[i] , str[position]);
}
}
main()
{
char str[] = "hello world";
// If duplicated character is allowed, let's sort the string first.
sort(str);
PermutationWithDuplicate(str, 0)
//method 2. Using Hashtable to make a judgement whether it's duplicated
PermutationWithDuplicateHash(str, 0)
}
m*i
8 楼
It is still a solution without sorting or hashing.
public void permutationRecursive(int[] arr, int index) {
if (arr.length == index) {
for (int i = 0; i < arr.length; ++i) {
System.out.printf("%d", arr[i]);
}
System.out.println();
return;
}
int lastSwap = 0;
for (int i = index; i < arr.length; ++i) {
if (arr[i] == arr[index] && i != index) continue;
if (arr[i] == lastSwap) continue;
lastSwap = arr[i];
swap(arr, i, index);
permutationRecursive(arr, index+1);
swap(arr, i, index);
}
}
private void swap(int[] arr, int i, int j) {
assert (i >= 0 && i >= 0 && i < arr.length && j < arr.length);
if (i != j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {2,3,2,3};
Permutation p = new Permutation();
System.out.println("Permutation");
p.permutationRecursive(arr, 0);
}
public void permutationRecursive(int[] arr, int index) {
if (arr.length == index) {
for (int i = 0; i < arr.length; ++i) {
System.out.printf("%d", arr[i]);
}
System.out.println();
return;
}
int lastSwap = 0;
for (int i = index; i < arr.length; ++i) {
if (arr[i] == arr[index] && i != index) continue;
if (arr[i] == lastSwap) continue;
lastSwap = arr[i];
swap(arr, i, index);
permutationRecursive(arr, index+1);
swap(arr, i, index);
}
}
private void swap(int[] arr, int i, int j) {
assert (i >= 0 && i >= 0 && i < arr.length && j < arr.length);
if (i != j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = {2,3,2,3};
Permutation p = new Permutation();
System.out.println("Permutation");
p.permutationRecursive(arr, 0);
}
s*n
10 楼
nice..thanks..
H*s
12 楼
这样不行啊, 如果输入: 1323, 输出有重复的。
我明白你的思路:就是每层循环在一个位置,每个unique字符只出现一次,
但是这只有hash才能保证啊。否则,例子中第二个3的lastSwap是2啊,所以还是有重复
的。
【在 m****i 的大作中提到】
: It is still a solution without sorting or hashing.
: public void permutationRecursive(int[] arr, int index) {
: if (arr.length == index) {
: for (int i = 0; i < arr.length; ++i) {
: System.out.printf("%d", arr[i]);
: }
: System.out.println();
: return;
: }
: int lastSwap = 0;
我明白你的思路:就是每层循环在一个位置,每个unique字符只出现一次,
但是这只有hash才能保证啊。否则,例子中第二个3的lastSwap是2啊,所以还是有重复
的。
【在 m****i 的大作中提到】
: It is still a solution without sorting or hashing.
: public void permutationRecursive(int[] arr, int index) {
: if (arr.length == index) {
: for (int i = 0; i < arr.length; ++i) {
: System.out.printf("%d", arr[i]);
: }
: System.out.println();
: return;
: }
: int lastSwap = 0;
w*x
14 楼
每个递归栈上用bool chr[256]
w*o
18 楼
测试了一下,用hash的方法PermutationWithDuplicateHash是对的。
另一个先sort,然后只用一个变量lastChar的方法,不正确,会有重复的输出。
【在 w*******s 的大作中提到】
: Two implementation. one use sort and one use hash.
: //pre: str is sorted.
: void PermutationWithDuplicate(char *str, int position)
: {
: if (position == strlen(str)) {
: printf("%s", str);
: return;
: }
:
: char lastChar = '\0';
另一个先sort,然后只用一个变量lastChar的方法,不正确,会有重复的输出。
【在 w*******s 的大作中提到】
: Two implementation. one use sort and one use hash.
: //pre: str is sorted.
: void PermutationWithDuplicate(char *str, int position)
: {
: if (position == strlen(str)) {
: printf("%s", str);
: return;
: }
:
: char lastChar = '\0';
h*e
20 楼
kindle pw的优点是屏幕分辨率高,1024*768
nook是800 *600
nook的优点是用android 2.1系统,可以越狱后装在线阅读软件
而kindle只能多看
nook是800 *600
nook的优点是用android 2.1系统,可以越狱后装在线阅读软件
而kindle只能多看
相关阅读
三足鼎立才是最利于发展的msn是不是出问题了yahoo.com.cn邮箱如何解决?Nexus 10还是Note 10.1?HTC One 没有search键跟大家说一个奇怪的电话波士顿抓人,Bing Map大胜,google map惨败htc one 初体验s4还是note2htc 是不是有病没有pps还有什么软件能直看港戏?HTC One是通吃吗?iPad里的lte SIM卡有没有可能插手机上用?单买手机的话,目前有哪些性价比比较好的?Can you order HTC one for Sprint SERO plan?windows 平板开始降价了gs3 手机用电求教:MITBBS费电比screen还多请教一个输入问题 (转载)yoga买回来用了一阵子推荐一个HTC ONE 的套吧