面试题

wind1373290 2009-07-15 09:53:53

int distinct(int[] A) {
//TODO
}
//A是一个整型数组,假如该数组为1,2,3,2,1,1
//要求返回不重复元素的个数,例如上面的数字为1,2,3;所以返回3
//题目不难,看谁的时间复杂度最优,效率最高
...全文
114 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlp999 2009-07-17
  • 打赏
  • 举报
回复

使用堆排序,复杂度是O(N) ;
每个元素插入堆中,最后返回堆的元素总数即可。
xxjjs 2009-07-17
  • 打赏
  • 举报
回复
用位图啊

int型共32位,每个数字分配两个bit,需要2^30,不过是1G的内存
扫描一遍就好,相应位为00表示没有出现,01表示出现一次,10表示出现两次或以上
只要找有几个01即可

复杂度是O(N)

不可能更小了
丈八涯 2009-07-16
  • 打赏
  • 举报
回复
使用java还非要不使用类库解决问题.
楼主要是想看hash实现就开个帖子直接要就可以了.
给你个c++版的,应该很容易看懂的.
是清华大学出版社的数据结构书上的
#ifndef __HASH_TABLE_H__
#define __HASH_TABLE_H__

// 散列表类
template <class ElemType, class KeyType>
class HashTable
{
protected:
// 散列表的的数据成员:
ElemType *ht; // 散列表
bool *empty; // 空元素
int m; // 散列表容量
int p; // 除留余数法的除数

// 辅助函数:
int H(KeyType key) const; // 散列函数
int Collision(KeyType key, int i) const; // 处理冲突的函数
bool SearchHelp(const KeyType &key, int &pos) const; // 查寻关键字为key的元素的位置

public:
// 二叉树方法声明及重载编译系统默认方法声明:
HashTable(int size, int divisor); // 构造函数
~HashTable(); // 析造函数
void Traverse(void (*Visit)(const ElemType &)) const; // 遍历散列表
bool Search(const KeyType &key, ElemType &e) const ; // 查寻关键字为key的元素的值
bool Insert(const ElemType &e); // 插入元素e
bool Delete(const KeyType &key); // 删除关键字为key的元素
HashTable(const HashTable<ElemType, KeyType> ©); // 复制构造函数
HashTable<ElemType, KeyType> &operator=
(const HashTable<ElemType, KeyType> ©); // 赋值语句重载
};

// 散列表类的实现部分
template <class ElemType, class KeyType>
int HashTable<ElemType, KeyType>::H(KeyType key) const
//操作结果: 返回散列地址
{
return key % p;
}

template <class ElemType, class KeyType>
int HashTable<ElemType, KeyType>::Collision(KeyType key, int i) const
//操作结果: 返回第i次冲突的探查地址
{
return (H(key) + i) % m;
}

template <class ElemType, class KeyType>
HashTable<ElemType, KeyType>::HashTable(int size, int divisor)
// 操作结果: 以size为散列表容量, divisor为除留余数法的除数构造一个空的散表表
{
m = size; // 赋值散列表容量
p = divisor; // 赋值除数
ht = new ElemType[m]; // 分配存储空间
empty = new bool[m]; // 分配存储空间

for (int pos = 0; pos < m; pos++)
{ // 将所有元素置空
empty[pos] = true;
}
}

template <class ElemType, class KeyType>
HashTable<ElemType, KeyType>::~HashTable()
// 操作结果: 销毁散列表
{
delete []ht; // 释放ht
delete []empty; // 释放empty
}

template <class ElemType, class KeyType>
void HashTable<ElemType, KeyType>::Traverse(void (*Visit)(const ElemType &)) const
// 操作结果: 依次对散列表的每个元素调用函数(*visit)
{
for (int pos = 0; pos < m; pos++)
{ // 对散列表的每个元素调用函数(*visit)
if (!empty[pos])
{ // 数据元素非空
(*Visit)(ht[pos]);
}
}
}

template <class ElemType, class KeyType>
bool HashTable<ElemType, KeyType>::SearchHelp(const KeyType &key, int &pos) const
// 操作结果: 查寻关键字为key的元素的位置,如果查找成功,返回true,并用pos指示待查数据
// 元素在散列表的位置,否则返回false
{
int c = 0; // 冲突次数
pos = H(key); // 散列表地址

while (c < m && // 冲突次数应小于m
!empty[pos] && // 元素ht[pos]非空
ht[pos] != key) // 关键字值不等
{
pos = Collision(key, ++c); //求得下一个探查地址
}

if (c >= m || empty[pos])
{ // 查找失败
return false;
}
else
{ // 查找成功
return true;
}
}

template <class ElemType, class KeyType>
bool HashTable<ElemType, KeyType>::Search(const KeyType &key, ElemType &e) const
// 操作结果: 查寻关键字为key的元素的值,如果查找成功,返回true,并用e返回元素的值,
// 否则返回false
{
int pos; // 元素的位置
if (SearchHelp(key, pos))
{ // 查找成功
e = ht[pos]; // 用e返回元素值
return true; // 返回true
}
else
{ // 查找失败
return false; // 返回false
}
}

template <class ElemType, class KeyType>
bool HashTable<ElemType, KeyType>::Insert(const ElemType &e)
// 操作结果: 在散列表中插入数据元素e,插入成功返回true,否则返回false
{
int pos; // 插入位置
if (!SearchHelp(e, pos) && empty[pos])
{ // 插入成功
ht[pos] = e; // 数据元素
empty[pos] = false; // 表示非空
return true;
}
else
{ // 插入失败
return false;
}
}

template <class ElemType, class KeyType>
bool HashTable<ElemType, KeyType>::Delete(const KeyType &key)
// 操作结果: 删除关键字为key的数据元素,删除成功返回true,否则返回false
{
int pos; // 数据元素位置
if (SearchHelp(key, pos))
{ // 删除成功
empty[pos] = true; // 表示元素为空
return true;
}
else
{ // 删除失败
return false;
}
}

template <class ElemType, class KeyType>
HashTable<ElemType, KeyType>::HashTable(const HashTable<ElemType, KeyType> ©)
// 操作结果:由散列表copy构造新散列表--复制构造函数
{
m = copy.m; // 散列表容量
p = copy.p; // 除留余数法的除数
ht = new ElemType[m]; // 分配存储空间
empty = new bool[m]; // 分配存储空间

for (int curPosition = 0; curPosition < m; curPosition++)
{ // 复制数据元素
ht[curPosition] = copy.ht[curPosition]; // 复制元素
empty[curPosition] = copy.empty[curPosition];// 复制元素是否为空值
}
}

template <class ElemType, class KeyType>
HashTable<ElemType, KeyType> &HashTable<ElemType, KeyType>::
operator=(const HashTable<ElemType, KeyType> ©)
// 操作结果:将散列表copy赋值给当前散列表--赋值语句重载
{
if (© != this)
{
delete []ht; // 释放当前散列表存储空间
m = copy.m; // 散列表容量
p = copy.p; // 除留余数法的除数
ht = new ElemType[m]; // 分配存储空间
empty = new bool[m]; // 分配存储空间

for (int curPosition = 0; curPosition < m; curPosition++)
{ // 复制数据元素
ht[curPosition] = copy.ht[curPosition]; // 复制元素
empty[curPosition] = copy.empty[curPosition];// 复制元素是否为空值
}
}
return *this;
}

#endif
绿色夹克衫 2009-07-16
  • 打赏
  • 举报
回复
找一个大一点的质数,比如99991,用这些数mod 99991的余数,作为hash值就可以了,
如果出现了hash碰撞,则在同一个桶内再做一次hash处理,比如求mod 99989的余数,如果还出现碰撞的话,那么就说明是同样的数,

除非两个数的差是99991 * 99989的倍数,否则是不会碰撞两次的

[Quote=引用 11 楼 wind1373290 的回复:]
谁能不用类库 根据题意写个哈希的实现撒, 期待高手。。。
[/Quote]
firePhoenix1981 2009-07-16
  • 打赏
  • 举报
回复
恩,1和2不是重复了吗?难道是连续的最大长度?
cantalou 2009-07-16
  • 打赏
  • 举报
回复
123
firePhoenix1981 2009-07-16
  • 打赏
  • 举报
回复
我怎么没有看懂题目?1 2 3 2 1 1 怎么就返回3呢?
ShowMan 2009-07-15
  • 打赏
  • 举报
回复
这个应该哈希最快了。。
fire_woods 2009-07-15
  • 打赏
  • 举报
回复
排序O(n*ln(n))
哈希O(N)
wind1373290 2009-07-15
  • 打赏
  • 举报
回复
不能使用类库
wind1373290 2009-07-15
  • 打赏
  • 举报
回复
谁能不用类库 根据题意写个哈希的实现撒, 期待高手。。。
bigbug9002 2009-07-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 shirdrn 的回复:]
因为Set里面不能包含重复的元素,将数组导入Set集合中,返回Set集合的大小即可。

Java code
int distinct(int[] A) {
Set<Integer> set = new HashSet<Integer>();
for(int i : A) {
set.add(i);
}
return set.size();
}
[/Quote]
这实际就是Hash
LeonTown 2009-07-15
  • 打赏
  • 举报
回复
关注
丈八涯 2009-07-15
  • 打赏
  • 举报
回复
为什么不使用类库呢?
难道有现成的hash还非要自己再去写?闲的啊!
千与 2009-07-15
  • 打赏
  • 举报
回复
因为Set里面不能包含重复的元素,将数组导入Set集合中,返回Set集合的大小即可。

int distinct(int[] A) {
Set<Integer> set = new HashSet<Integer>();
for(int i : A) {
set.add(i);
}
return set.size();
}

fallening 2009-07-15
  • 打赏
  • 举报
回复
sort + unique
showjim 2009-07-15
  • 打赏
  • 举报
回复
量小用hash(可以自己写hash函数),量大(元素总量超过位图初始化的开销)用位图

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧