Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawstein committed Jan 26, 2013
0 parents commit 3e1649a
Show file tree
Hide file tree
Showing 64 changed files with 2,072 additions and 0 deletions.
Binary file added 1.1
Binary file not shown.
53 changes: 53 additions & 0 deletions 1.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
Binary file added 1.2
Binary file not shown.
43 changes: 43 additions & 0 deletions 1.2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <cstring>
using namespace std;

void swap(char &a, char &b)
{
a = a^b;
b = a^b;
a = a^b;
}

void reverse2(char *s)
{
int n = strlen(s);
for(int i=0; i<n/2; ++i)
swap(s[i], s[n-i-1]);
}

void reverse(char *s)
{
char *end = s;
char tmp;
if(s)
{
while(*end)
++end;
--end;
while(s < end)
{
tmp = *s;
*s++ = *end;
*end-- = tmp;
}
}
}
int main()
{
char s[] = "1234567890";
reverse2(s);
cout<<s<<endl;
return 0;

}
Binary file added 1.3
Binary file not shown.
118 changes: 118 additions & 0 deletions 1.3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <iostream>
#include <cstring>
using namespace std;

string removeDuplicate1(string s)
{
int check = 0;
int len = s.length();
if(len < 2) return s;
string str = "";
for(int i=0; i<len; ++i)
{
int v = (int)(s[i]-'a');
if((check & (1<<v)) == 0)
{
str += s[i];
check |= (1<<v);
}
}
return str;
}

string removeDuplicate2(string s)
{
int len = s.length();
if(len < 2) return s;
string str = "";
for(int i=0; i<len; ++i)
{
if(s[i] != '\0')
{
str += s[i];
for(int j=i+1; j<len; ++j)
if(s[j]==s[i])
s[j] = '\0';
}
}
return str;
}

void removeDuplicate3(char s[])
{
int len = strlen(s);
if(len < 2) return;
int p = 0;
for(int i=0; i<len; ++i)
{
if(s[i] != '\0')
{
s[p++] = s[i];
for(int j=i+1; j<len; ++j)
if(s[j]==s[i])
s[j] = '\0';
}
}
s[p] = '\0';
}

void removeDuplicate4(char s[])
{
int len = strlen(s);
if(len < 2) return;
bool c[256];
memset(c, 0, sizeof(c));
int p = 0;
for(int i=0; i<len; ++i)
{
if(!c[s[i]])
{
s[p++] = s[i];
c[s[i]] = true;
}
}
s[p] = '\0';
}

void removeDuplicate5(char s[])
{
int len = strlen(s);
if(len < 2) return;
int check = 0, p = 0;
for(int i=0; i<len; ++i)
{
int v = (int)(s[i]-'a');
if((check & (1<<v))==0)
{
s[p++] = s[i];
check |= (1<<v);
}
}
s[p] = '\0';
}
int main()
{
string s1 = "abcde";
string s2 = "aaabbb";
string s3 = "";
string s4 = "abababc";
string s5 = "ccccc";
cout<<removeDuplicate1(s1)<<" "<<removeDuplicate2(s1)<<endl;
cout<<removeDuplicate1(s2)<<" "<<removeDuplicate2(s2)<<endl;
cout<<removeDuplicate1(s3)<<" "<<removeDuplicate2(s3)<<endl;
cout<<removeDuplicate1(s4)<<" "<<removeDuplicate2(s4)<<endl;
cout<<removeDuplicate1(s5)<<" "<<removeDuplicate2(s5)<<endl;
char ss1[] = "abcde";
char ss2[] = "aaabbb";
char ss3[] = "";
char ss4[] = "abababc";
char ss5[] = "ccccc";
removeDuplicate5(ss1);
removeDuplicate5(ss2);
removeDuplicate5(ss3);
removeDuplicate5(ss4);
removeDuplicate5(ss5);
cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;
//cout<<removeDuplicate1(s6)<<" "<<removeDuplicate2(s6)<<endl;
return 0;
}
Binary file added 1.4
Binary file not shown.
36 changes: 36 additions & 0 deletions 1.4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

bool isAnagram1(string s, string t)
{
sort(&s[0], &s[0]+s.length());
sort(&t[0], &t[0]+t.length());
if(s == t) return true;
else return false;
}
bool isAnagram(string s, string t)
{
if(s=="" || t=="") return false;
if(s.length() != t.length()) return false;
int len = s.length();
int c[256];
memset(c, 0, sizeof(c));
for(int i=0; i<len; ++i)
++c[(int)s[i]];
for(int i=0; i<len; ++i)
--c[(int)t[i]];
for(int i=0; i<256; ++i)
if(c[i] != 0)
return false;
return true;
}
int main()
{
string s = "aaabbb";
string t = "ababab";
//cout<<isAnagram(s, t)<<endl;
cout<<isAnagram1(s, t)<<endl;
return 0;
}
Binary file added 1.5
Binary file not shown.
72 changes: 72 additions & 0 deletions 1.5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <iostream>
#include <cstring>
using namespace std;

char* replace1(char *c)
{
int len = strlen(c);
if(len == 0) return NULL;
int cnt = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
++cnt;
}
char *cc = new char[len+2*cnt];
int p = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
{
cc[p] = '%';
cc[p+1] = '2';
cc[p+2] = '0';
p += 3;
}
else
{
cc[p] = c[i];
++p;
}
}
cc[p] = '\0';
return cc;
}

void replace2(char *c)
{
int len = strlen(c);
if(len == 0) return;
int cnt = 0;
for(int i=0; i<len; ++i)
{
if(c[i] == ' ')
++cnt;
}
int p = len + 2*cnt;
c[p] = '\0';//the space must be allocated first.
--p;
for(int i=len-1; i>=0; --i)
{
if(c[i] == ' ')
{
c[p] = '0';
c[p-1] = '2';
c[p-2] = '%';
p -= 3;
}
else
{
c[p] = c[i];
--p;
}
}
}
int main()
{
char c[] = "i am hawstein.";
cout<<replace1(c)<<endl;
//replace2(c);
cout<<c<<endl;
return 0;
}
Binary file added 1.6
Binary file not shown.
41 changes: 41 additions & 0 deletions 1.6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
using namespace std;

void swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
void transpose(int a[][4], int n)
{
for(int i=0; i<n; ++i)
for(int j=i+1; j<n; ++j)
swap(a[i][j], a[j][i]);
for(int i=0; i<n/2; ++i)
for(int j=0; j<n; ++j)
swap(a[i][j], a[n-1-i][j]);
}
int main()
{
int a[4][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
for(int i=0; i<4; ++i)
{
for(int j=0; j<4; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
transpose(a, 4);
for(int i=0; i<4; ++i)
{
for(int j=0; j<4; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
Binary file added 1.7
Binary file not shown.
Loading

0 comments on commit 3e1649a

Please sign in to comment.