首页 > 其他 > 详细

1.1

时间:2014-08-09 18:17:58      阅读:276      评论:0      收藏:0      [点我收藏+]

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 }

 

1.1,布布扣,bubuko.com

1.1

原文:http://www.cnblogs.com/cane/p/3901239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!