Skip to content

Commit 7b965b8

Browse files
committed
9 problems solved.
1 parent ac29fb9 commit 7b965b8

File tree

9 files changed

+215
-0
lines changed

9 files changed

+215
-0
lines changed

src/1475.cpp

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

src/1572.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int diagonalSum(vector<vector<int>>& mat) {
2+
int row = mat.size();
3+
int sum = 0;
4+
for (int i = 0; i < row; ++i)
5+
sum += mat[i][i] + mat[row-1-i][i];
6+
7+
if (row%2 == 1)
8+
sum -= mat[row/2][row/2];
9+
return sum;
10+
}

src/1662.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
2+
int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
3+
int len1 = word1.size();
4+
int len2 = word2.size();
5+
6+
int sz1 = word1[0].size();
7+
int sz2 = word2[0].size();
8+
while (i1 < len1 && i2 < len2)
9+
{
10+
while (j1 < sz1 && j2 < sz2)
11+
{
12+
if (word1[i1][j1] != word2[i2][j2])
13+
return false;
14+
++j1;
15+
++j2;
16+
}
17+
if (j1 == sz1)
18+
{
19+
++i1;
20+
if (i1 < len1)
21+
{
22+
sz1 = word1[i1].size();
23+
j1 = 0;
24+
}
25+
}
26+
if (j2 == sz2)
27+
{
28+
++i2;
29+
if (i2 < len2)
30+
{
31+
sz2 = word2[i2].size();
32+
j2 = 0;
33+
}
34+
}
35+
}
36+
37+
return (i1 == len1 && i2 == len2);
38+
}

src/1672.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
int maximumWealth(vector<vector<int>>& accounts) {
2+
int row = accounts.size();
3+
if (row == 0)
4+
return 0;
5+
int col = accounts[0].size();
6+
int maxwealth = 0;
7+
for (int i = 0; i < row; ++i)
8+
{
9+
int wealth = accumulate(accounts[i].begin(), accounts[i].end(), 0);
10+
if (wealth >maxwealth)
11+
maxwealth = wealth;
12+
}
13+
14+
return maxwealth;
15+
}

src/1678.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
string interpret(string command) {
2+
stringstream ss;
3+
int len = command.size();
4+
int i = 0;
5+
while (i < len)
6+
{
7+
if (command[i] == 'G')
8+
{
9+
ss << 'G';
10+
++i;
11+
}
12+
else if (command[i] == '(')
13+
{
14+
if (command[i+1] == ')')
15+
{
16+
ss << 'o';
17+
i += 2;
18+
}
19+
else
20+
{
21+
ss << "al";
22+
i += 4;
23+
}
24+
}
25+
}
26+
27+
return ss.str();
28+
}

src/1684.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int countConsistentStrings(string allowed, vector<string>& words) {
2+
int flag[26] = {0};
3+
int len = allowed.size();
4+
for (int i = 0; i < len; ++i)
5+
flag[allowed[i]-'a'] = 1;
6+
7+
int sz = words.size();
8+
int ct = 0;
9+
for (int i = 0; i < sz; ++i)
10+
{
11+
int n = words[i].size();
12+
int j = 0;
13+
for (; j < n; ++j)
14+
{
15+
if (flag[words[i][j]-'a'] == 0)
16+
break;
17+
}
18+
if (j == n)
19+
++ct;
20+
}
21+
22+
return ct;
23+
}

src/1688.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int numberOfMatches(int n) {
2+
return n-1;
3+
}

src/1694.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
string reformatNumber(string number) {
2+
int len = number.size();
3+
if (len == 2)
4+
return number;
5+
6+
string str(len, ' ');
7+
int j = 0;
8+
for (int i = 0; i < len; ++i)
9+
{
10+
if (number[i] != ' ' && number[i] != '-')
11+
str[j++] = number[i];
12+
}
13+
14+
if (j <= 3)
15+
return str.substr(0, j);
16+
17+
int r = j%3;
18+
int lim = r == 0 ? j : (r == 1 ? j-4 : j-2);
19+
int n = 0;
20+
if (r == 0)
21+
n = j+j/3-1;
22+
else if (r == 1)
23+
n = j-4+(j-4)/3-1+6;
24+
else
25+
n = j-2+(j-2)/3-1+3;
26+
27+
string res(n, ' ');
28+
int i = 0, k = 0;
29+
while (i < lim)
30+
{
31+
res[k++] = str[i++];
32+
if (k < n && i%3 == 0)
33+
res[k++] = '-';
34+
}
35+
36+
if (r == 1)
37+
{
38+
res[k] = str[i];
39+
res[k+1] = str[i+1];
40+
res[k+2] = '-';
41+
res[k+3] = str[i+2];
42+
res[k+4] = str[i+3];
43+
}
44+
else if (r == 2)
45+
{
46+
res[k] = str[i];
47+
res[k+1] = str[i+1];
48+
}
49+
50+
return res;
51+
}

src/1704.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
bool halvesAreAlike(string s) {
2+
int len = s.size();
3+
int n1 = 0, n2 = 0;
4+
for (int i = 0; i < len/2; ++i)
5+
{
6+
switch(tolower(s[i]))
7+
{
8+
case 'a':
9+
case 'e':
10+
case 'i':
11+
case 'o':
12+
case 'u':
13+
++n1;
14+
}
15+
}
16+
for (int i = len/2; i < len; ++i)
17+
{
18+
switch(tolower(s[i]))
19+
{
20+
case 'a':
21+
case 'e':
22+
case 'i':
23+
case 'o':
24+
case 'u':
25+
++n2;
26+
}
27+
}
28+
29+
return n1 == n2;
30+
}

0 commit comments

Comments
 (0)