Skip to content

Commit 3179289

Browse files
committed
5 problems solved.
1 parent 4119a41 commit 3179289

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

src/884.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
vector<string> uncommonFromSentences(string A, string B) {
2+
int len1 = A.size();
3+
int len2 = B.size();
4+
if (len1 == 0 && len2 == 0)
5+
return {};
6+
7+
map<string, int> mp;
8+
string::size_type i = A.find_first_not_of(" ");
9+
while (i != string::npos)
10+
{
11+
string::size_type pos = A.find(' ', i);
12+
if (pos != string::npos)
13+
{
14+
mp[A.substr(i, pos-i)]++;
15+
i = A.find_first_not_of(" ", pos+1);
16+
}
17+
else
18+
{
19+
mp[A.substr(i)]++;
20+
break;
21+
}
22+
}
23+
24+
i = B.find_first_not_of(" ");
25+
while (i != string::npos)
26+
{
27+
string::size_type pos = B.find(' ', i);
28+
if (pos != string::npos)
29+
{
30+
mp[B.substr(i, pos-i)]++;
31+
i = B.find_first_not_of(" ", pos+1);
32+
}
33+
else
34+
{
35+
mp[B.substr(i)]++;
36+
break;
37+
}
38+
}
39+
40+
vector<string> res;
41+
for (auto& m : mp)
42+
{
43+
if (m.second == 1)
44+
res.push_back(m.first);
45+
}
46+
47+
return res;
48+
}

src/893.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
int numSpecialEquivGroups(vector<string>& A) {
2+
int len = A.size();
3+
vector<vector<int>> vec(len, vector<int>(52, 0));
4+
for (int i = 0; i < len; ++i)
5+
{
6+
int sz = A[i].size();
7+
for (int j = 0; j < sz; ++j)
8+
{
9+
++vec[i][j%2*26+A[i][j]-'a'];
10+
}
11+
}
12+
13+
auto cmp = [](const vector<int>& a, const vector<int>& b) {
14+
for (int i = 0; i < 52; ++i)
15+
{
16+
if (a[i] == b[i])
17+
continue;
18+
else if (a[i] < b[i])
19+
return true;
20+
else
21+
return false;
22+
}
23+
return false;
24+
};
25+
26+
sort(vec.begin(), vec.end(), cmp);
27+
return unique(vec.begin(), vec.end()) - vec.begin();
28+
}

src/905.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
vector<int> sortArrayByParity(vector<int>& A) {
2+
int len = A.size();
3+
if (len <= 1)
4+
return A;
5+
int i = 0, j = len-1;
6+
while (i < j)
7+
{
8+
while (i < j && A[i]%2 == 0)
9+
++i;
10+
while (i < j && A[j]%2 == 1)
11+
--j;
12+
if (i < j)
13+
swap(A[i], A[j]);
14+
}
15+
16+
return A;
17+
}

src/908.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int smallestRangeI(vector<int>& A, int K) {
2+
int len = A.size();
3+
if (len <= 1)
4+
return 0;
5+
6+
sort(A.begin(), A.end());
7+
int diff = A[len-1] - A[0];
8+
return diff <= 2*K ? 0 : diff-2*K;
9+
}

src/929.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
int numUniqueEmails(vector<string>& emails) {
2+
int len = emails.size();
3+
if (len <= 1)
4+
return len;
5+
6+
set<string> uniqueEml;
7+
for (int i = 0; i < len; ++i)
8+
{
9+
int sz = emails[i].size();
10+
string res(sz, ' ');
11+
int j = 0, k = 0;
12+
while (j < sz && emails[i][j] != '@')
13+
{
14+
if (emails[i][j] == '+')
15+
break;
16+
else if (emails[i][j] != '.')
17+
res[k++] = emails[i][j];
18+
++j;
19+
}
20+
while (emails[i][j] != '@')
21+
++j;
22+
while (j < sz)
23+
{
24+
res[k] = emails[i][j];
25+
++j;
26+
++k;
27+
}
28+
uniqueEml.insert(res.substr(0, k));
29+
}
30+
31+
return uniqueEml.size();
32+
}

0 commit comments

Comments
 (0)