File tree 15 files changed +55
-52
lines changed
15 files changed +55
-52
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,5 @@ int main()
113
113
removeDuplicate5 (ss4);
114
114
removeDuplicate5 (ss5);
115
115
cout<<ss1<<" " <<ss2<<" " <<ss3<<" " <<ss4<<" " <<ss5<<endl;
116
- // cout<<removeDuplicate1(s6)<<" "<<removeDuplicate2(s6)<<endl;
117
116
return 0 ;
118
117
}
Original file line number Diff line number Diff line change 3
3
#include < algorithm>
4
4
using namespace std ;
5
5
6
- bool isAnagram1 (string s, string t)
7
- {
6
+ bool isAnagram1 (string s, string t){
7
+ if (s==" " || t==" " ) return false ;
8
+ if (s.length () != t.length ()) return false ;
9
+
8
10
sort (&s[0 ], &s[0 ]+s.length ());
9
11
sort (&t[0 ], &t[0 ]+t.length ());
10
12
if (s == t) return true ;
11
13
else return false ;
12
14
}
13
- bool isAnagram (string s, string t)
14
- {
15
+ bool isAnagram (string s, string t){
15
16
if (s==" " || t==" " ) return false ;
16
17
if (s.length () != t.length ()) return false ;
18
+
17
19
int len = s.length ();
18
20
int c[256 ];
19
21
memset (c, 0 , sizeof (c));
@@ -31,6 +33,6 @@ int main()
31
33
string s = " aaabbb" ;
32
34
string t = " ababab" ;
33
35
// cout<<isAnagram(s, t)<<endl;
34
- cout<<isAnagram1 (s, t)<<endl;
36
+ cout<<isAnagram (s, t)<<endl;
35
37
return 0 ;
36
38
}
Original file line number Diff line number Diff line change 2
2
#include < cstring>
3
3
using namespace std ;
4
4
5
- char * replace1 (char *c)
6
- {
5
+ char * replace1 (char *c){
6
+ if (c == NULL ) return NULL ;
7
7
int len = strlen (c);
8
8
if (len == 0 ) return NULL ;
9
9
int cnt = 0 ;
@@ -12,7 +12,7 @@ char* replace1(char *c)
12
12
if (c[i] == ' ' )
13
13
++cnt;
14
14
}
15
- char *cc = new char [len+2 *cnt];
15
+ char *cc = new char [len+2 *cnt+ 1 ];
16
16
int p = 0 ;
17
17
for (int i=0 ; i<len; ++i)
18
18
{
@@ -33,8 +33,8 @@ char* replace1(char *c)
33
33
return cc;
34
34
}
35
35
36
- void replace2 (char *c)
37
- {
36
+ void replace2 (char *c){
37
+ if (c == NULL ) return ;
38
38
int len = strlen (c);
39
39
if (len == 0 ) return ;
40
40
int cnt = 0 ;
@@ -44,8 +44,7 @@ void replace2(char *c)
44
44
++cnt;
45
45
}
46
46
int p = len + 2 *cnt;
47
- c[p] = ' \0 ' ;// the space must be allocated first.
48
- --p;
47
+ c[p--] = ' \0 ' ;// the space must be allocated first.
49
48
for (int i=len-1 ; i>=0 ; --i)
50
49
{
51
50
if (c[i] == ' ' )
@@ -62,10 +61,9 @@ void replace2(char *c)
62
61
}
63
62
}
64
63
}
65
- int main ()
66
- {
64
+ int main (){
67
65
const int len = 100 ;
68
- char c[len] = " i am tom " ;
66
+ char c[len] = " " ;
69
67
cout<<replace1 (c)<<endl;
70
68
replace2 (c);
71
69
cout<<c<<endl;
Original file line number Diff line number Diff line change 1
1
#include < iostream>
2
2
using namespace std ;
3
3
4
- void swap (int &a, int &b)
5
- {
4
+ void swap (int &a, int &b){
6
5
int t = a;
7
6
a = b;
8
7
b = t;
9
8
}
10
- void transpose (int a[][4 ], int n)
11
- {
9
+ void transpose (int a[][4 ], int n){
12
10
for (int i=0 ; i<n; ++i)
13
11
for (int j=i+1 ; j<n; ++j)
14
12
swap (a[i][j], a[j][i]);
15
13
for (int i=0 ; i<n/2 ; ++i)
16
14
for (int j=0 ; j<n; ++j)
17
15
swap (a[i][j], a[n-1 -i][j]);
18
16
}
19
- int main ()
20
- {
17
+ int main (){
21
18
int a[4 ][4 ] = {
22
19
{1 , 2 , 3 , 4 },
23
20
{5 , 6 , 7 , 8 },
24
21
{9 , 10 , 11 , 12 },
25
22
{13 , 14 , 15 , 16 }
26
23
};
27
- for (int i=0 ; i<4 ; ++i)
28
- {
24
+ for (int i=0 ; i<4 ; ++i){
29
25
for (int j=0 ; j<4 ; ++j)
30
26
cout<<a[i][j]<<" " ;
31
27
cout<<endl;
32
28
}
29
+ cout<<endl;
33
30
transpose (a, 4 );
34
- for (int i=0 ; i<4 ; ++i)
35
- {
31
+ for (int i=0 ; i<4 ; ++i){
36
32
for (int j=0 ; j<4 ; ++j)
37
33
cout<<a[i][j]<<" " ;
38
34
cout<<endl;
Original file line number Diff line number Diff line change 2
2
#include < cstdio>
3
3
using namespace std ;
4
4
5
- void zero (int **a, int m, int n)
6
- {
7
- int *row = new int [m];
8
- int *col = new int [n];
5
+ void zero (int **a, int m, int n){
6
+ bool *row = new bool [m];
7
+ bool *col = new bool [n];
9
8
for (int i=0 ; i<m; ++i)
10
9
for (int j=0 ; j<n; ++j)
11
- if (a[i][j] == 0 )
12
- {
13
- row[i] = 1 ;
14
- col[j] = 1 ;
10
+ if (a[i][j] == 0 ){
11
+ row[i] = true ;
12
+ col[j] = true ;
15
13
}
16
14
for (int i=0 ; i<m; ++i)
17
15
for (int j=0 ; j<n; ++j)
18
- if (row[i]== 1 || col[j]== 1 )
16
+ if (row[i] || col[j])
19
17
a[i][j] = 0 ;
20
18
}
21
19
int main ()
@@ -31,9 +29,14 @@ int main()
31
29
for (int i=0 ; i<m; ++i)
32
30
for (int j=0 ; j<n; ++j)
33
31
cin>>a[i][j];
32
+ for (int i=0 ; i<m; ++i){
33
+ for (int j=0 ; j<n; ++j)
34
+ cout<<a[i][j]<<" " ;
35
+ cout<<endl;
36
+ }
37
+ cout<<endl;
34
38
zero (a, m, n);
35
- for (int i=0 ; i<m; ++i)
36
- {
39
+ for (int i=0 ; i<m; ++i){
37
40
for (int j=0 ; j<n; ++j)
38
41
cout<<a[i][j]<<" " ;
39
42
cout<<endl;
Original file line number Diff line number Diff line change 2
2
#include < string>
3
3
using namespace std ;
4
4
5
- bool isRotation (string s1, string s2)
6
- {
5
+ bool isSubstring (string s1, string s2){
6
+ if (s1.find (s2) != string::npos) return true ;
7
+ else return false ;
8
+ }
9
+ bool isRotation (string s1, string s2){
7
10
if (s1.length () != s2.length () || s1.length ()<=0 )
8
11
return false ;
9
- s1 += s1;
10
- if (s1.find (s2) >= 0 ) return true ;
11
- else return false ;
12
+ return isSubstring (s1+s1, s2);
12
13
}
13
14
14
- int main ()
15
- {
16
- string s1 = " a " ;
17
- string s2 = " a " ;
18
- cout<<isRotation (s1, s2) ;
15
+ int main (){
16
+ string s1 = " apple " ;
17
+ string s2 = " pleap " ;
18
+ cout<< isRotation (s1, s2)<<endl ;
19
+ // cout<<string::npos<<endl ;
19
20
return 0 ;
20
21
}
Original file line number Diff line number Diff line change @@ -6,10 +6,14 @@ void printLastKLines(ifstream &fin, int k){
6
6
string line[k];
7
7
int lines = 0 ;
8
8
string tmp;
9
- while (getline (fin, tmp) && !fin.eof ()){
9
+ while (getline (fin, tmp)){ // && !fin.eof()
10
10
line[lines%k] = tmp;
11
11
++lines;
12
12
}
13
+ // while(!fin.eof()){
14
+ // getline(fin, line[lines%k]);
15
+ // ++lines;
16
+ // }
13
17
int start, cnt;
14
18
if (lines < k){
15
19
start = 0 ;
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ node* addlink(node *p, node *q){
38
38
c = t/10 ;
39
39
p = p->next ; q = q->next ;
40
40
}
41
- if (p){
41
+ while (p){
42
42
int t = p->data + c;
43
43
node *r = new node ();
44
44
r->data = t%10 ;
@@ -47,7 +47,7 @@ node* addlink(node *p, node *q){
47
47
c = t/10 ;
48
48
p = p->next ;
49
49
}
50
- if (q){
50
+ while (q){
51
51
int t = q->data + c;
52
52
node *r = new node ();
53
53
r->data = t%10 ;
@@ -73,9 +73,9 @@ void print(node *head){
73
73
}
74
74
75
75
int main (){
76
- int n = 4 ;
76
+ int n = 5 ;
77
77
int a[] = {
78
- 1 , 2 , 9 , 3
78
+ 1 , 2 , 9 , 9 , 3
79
79
};
80
80
int m = 3 ;
81
81
int b[] = {
You can’t perform that action at this time.
0 commit comments