问题分析(问题描述见后面):
首先建立一个dup表,来记录duplicate item下一次出现的下标,如果没有下一相同的item,则记为0.
循环dup表:
当前位置i
如果没有duplicate值,直接写到输出tlist中.
有duplicate值:
寻找 head , tail下标值. head=i(当前循环到的位置), tail=min(head后的每一个没有duplicate 的item的下标,最后duplicate项的下标)
在 head---tail 这段区域中判断是否有比当前item更小的值:
是----舍弃当前值
否----输出到tlist中,将duplicate值标记为已经舍弃.
代码:





















































问题描述:
We have a sequence of integers, and we would like to remove all duplicate elements from this sequence. There may be multiple ways to perform this task. For example, given the sequence { 1, 2, 1, 3 }, we could end up with either { 1, 2, 3 } or { 2, 1, 3 } as the remaining sequence, depending on which duplicate 1 we remove from the original sequence. For this problem, we want to return the lexicographically first of of all possible remaining sequences. A sequence S1 comes before sequence S2 lexicographically if and only if S1 has a smaller value than S2 at the lowest index at which the two sequences differ (so, for example, { 1, 2, 3 } comes before { 2, 3, 1 }).
You will be given a int[] sequence. Return a int[] containing the sequence after all the duplicates are removed. See the examples for further clarification.
Definition
Class:
HardDuplicateRemover
Method:
process
Parameters:
int[]
Returns:
int[]
Method signature:
int[] process(int[] sequence)
(be sure your method is public)
Problem Statement
Constraints
-
sequence will have between 1 and 50 elements, inclusive.
-
Each element of sequence will be between 1 and 1000, inclusive.
Examples
0)
{5, 6, 5, 1, 6, 5}
Returns: {1, 6, 5 }
There are six different ways to remove duplicates (remaining numbers are marked by '*'):
{ *5, *6, 5, *1, 6, 5},
{ *5, 6, 5, *1, *6, 5},
{ 5, *6, *5, *1, 6, 5},
{ 5, 6, *5, *1, *6, 5},
{ 5, *6, 5, *1, 6, *5},
{ 5, 6, 5, *1, *6, *5}.
The last variant is the lexicographically first.
1)
{3, 2, 4, 2, 4, 4}
Returns: {3, 2, 4 }
2)
{6, 6, 6, 6, 6, 6}
Returns: {6 }
3)
{1, 3, 2, 4, 2, 3}
Returns: {1, 2, 4, 3 }
4)
{5, 4, 1, 5}
Returns: {4, 1, 5 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
题外语:对这个问题,很多人都给出了自己的算法,其中定有许多精妙处,可惜作者没有说明他的思路,百看不得其真意.我想贴代码无非是为了知识的共享,你要让别人很容易明白你在说什么,那么最好写一下伪代码或说明一下思路吧,毕竟没有人真正的会拷贝下你的代码一点点调试.