Skip to content

Commit b4d2fdc

Browse files
committed
5 problems solved.
1 parent 55a13cb commit b4d2fdc

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

src/108.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
TreeNode* sortedArrayToBST(vector<int>& nums) {
2+
int len = nums.size();
3+
return generate(nums, 0, len-1);
4+
}
5+
6+
TreeNode* generate(vector<int>& nums, int p, int q)
7+
{
8+
TreeNode* node = nullptr;
9+
if (p <= q)
10+
{
11+
int lim = (p+q)/2;
12+
node = new TreeNode(nums[lim]);
13+
node->left = generate(nums, p, lim-1);
14+
node->right = generate(nums, lim+1, q);
15+
}
16+
17+
return node;
18+
}

src/2124.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
bool checkString(string s) {
2+
int len = s.size();
3+
int i = 0;
4+
while (i < len && s[i] == 'a')
5+
++i;
6+
if (i >= len-1)
7+
return true;
8+
for (; i < len; ++i)
9+
{
10+
if (s[i] == 'a')
11+
return false;
12+
}
13+
14+
return true;
15+
}

src/2125.cpp

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

src/2129.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
string capitalizeTitle(string title) {
2+
int len = title.size();
3+
int i = 0, pos = 0;
4+
int n = 0;
5+
while (i < len)
6+
{
7+
if (title[i] != ' ')
8+
{
9+
title[i] = tolower(title[i]);
10+
++n;
11+
}
12+
else
13+
{
14+
if (n > 2)
15+
title[pos] = toupper(title[pos]);
16+
n = 0;
17+
pos = i+1;
18+
}
19+
++i;
20+
}
21+
22+
if (n > 2)
23+
title[pos] = toupper(title[pos]);
24+
25+
return title;
26+
}

src/2133.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
bool checkValid(vector<vector<int>>& matrix) {
2+
int n = matrix.size();
3+
vector<int> flag(n, 0);
4+
for (int i = 0; i < n; ++i)
5+
{
6+
flag.assign(n, 0);
7+
int ct = 0;
8+
for (int j = 0; j < n; ++j)
9+
{
10+
if (flag[matrix[i][j]-1] == 0)
11+
{
12+
flag[matrix[i][j]-1] = 1;
13+
++ct;
14+
}
15+
}
16+
if (ct != n)
17+
return false;
18+
}
19+
20+
for (int i = 0; i < n; ++i)
21+
{
22+
flag.assign(n, 0);
23+
int ct = 0;
24+
for (int j = 0; j < n; ++j)
25+
{
26+
if (flag[matrix[j][i]-1] == 0)
27+
{
28+
flag[matrix[j][i]-1] = 1;
29+
++ct;
30+
}
31+
}
32+
if (ct != n)
33+
return false;
34+
}
35+
36+
return true;
37+
}

0 commit comments

Comments
 (0)