由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 顶风上来问道题:一个很大char[], 如何in-place 删除重复元素
相关主题
G家电面被拒,请帮助分析原因cc1501.3题,请帮忙测试下代码
Google实习第一轮电话面试总结leetcode: Remove Duplicates from Sorted Array
ebay电面面经,攒人品,求好运【一个BB公司问的字母排序的问题】
find first nonduplicate unicode questions一道msft的题
string permutation,怎么处理重复字母?leetcode里面的Recover Binary Search Tree怎么用O(1)space
为什么加个结束符leetcode就run time error呢?怎么理解递归解决的“swap every two elements in a linked list”?
回馈本版,贴GOOGLE电话面经弱问,BST到底能不能有重复值?
请教C/C++小再问个简单的C问题
相关话题的讨论汇总
话题: char话题: array话题: 256话题: place话题: duplicate
进入JobHunting版参与讨论
1 (共1页)
n*******r
发帖数: 22
1
实在想不出in-place的方法。哪位提示我一下?
A*****i
发帖数: 3587
2
用vector
然后用迭代器迭代
以前做file system的时候经常遇到这种问题
n*******r
发帖数: 22
3
你说的是stl::vector?那还是in-place吗?

【在 A*****i 的大作中提到】
: 用vector
: 然后用迭代器迭代
: 以前做file system的时候经常遇到这种问题

D*****7
发帖数: 766
4
每碰到一个,就去已经找过的部分看看是不是已经存在,时间复杂度很高
如果对时间复杂度有进一步的要求话,就将已经找过的部分构建成一个BST(用Array来
表达BST)。

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
k*p
发帖数: 1526
5
二重循环,维护tail指针

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
h**k
发帖数: 3368
6
如果允许用额外空间,可以先记录重复字符,然后用两个指针遍历数组。这个遍历类似
于Binary Search中二分数组。

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
r*******e
发帖数: 7583
7
时间复杂度没有要求?
那就in-place sort,然后用两个指针走一遍就好了。。

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
i**c
发帖数: 26
8
你这个"很大的CHAR[]",言外之意要不能一次装入内存?

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
p******r
发帖数: 2999
9
利用bitmap,只需要一个char

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
j*****u
发帖数: 1133
10
允许额外空间不?
void RemoveDuplicates(ref T[] array)
{
var set = new HashSet();
int last = 0;
for (int i = 0; i < array.Length; ++i)
if (set.Add(array[i]))
array[last++] = array[i];
Array.Resize(ref array, last);
}

【在 n*******r 的大作中提到】
: 实在想不出in-place的方法。哪位提示我一下?
相关主题
为什么加个结束符leetcode就run time error呢?cc1501.3题,请帮忙测试下代码
回馈本版,贴GOOGLE电话面经leetcode: Remove Duplicates from Sorted Array
请教C/C++小【一个BB公司问的字母排序的问题】
进入JobHunting版参与讨论
j********x
发帖数: 2330
11
o了,就这么精妙

【在 p******r 的大作中提到】
: 利用bitmap,只需要一个char
f*****w
发帖数: 2602
12

为什么?

【在 p******r 的大作中提到】
: 利用bitmap,只需要一个char
g***s
发帖数: 3811
13
same idea with the question yesterday.
for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
which means it is not a duplicate, swap; otherwise(duplicate), clean.
for the rest, just check/clean.
after the above steps, compact all chars.
c*********t
发帖数: 2921
14
grass,
能不能讲讲你指的”the question yesterday“ 是哪个帖子?
你的方法能不能再讲的具体一点,好像没有领会你的要点。
谢谢!

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

m**q
发帖数: 189
15
grass 你这方法太牛了,最近在板上看到至少三四道可以用这个解的题了...

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

h*****g
发帖数: 312
16
NB!!!!

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

h*****g
发帖数: 312
17
咋搞?

【在 p******r 的大作中提到】
: 利用bitmap,只需要一个char
y***n
发帖数: 1594
18
why 256 ?

a[a[i]]!=a[i],

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

d*******l
发帖数: 338
19
这是哪个题啊,好象跟这个帖子讨论的不太一样?倒是跟我以前见过的有点像。。
这个帖子的题我觉得bitmap方法就很好了

【在 y***n 的大作中提到】
: why 256 ?
:
: a[a[i]]!=a[i],

g**e
发帖数: 6127
20
should check the first 65535 items?

【在 y***n 的大作中提到】
: why 256 ?
:
: a[a[i]]!=a[i],

相关主题
一道msft的题弱问,BST到底能不能有重复值?
leetcode里面的Recover Binary Search Tree怎么用O(1)space再问个简单的C问题
怎么理解递归解决的“swap every two elements in a linked list”?面试中举例常见的高效排序算法, 为什么都举quicksort和mergesort, 很少说heapsort呢
进入JobHunting版参与讨论
y***n
发帖数: 1594
21
Because Unicode?

【在 g**e 的大作中提到】
: should check the first 65535 items?
s**x
发帖数: 7506
22
没看懂。 only process first 256?
clean? clean what?
swap? what the loop start after swap? start next position or the current
position need to be checked again with the swapped char?

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

g**e
发帖数: 6127
23
yes

【在 y***n 的大作中提到】
: Because Unicode?
f****4
发帖数: 1359
24
这个才是正解;而不是256

【在 g**e 的大作中提到】
: should check the first 65535 items?
f****4
发帖数: 1359
25
1 10 100
下标越界

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

g***s
发帖数: 3811
26
hehe. i just gave the idea. I assumed char.length is large than 256.

【在 f****4 的大作中提到】
: 1 10 100
: 下标越界

p*****a
发帖数: 147
27
This seems not necessary. a bit map of size 256bits would do the simple
check of duplicate or not. (assuming ascii codes here).

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

y***n
发帖数: 1594
28
so 256 is non-unicode

【在 g**e 的大作中提到】
: yes
g***s
发帖数: 3811
29
i see. but 'in-place' in the question.

【在 p*****a 的大作中提到】
: This seems not necessary. a bit map of size 256bits would do the simple
: check of duplicate or not. (assuming ascii codes here).

e******0
发帖数: 211
30
a[a[i]] != a[i]
这没看懂啊
为这么这不是duplicate?

【在 g***s 的大作中提到】
: same idea with the question yesterday.
: for first 256 items, a[i], loop check the position a[i], if a[a[i]]!=a[i],
: which means it is not a duplicate, swap; otherwise(duplicate), clean.
: for the rest, just check/clean.
: after the above steps, compact all chars.

相关主题
amazon 第一轮电话面试Google实习第一轮电话面试总结
一个容易记忆的permutation算法ebay电面面经,攒人品,求好运
G家电面被拒,请帮助分析原因find first nonduplicate unicode questions
进入JobHunting版参与讨论
f****4
发帖数: 1359
31
那个idea只是在连续整数差一个,两个的情况下才比较合适
这里套的话不合适:题目没说char[]放的东西连续
并且要求in-place,那么用bitmap应该也是不合要求的(??)
如果能放进内存,可以用变形的quicksort来做
Partition好之后
a[0]x a[i+2]>x ..
. a[j]>x
交换 a[j]<=>a[i]; a[j-1]<=>a[i-1]...a[j-k+2]<=>a[i-k+2]
设置j=j-k+1 (重复的元素被交换到了数组尾部,并且不再参与下一次的递归)
继续
最后得到的数组即为无重复数组+尾部所有的重复元素
将重复元素清空即可

【在 g***s 的大作中提到】
: hehe. i just gave the idea. I assumed char.length is large than 256.
g***s
发帖数: 3811
32
你没有很好的理解。多考古。

a[i+2]>x ..

【在 f****4 的大作中提到】
: 那个idea只是在连续整数差一个,两个的情况下才比较合适
: 这里套的话不合适:题目没说char[]放的东西连续
: 并且要求in-place,那么用bitmap应该也是不合要求的(??)
: 如果能放进内存,可以用变形的quicksort来做
: Partition好之后
: a[0]x a[i+2]>x ..
: . a[j]>x
: 交换 a[j]<=>a[i]; a[j-1]<=>a[i-1]...a[j-k+2]<=>a[i-k+2]
: 设置j=j-k+1 (重复的元素被交换到了数组尾部,并且不再参与下一次的递归)
: 继续

f****4
发帖数: 1359
33
好像不用考古吧
那个a[a[i]] == a[i]的办法,只要给这么个例子就不work了
int a[]={200,1000,10000};

【在 g***s 的大作中提到】
: 你没有很好的理解。多考古。
:
: a[i+2]>x ..

f****4
发帖数: 1359
34
也不需要变形的quicksort这么复杂,直接quicksort后再走一遍即可
g***s
发帖数: 3811
35
1. char的范围是-127到127. 具体实现的时候转换成0到256
2. 我前面已经说了,假设length>255
3. 这个方法不是只能用在却一两个数的情况下
4. 我只是给个大概的方向,具体实现可以考古。

【在 f****4 的大作中提到】
: 好像不用考古吧
: 那个a[a[i]] == a[i]的办法,只要给这么个例子就不work了
: int a[]={200,1000,10000};

f****4
发帖数: 1359
36
-127~127是ascii编码,char* 支持utf8
a[a[i]]==a[i]的确支持int a[10001]={10,100,10000};但必须有假设a足够大>=10001
(这个面试的人能同意么?特别是我给的这个情况)
我想强调的是,这个题目很多限制不确定,a[a[i]]==a[i]不一定能套上

【在 g***s 的大作中提到】
: 1. char的范围是-127到127. 具体实现的时候转换成0到256
: 2. 我前面已经说了,假设length>255
: 3. 这个方法不是只能用在却一两个数的情况下
: 4. 我只是给个大概的方向,具体实现可以考古。

g***s
发帖数: 3811
37
题目都说了是char[]. 结果你改成int[]. 按你的理解,还要先把char[]里面多位合并
成一位来做?你要这么专我也没啥好说的了。

10001

【在 f****4 的大作中提到】
: -127~127是ascii编码,char* 支持utf8
: a[a[i]]==a[i]的确支持int a[10001]={10,100,10000};但必须有假设a足够大>=10001
: (这个面试的人能同意么?特别是我给的这个情况)
: 我想强调的是,这个题目很多限制不确定,a[a[i]]==a[i]不一定能套上

f****4
发帖数: 1359
38
我用int a[]举例是因为我不知道怎么输入utf8字符
我上面也说了,char *支持utf8,这个题目很多限制不确定,不是a[a[i]]==a[i]就一
定能套上去的

【在 g***s 的大作中提到】
: 题目都说了是char[]. 结果你改成int[]. 按你的理解,还要先把char[]里面多位合并
: 成一位来做?你要这么专我也没啥好说的了。
:
: 10001

g**e
发帖数: 6127
39
太nice啦,点到为止就行了

【在 g***s 的大作中提到】
: 题目都说了是char[]. 结果你改成int[]. 按你的理解,还要先把char[]里面多位合并
: 成一位来做?你要这么专我也没啥好说的了。
:
: 10001

1 (共1页)
进入JobHunting版参与讨论
相关主题
再问个简单的C问题string permutation,怎么处理重复字母?
面试中举例常见的高效排序算法, 为什么都举quicksort和mergesort, 很少说heapsort呢为什么加个结束符leetcode就run time error呢?
amazon 第一轮电话面试回馈本版,贴GOOGLE电话面经
一个容易记忆的permutation算法请教C/C++小
G家电面被拒,请帮助分析原因cc1501.3题,请帮忙测试下代码
Google实习第一轮电话面试总结leetcode: Remove Duplicates from Sorted Array
ebay电面面经,攒人品,求好运【一个BB公司问的字母排序的问题】
find first nonduplicate unicode questions一道msft的题
相关话题的讨论汇总
话题: char话题: array话题: 256话题: place话题: duplicate