Skip to content

Commit bf6f054

Browse files
committed
alter
1 parent eb482b9 commit bf6f054

15 files changed

+55
-52
lines changed

1.3.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,5 @@ int main()
113113
removeDuplicate5(ss4);
114114
removeDuplicate5(ss5);
115115
cout<<ss1<<" "<<ss2<<" "<<ss3<<" "<<ss4<<" "<<ss5<<endl;
116-
//cout<<removeDuplicate1(s6)<<" "<<removeDuplicate2(s6)<<endl;
117116
return 0;
118117
}

1.4

40 Bytes
Binary file not shown.

1.4.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
#include <algorithm>
44
using namespace std;
55

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+
810
sort(&s[0], &s[0]+s.length());
911
sort(&t[0], &t[0]+t.length());
1012
if(s == t) return true;
1113
else return false;
1214
}
13-
bool isAnagram(string s, string t)
14-
{
15+
bool isAnagram(string s, string t){
1516
if(s=="" || t=="") return false;
1617
if(s.length() != t.length()) return false;
18+
1719
int len = s.length();
1820
int c[256];
1921
memset(c, 0, sizeof(c));
@@ -31,6 +33,6 @@ int main()
3133
string s = "aaabbb";
3234
string t = "ababab";
3335
//cout<<isAnagram(s, t)<<endl;
34-
cout<<isAnagram1(s, t)<<endl;
36+
cout<<isAnagram(s, t)<<endl;
3537
return 0;
3638
}

1.5

8 Bytes
Binary file not shown.

1.5.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <cstring>
33
using namespace std;
44

5-
char* replace1(char *c)
6-
{
5+
char* replace1(char *c){
6+
if(c == NULL) return NULL;
77
int len = strlen(c);
88
if(len == 0) return NULL;
99
int cnt = 0;
@@ -12,7 +12,7 @@ char* replace1(char *c)
1212
if(c[i] == ' ')
1313
++cnt;
1414
}
15-
char *cc = new char[len+2*cnt];
15+
char *cc = new char[len+2*cnt+1];
1616
int p = 0;
1717
for(int i=0; i<len; ++i)
1818
{
@@ -33,8 +33,8 @@ char* replace1(char *c)
3333
return cc;
3434
}
3535

36-
void replace2(char *c)
37-
{
36+
void replace2(char *c){
37+
if(c == NULL) return;
3838
int len = strlen(c);
3939
if(len == 0) return;
4040
int cnt = 0;
@@ -44,8 +44,7 @@ void replace2(char *c)
4444
++cnt;
4545
}
4646
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.
4948
for(int i=len-1; i>=0; --i)
5049
{
5150
if(c[i] == ' ')
@@ -62,10 +61,9 @@ void replace2(char *c)
6261
}
6362
}
6463
}
65-
int main()
66-
{
64+
int main(){
6765
const int len = 100;
68-
char c[len] = "i am tom";
66+
char c[len] = "";
6967
cout<<replace1(c)<<endl;
7068
replace2(c);
7169
cout<<c<<endl;

1.6

4 Bytes
Binary file not shown.

1.6.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
11
#include <iostream>
22
using namespace std;
33

4-
void swap(int &a, int &b)
5-
{
4+
void swap(int &a, int &b){
65
int t = a;
76
a = b;
87
b = t;
98
}
10-
void transpose(int a[][4], int n)
11-
{
9+
void transpose(int a[][4], int n){
1210
for(int i=0; i<n; ++i)
1311
for(int j=i+1; j<n; ++j)
1412
swap(a[i][j], a[j][i]);
1513
for(int i=0; i<n/2; ++i)
1614
for(int j=0; j<n; ++j)
1715
swap(a[i][j], a[n-1-i][j]);
1816
}
19-
int main()
20-
{
17+
int main(){
2118
int a[4][4] = {
2219
{1, 2, 3, 4},
2320
{5, 6, 7, 8},
2421
{9, 10, 11, 12},
2522
{13, 14, 15, 16}
2623
};
27-
for(int i=0; i<4; ++i)
28-
{
24+
for(int i=0; i<4; ++i){
2925
for(int j=0; j<4; ++j)
3026
cout<<a[i][j]<<" ";
3127
cout<<endl;
3228
}
29+
cout<<endl;
3330
transpose(a, 4);
34-
for(int i=0; i<4; ++i)
35-
{
31+
for(int i=0; i<4; ++i){
3632
for(int j=0; j<4; ++j)
3733
cout<<a[i][j]<<" ";
3834
cout<<endl;

1.7

84 Bytes
Binary file not shown.

1.7.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
#include <cstdio>
33
using namespace std;
44

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];
98
for(int i=0; i<m; ++i)
109
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;
1513
}
1614
for(int i=0; i<m; ++i)
1715
for(int j=0; j<n; ++j)
18-
if(row[i]==1 || col[j]==1)
16+
if(row[i] || col[j])
1917
a[i][j] = 0;
2018
}
2119
int main()
@@ -31,9 +29,14 @@ int main()
3129
for(int i=0; i<m; ++i)
3230
for(int j=0; j<n; ++j)
3331
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;
3438
zero(a, m, n);
35-
for(int i=0; i<m; ++i)
36-
{
39+
for(int i=0; i<m; ++i){
3740
for(int j=0; j<n; ++j)
3841
cout<<a[i][j]<<" ";
3942
cout<<endl;

1.8

4.9 KB
Binary file not shown.

1.8.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
#include <string>
33
using namespace std;
44

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){
710
if(s1.length() != s2.length() || s1.length()<=0)
811
return false;
9-
s1 += s1;
10-
if(s1.find(s2) >= 0) return true;
11-
else return false;
12+
return isSubstring(s1+s1, s2);
1213
}
1314

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;
1920
return 0;
2021
}

13.1

-137 Bytes
Binary file not shown.

13.1.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ void printLastKLines(ifstream &fin, int k){
66
string line[k];
77
int lines = 0;
88
string tmp;
9-
while(getline(fin, tmp) && !fin.eof()){
9+
while(getline(fin, tmp)){//&& !fin.eof()
1010
line[lines%k] = tmp;
1111
++lines;
1212
}
13+
// while(!fin.eof()){
14+
// getline(fin, line[lines%k]);
15+
// ++lines;
16+
// }
1317
int start, cnt;
1418
if(lines < k){
1519
start = 0;

2.4

24 Bytes
Binary file not shown.

2.4.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ node* addlink(node *p, node *q){
3838
c = t/10;
3939
p = p->next; q = q->next;
4040
}
41-
if(p){
41+
while(p){
4242
int t = p->data + c;
4343
node *r = new node();
4444
r->data = t%10;
@@ -47,7 +47,7 @@ node* addlink(node *p, node *q){
4747
c = t/10;
4848
p = p->next;
4949
}
50-
if(q){
50+
while(q){
5151
int t = q->data + c;
5252
node *r = new node();
5353
r->data = t%10;
@@ -73,9 +73,9 @@ void print(node *head){
7373
}
7474

7575
int main(){
76-
int n = 4;
76+
int n = 5;
7777
int a[] = {
78-
1, 2, 9, 3
78+
1, 2, 9, 9, 3
7979
};
8080
int m = 3;
8181
int b[] = {

0 commit comments

Comments
 (0)