Skip to content

Commit 3e1649a

Browse files
committed
first commit
0 parents  commit 3e1649a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2072
-0
lines changed

1.1

34.8 KB
Binary file not shown.

1.1.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
bool isUnique1(string s)
6+
{
7+
bool a[256];
8+
memset(a, 0, sizeof(a));
9+
int len = s.length();
10+
for(int i=0; i<len; ++i)
11+
{
12+
int v = (int)s[i];
13+
if(a[v]) return false;
14+
a[v] = true;
15+
}
16+
return true;
17+
}
18+
19+
bool isUnique2(string s)
20+
{
21+
int a[8];
22+
memset(a, 0, sizeof(a));
23+
int len = s.length();
24+
for(int i=0; i<len; ++i)
25+
{
26+
int v = (int)s[i];
27+
int idx = v/32, shift=v%32;
28+
if(a[idx] & (1<<shift)) return false;
29+
a[idx] |= (1<<shift);
30+
}
31+
return true;
32+
}
33+
34+
bool isUnique3(string s)
35+
{
36+
int check = 0;
37+
int len = s.length();
38+
for(int i=0; i<len; ++i)
39+
{
40+
int v = (int)(s[i]-'a');
41+
if(check & (1<<v)) return false;
42+
check |= (1<<v);
43+
}
44+
return true;
45+
}
46+
int main()
47+
{
48+
string s1 = "i am hawstein.";
49+
string s2 = "abcdefghijklmnopqrstuvwxyz1234567890";
50+
cout<<isUnique1(s1)<<" "<<isUnique1(s2)<<endl;
51+
cout<<isUnique2(s1)<<" "<<isUnique2(s2)<<endl;
52+
return 0;
53+
}

1.2

17.9 KB
Binary file not shown.

1.2.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
void swap(char &a, char &b)
6+
{
7+
a = a^b;
8+
b = a^b;
9+
a = a^b;
10+
}
11+
12+
void reverse2(char *s)
13+
{
14+
int n = strlen(s);
15+
for(int i=0; i<n/2; ++i)
16+
swap(s[i], s[n-i-1]);
17+
}
18+
19+
void reverse(char *s)
20+
{
21+
char *end = s;
22+
char tmp;
23+
if(s)
24+
{
25+
while(*end)
26+
++end;
27+
--end;
28+
while(s < end)
29+
{
30+
tmp = *s;
31+
*s++ = *end;
32+
*end-- = tmp;
33+
}
34+
}
35+
}
36+
int main()
37+
{
38+
char s[] = "1234567890";
39+
reverse2(s);
40+
cout<<s<<endl;
41+
return 0;
42+
43+
}

1.3

36.4 KB
Binary file not shown.

1.3.cpp

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
string removeDuplicate1(string s)
6+
{
7+
int check = 0;
8+
int len = s.length();
9+
if(len < 2) return s;
10+
string str = "";
11+
for(int i=0; i<len; ++i)
12+
{
13+
int v = (int)(s[i]-'a');
14+
if((check & (1<<v)) == 0)
15+
{
16+
str += s[i];
17+
check |= (1<<v);
18+
}
19+
}
20+
return str;
21+
}
22+
23+
string removeDuplicate2(string s)
24+
{
25+
int len = s.length();
26+
if(len < 2) return s;
27+
string str = "";
28+
for(int i=0; i<len; ++i)
29+
{
30+
if(s[i] != '\0')
31+
{
32+
str += s[i];
33+
for(int j=i+1; j<len; ++j)
34+
if(s[j]==s[i])
35+
s[j] = '\0';
36+
}
37+
}
38+
return str;
39+
}
40+
41+
void removeDuplicate3(char s[])
42+
{
43+
int len = strlen(s);
44+
if(len < 2) return;
45+
int p = 0;
46+
for(int i=0; i<len; ++i)
47+
{
48+
if(s[i] != '\0')
49+
{
50+
s[p++] = s[i];
51+
for(int j=i+1; j<len; ++j)
52+
if(s[j]==s[i])
53+
s[j] = '\0';
54+
}
55+
}
56+
s[p] = '\0';
57+
}
58+
59+
void removeDuplicate4(char s[])
60+
{
61+
int len = strlen(s);
62+
if(len < 2) return;
63+
bool c[256];
64+
memset(c, 0, sizeof(c));
65+
int p = 0;
66+
for(int i=0; i<len; ++i)
67+
{
68+
if(!c[s[i]])
69+
{
70+
s[p++] = s[i];
71+
c[s[i]] = true;
72+
}
73+
}
74+
s[p] = '\0';
75+
}
76+
77+
void removeDuplicate5(char s[])
78+
{
79+
int len = strlen(s);
80+
if(len < 2) return;
81+
int check = 0, p = 0;
82+
for(int i=0; i<len; ++i)
83+
{
84+
int v = (int)(s[i]-'a');
85+
if((check & (1<<v))==0)
86+
{
87+
s[p++] = s[i];
88+
check |= (1<<v);
89+
}
90+
}
91+
s[p] = '\0';
92+
}
93+
int main()
94+
{
95+
string s1 = "abcde";
96+
string s2 = "aaabbb";
97+
string s3 = "";
98+
string s4 = "abababc";
99+
string s5 = "ccccc";
100+
cout<<removeDuplicate1(s1)<<" "<<removeDuplicate2(s1)<<endl;
101+
cout<<removeDuplicate1(s2)<<" "<<removeDuplicate2(s2)<<endl;
102+
cout<<removeDuplicate1(s3)<<" "<<removeDuplicate2(s3)<<endl;
103+
cout<<removeDuplicate1(s4)<<" "<<removeDuplicate2(s4)<<endl;
104+
cout<<removeDuplicate1(s5)<<" "<<removeDuplicate2(s5)<<endl;
105+
char ss1[] = "abcde";
106+
char ss2[] = "aaabbb";
107+
char ss3[] = "";
108+
char ss4[] = "abababc";
109+
char ss5[] = "ccccc";
110+
removeDuplicate5(ss1);
111+
removeDuplicate5(ss2);
112+
removeDuplicate5(ss3);
113+
removeDuplicate5(ss4);
114+
removeDuplicate5(ss5);
115+
cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;
116+
//cout<<removeDuplicate1(s6)<<" "<<removeDuplicate2(s6)<<endl;
117+
return 0;
118+
}

1.4

48 KB
Binary file not shown.

1.4.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <algorithm>
4+
using namespace std;
5+
6+
bool isAnagram1(string s, string t)
7+
{
8+
sort(&s[0], &s[0]+s.length());
9+
sort(&t[0], &t[0]+t.length());
10+
if(s == t) return true;
11+
else return false;
12+
}
13+
bool isAnagram(string s, string t)
14+
{
15+
if(s=="" || t=="") return false;
16+
if(s.length() != t.length()) return false;
17+
int len = s.length();
18+
int c[256];
19+
memset(c, 0, sizeof(c));
20+
for(int i=0; i<len; ++i)
21+
++c[(int)s[i]];
22+
for(int i=0; i<len; ++i)
23+
--c[(int)t[i]];
24+
for(int i=0; i<256; ++i)
25+
if(c[i] != 0)
26+
return false;
27+
return true;
28+
}
29+
int main()
30+
{
31+
string s = "aaabbb";
32+
string t = "ababab";
33+
//cout<<isAnagram(s, t)<<endl;
34+
cout<<isAnagram1(s, t)<<endl;
35+
return 0;
36+
}

1.5

17.9 KB
Binary file not shown.

1.5.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
char* replace1(char *c)
6+
{
7+
int len = strlen(c);
8+
if(len == 0) return NULL;
9+
int cnt = 0;
10+
for(int i=0; i<len; ++i)
11+
{
12+
if(c[i] == ' ')
13+
++cnt;
14+
}
15+
char *cc = new char[len+2*cnt];
16+
int p = 0;
17+
for(int i=0; i<len; ++i)
18+
{
19+
if(c[i] == ' ')
20+
{
21+
cc[p] = '%';
22+
cc[p+1] = '2';
23+
cc[p+2] = '0';
24+
p += 3;
25+
}
26+
else
27+
{
28+
cc[p] = c[i];
29+
++p;
30+
}
31+
}
32+
cc[p] = '\0';
33+
return cc;
34+
}
35+
36+
void replace2(char *c)
37+
{
38+
int len = strlen(c);
39+
if(len == 0) return;
40+
int cnt = 0;
41+
for(int i=0; i<len; ++i)
42+
{
43+
if(c[i] == ' ')
44+
++cnt;
45+
}
46+
int p = len + 2*cnt;
47+
c[p] = '\0';//the space must be allocated first.
48+
--p;
49+
for(int i=len-1; i>=0; --i)
50+
{
51+
if(c[i] == ' ')
52+
{
53+
c[p] = '0';
54+
c[p-1] = '2';
55+
c[p-2] = '%';
56+
p -= 3;
57+
}
58+
else
59+
{
60+
c[p] = c[i];
61+
--p;
62+
}
63+
}
64+
}
65+
int main()
66+
{
67+
char c[] = "i am hawstein.";
68+
cout<<replace1(c)<<endl;
69+
//replace2(c);
70+
cout<<c<<endl;
71+
return 0;
72+
}

1.6

17.5 KB
Binary file not shown.

1.6.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void swap(int &a, int &b)
5+
{
6+
int t = a;
7+
a = b;
8+
b = t;
9+
}
10+
void transpose(int a[][4], int n)
11+
{
12+
for(int i=0; i<n; ++i)
13+
for(int j=i+1; j<n; ++j)
14+
swap(a[i][j], a[j][i]);
15+
for(int i=0; i<n/2; ++i)
16+
for(int j=0; j<n; ++j)
17+
swap(a[i][j], a[n-1-i][j]);
18+
}
19+
int main()
20+
{
21+
int a[4][4] = {
22+
{1, 2, 3, 4},
23+
{5, 6, 7, 8},
24+
{9, 10, 11, 12},
25+
{13, 14, 15, 16}
26+
};
27+
for(int i=0; i<4; ++i)
28+
{
29+
for(int j=0; j<4; ++j)
30+
cout<<a[i][j]<<" ";
31+
cout<<endl;
32+
}
33+
transpose(a, 4);
34+
for(int i=0; i<4; ++i)
35+
{
36+
for(int j=0; j<4; ++j)
37+
cout<<a[i][j]<<" ";
38+
cout<<endl;
39+
}
40+
return 0;
41+
}

1.7

19.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)