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 }
  • 相关阅读:
    IOS调试问题集
    sql常用语句及日期格式
    事件的简单面试题
    首博
    T-SQL查询进阶--详解公用表表达式(CTE)
    c++分布式计算类库
    MSSQL on Linux
    使用supervisor实现.NET Core程序后台运行
    CentOS下安装Nginx并安装服务实现自启动
    macOS安装MongoDB
  • 原文地址:https://www.cnblogs.com/cane/p/3901239.html
Copyright © 2011-2022 走看看