forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.1.cpp
53 lines (50 loc) · 1.02 KB
/
1.1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstring>
using namespace std;
bool isUnique1(string s)
{
bool a[256];
memset(a, 0, sizeof(a));
int len = s.length();
for(int i=0; i<len; ++i)
{
int v = (int)s[i];
if(a[v]) return false;
a[v] = true;
}
return true;
}
bool isUnique2(string s)
{
int a[8];
memset(a, 0, sizeof(a));
int len = s.length();
for(int i=0; i<len; ++i)
{
int v = (int)s[i];
int idx = v/32, shift=v%32;
if(a[idx] & (1<<shift)) return false;
a[idx] |= (1<<shift);
}
return true;
}
bool isUnique3(string s)
{
int check = 0;
int len = s.length();
for(int i=0; i<len; ++i)
{
int v = (int)(s[i]-'a');
if(check & (1<<v)) return false;
check |= (1<<v);
}
return true;
}
int main()
{
string s1 = "i am hawstein.";
string s2 = "abcdefghijklmnopqrstuvwxyz1234567890";
cout<<isUnique1(s1)<<" "<<isUnique1(s2)<<endl;
cout<<isUnique2(s1)<<" "<<isUnique2(s2)<<endl;
return 0;
}