Skip to content

Commit 266d9fa

Browse files
committed
add
1 parent d64e8f8 commit 266d9fa

File tree

13 files changed

+209
-30
lines changed

13 files changed

+209
-30
lines changed

Diff for: 1.2

301 Bytes
Binary file not shown.

Diff for: 1.2.cpp

+12-24
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,30 @@
22
#include <cstring>
33
using namespace std;
44

5-
void swap(char &a, char &b)
6-
{
5+
void swap(char &a, char &b){
76
a = a^b;
87
b = a^b;
98
a = a^b;
109
}
1110

12-
void reverse2(char *s)
13-
{
11+
void reverse2(char *s){
1412
int n = strlen(s);
1513
for(int i=0; i<n/2; ++i)
1614
swap(s[i], s[n-i-1]);
1715
}
1816

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-
}
17+
void reverse1(char *s){
18+
if(!s) return;
19+
char *p = s, *q = s;
20+
while(*q) ++q;
21+
--q;
22+
while(p < q)
23+
swap(*p++, *q--);
3524
}
36-
int main()
37-
{
25+
26+
int main(){
3827
char s[] = "1234567890";
39-
reverse2(s);
28+
reverse1(s);
4029
cout<<s<<endl;
4130
return 0;
42-
4331
}

Diff for: 1.4

-40 Bytes
Binary file not shown.

Diff for: 1.4.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ bool isAnagram(string s, string t){
1919
int len = s.length();
2020
int c[256];
2121
memset(c, 0, sizeof(c));
22-
for(int i=0; i<len; ++i)
22+
for(int i=0; i<len; ++i){
2323
++c[(int)s[i]];
24-
for(int i=0; i<len; ++i)
2524
--c[(int)t[i]];
25+
}
26+
2627
for(int i=0; i<256; ++i)
2728
if(c[i] != 0)
2829
return false;

Diff for: 1.7

482 Bytes
Binary file not shown.

Diff for: 1.7.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <iostream>
22
#include <cstdio>
3+
#include <cstring>
34
using namespace std;
45

56
void zero(int **a, int m, int n){
67
bool *row = new bool[m];
78
bool *col = new bool[n];
9+
memset(row, false, sizeof(row));
10+
memset(col, false, sizeof(col));
811
for(int i=0; i<m; ++i)
912
for(int j=0; j<n; ++j)
1013
if(a[i][j] == 0){

Diff for: 20.4

692 Bytes
Binary file not shown.

Diff for: 20.4.cpp

+50-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
#include <iostream>
22
using namespace std;
33

4+
int Count2(int n){
5+
int count = 0;
6+
while(n > 0){
7+
if(n%10 == 2)
8+
++count;
9+
n /= 10;
10+
}
11+
return count;
12+
}
13+
14+
int Count2s1(int n){
15+
int count = 0;
16+
for(int i=0; i<=n; ++i)
17+
count += Count2(i);
18+
return count;
19+
20+
}
21+
422
int Count2s(int n){
523
int count = 0;
624
int factor = 1;
725
int low = 0, cur = 0, high = 0;
826

927
while(n/factor != 0){
10-
low = n - (n/factor) * factor;
11-
cur = (n/factor) % 10;
12-
high = n / (factor*10);
28+
low = n - (n/factor) * factor;//低位数字
29+
cur = (n/factor) % 10;//当前位数字
30+
high = n / (factor*10);//高位数字
1331

1432
switch(cur){
1533
case 0:
@@ -29,8 +47,36 @@ int Count2s(int n){
2947

3048
return count;
3149
}
50+
51+
int Countis(int n, int i){
52+
if(i<1 || i>9) return -1;//i只能是1到9
53+
54+
int count = 0;
55+
int factor = 1;
56+
int low = 0, cur = 0, high = 0;
57+
58+
while(n/factor != 0){
59+
low = n - (n/factor) * factor;//低位数字
60+
cur = (n/factor) % 10;//当前位数字
61+
high = n / (factor*10);//高位数字
62+
63+
if(cur < i)
64+
count += high * factor;
65+
else if(cur == i)
66+
count += high * factor + low + 1;
67+
else
68+
count += (high + 1) * factor;
69+
70+
factor *= 10;
71+
}
72+
73+
return count;
74+
}
3275
int main(){
3376
for(int i=1; i<1000; ++i)
34-
cout<<i<<" "<<Count2s(i)<<endl;
77+
cout<<Count2s1(i)<<" "<<Count2s(i)<<" "<<Countis(i, 2)<<endl;
78+
// int n = 999;
79+
// for(int i=1; i<10; ++i)
80+
// cout<<Countis(n, i)<<endl;
3581
return 0;
3682
}

Diff for: 20.5

35.1 KB
Binary file not shown.

Diff for: 20.5.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
const int kMaxInt = ~(1<<31);
5+
6+
int ShortestDist(string text[], int n, string word1, string word2){
7+
int min = kMaxInt / 2;
8+
int pos1 = -min;
9+
int pos2 = -min;
10+
11+
for(int pos=0; pos<n; ++pos){
12+
if(text[pos] == word1){
13+
pos1 = pos;
14+
int dist = pos1 - pos2;
15+
if(dist < min)
16+
min = dist;
17+
}
18+
else if(text[pos] == word2){
19+
pos2 = pos;
20+
int dist = pos2 - pos1;
21+
if(dist < min)
22+
min = dist;
23+
}
24+
}
25+
26+
return min;
27+
}
28+
int main(){
29+
string text[] = {
30+
"What","is","your","name","My","name","is","Hawstein"
31+
};
32+
int len = 8;
33+
cout<<ShortestDist(text, len, "is", "name")<<endl;
34+
return 0;
35+
}

Diff for: 20.7

56.3 KB
Binary file not shown.

Diff for: 20.7.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include "hash.h"
4+
using namespace std;
5+
6+
Hash hash;
7+
8+
inline bool cmp(string s1, string s2){//按长度从大到小排
9+
return s2.length() < s1.length();
10+
}
11+
12+
bool MakeOfWords(string word, int length){
13+
//cout<<"curr: "<<word<<endl;
14+
int len = word.length();
15+
//cout<<"len:"<<len<<endl;
16+
if(len == 0) return true;
17+
18+
for(int i=1; i<=len; ++i){
19+
if(i == length) return false;//取到原始串,即自身
20+
string str = word.substr(0, i);
21+
//cout<<str<<endl;
22+
if(hash.find((char*)&str[0])){
23+
if(MakeOfWords(word.substr(i), length))
24+
return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
void PrintLongestWord(string word[], int n){
31+
for(int i=0; i<n; ++i)
32+
hash.insert((char*)&word[i][0]);
33+
sort(word, word+n, cmp);
34+
35+
for(int i=0; i<n; ++i){
36+
if(MakeOfWords(word[i], word[i].length())){
37+
cout<<"Longest Word: "<<word[i]<<endl;
38+
return;
39+
}
40+
}
41+
}
42+
43+
int main(){
44+
string arr[] = {"test", "tester", "testertest", "testing",
45+
"apple", "seattle", "banana", "batting", "ngcat",
46+
"batti","bat", "testingtester", "testbattingcat"};
47+
int len = 13;
48+
PrintLongestWord(arr, len);
49+
return 0;
50+
}

Diff for: hash.h

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <cstring>
2+
3+
const int kWordSize = 26 + 5;
4+
const int kNodeSize = 1200 + 5;
5+
const int kHashSize = 10001; //大质数
6+
7+
struct Node{
8+
char word[kWordSize];
9+
Node *next;
10+
};
11+
Node node[kNodeSize + 1];
12+
Node* head[kHashSize + 1];
13+
14+
class Hash{
15+
public:
16+
Hash();
17+
unsigned int hash(const char* str);
18+
void insert(const char* str);
19+
bool find(const char* str);
20+
private:
21+
unsigned int seed;
22+
unsigned int size;
23+
};
24+
25+
Hash::Hash():seed(131),size(0){
26+
memset(head, 0, sizeof(head));
27+
}
28+
29+
unsigned int Hash::hash(const char* str){
30+
unsigned int hash = 0;
31+
while(*str++)
32+
hash = hash * seed + (*str);
33+
return (hash & 0x7FFFFFFF) % kHashSize;
34+
}
35+
36+
void Hash::insert(const char* str){
37+
unsigned int id = hash(str);
38+
char *dst = (char*)node[size].word;
39+
while(*dst++ = *str++);
40+
node[size].next = head[id];
41+
head[id] = &node[size];
42+
++size;
43+
}
44+
45+
bool Hash::find(const char* str){
46+
unsigned int id = hash(str);
47+
for(Node* p=head[id]; p ; p=p->next){
48+
char *dst = (char*)p->word;
49+
int i = 0;
50+
while(*(str+i) && *(dst+i)==*(str+i))
51+
++i;
52+
if(*(str+i)=='\0' && *(dst+i)=='\0')
53+
return true;
54+
}
55+
return false;
56+
}

0 commit comments

Comments
 (0)