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 }
  • 相关阅读:
    前端页面实现报警器提示音效果
    Bootstrap相关优质项目学习清单
    Bootstrap相关优质项目学习清单
    [慕课笔记] node+mongodb建站攻略
    基础知识(11)- 异常、断言、日志和调试
    洛谷 P2580 于是他错误的点名开始了
    codevs 4189 字典
    HDU 1251 统计难题
    HDU 1827 Summer Holiday
    HDU 3836 Equivalent Sets
  • 原文地址:https://www.cnblogs.com/cane/p/3901239.html
Copyright © 2011-2022 走看看