zoukankan      html  css  js  c++  java
  • 1.1

    1.1 Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures? 

     1 #include <iostream>
     2 #include <queue>
     3 #include <climits>
     4 #include <algorithm>
     5 #include <memory.h>
     6 #include <stdio.h>
     7 #include <map>
     8 #include <vector>
     9 #include <list>
    10 #include <stack>
    11 using namespace std;
    12 
    13 ///判断一个字符串是否有重复字符出现
    14 ///corner case:字符串为null
    15 //思路1:如果字符串采用的是ascii编码的话,建立一个256大小的bool数组
    16 //初始设置为true,出现一次相应位置设置为false,如果碰到已经是false的了
    17 //就返回false;否则返回true
    18 //空间复杂度:O(1),时间复杂度:O(n)
    19 bool fun_1(string s)
    20 {
    21     bool isExisist[256];
    22     memset(isExisist,true,sizeof(isExisist));
    23     if(s.length() == 0)
    24         return false;
    25     int i;
    26     for(i = 0 ; i < s.length() ; ++i)
    27     {
    28         //cout<<(int)s[i]<<endl;
    29         if(isExisist[s[i]] == false)
    30             return false;
    31         isExisist[s[i]] = false;
    32     }
    33     return true;
    34 }
    35 //思路2:如果不能使用额外数据结构,那么可以直接暴力求解
    36 //或者如果可以打乱原始字符串顺序的话,可以先排序后遍历
    37 //实现:无
    38 
    39 int main(int argc, char *argv[])
    40 {
    41     string s = "aaa";
    42     cout<<fun_1(s)<<endl;
    43     return 0;
    44 }
  • 相关阅读:
    BZOJ1066: [SCOI2007]蜥蜴
    BZOJ1934: [Shoi2007]Vote 善意的投票
    BZOJ2321: [BeiJing2011集训]星器
    BZOJ1076: [SCOI2008]奖励关
    BZOJ1821: [JSOI2010]Group 部落划分 Group
    BZOJ3038: 上帝造题的七分钟2
    NOIP2017滚粗记
    BZOJ1087: [SCOI2005]互不侵犯King
    BZOJ1085: [SCOI2005]骑士精神
    BZOJ1295: [SCOI2009]最长距离
  • 原文地址:https://www.cnblogs.com/cane/p/3901239.html
Copyright © 2011-2022 走看看