CareerCup-150 1.1

Career Cup 刷题活动开始..............

第一题:Implement an algorithm to determine if a string has all unique characters  What if you 
can not use additional data structures?

思想:基本思想还是使用哈希,对数组中的每个元素对应到哈希表中并查看这个元素是否已经存在了。这个题计较基本的,使用简单的bitmap就可以完成。对于复杂的情况就需要设计更好一点的哈希表了,相信后面的题目会遇到这种情况的。

如果不使用额外空间的话,可以先使用快速排序来进行排序,然后再扫描一遍检查是否有相同的元素。

#include <iostream>
#include <cstring>
using namespace std;

// To check whether all the characters in the array are unique.
bool isUniqueArray(char* array, int size){
    bool* bitmap = new bool[256+1];
    memset(bitmap, 0, sizeof(bool)*(size+1));
    for(int i=0; i<size; i++){
        int position = array[i];
        if( 0 == bitmap[position] ){
            bitmap[position] = 1;
        }else{
            return false;
        }
    }
    delete []bitmap;
    return true;
}

int main(){
    char list[] = "123456789abcdefg1";
    bool ret = isUniqueArray(list, strlen(list));
    if( ret ){
        cout<<"Unique"<<endl;
    }else{
        cout<<"No "<<endl;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值