Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2441c4f

Browse files
update solutions
1 parent e29a178 commit 2441c4f

25 files changed

+552
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Runtime: 268 ms
2+
// Memory Usage: 60.5 MB
3+
class Solution {
4+
public:
5+
void traverse(TreeNode* root, vector<int> &l) {
6+
if (!root) return;
7+
traverse(root->left, l);
8+
l.push_back(root->val);
9+
traverse(root->right, l);
10+
}
11+
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
12+
vector<int> l1, l2;
13+
traverse(root1, l1);
14+
traverse(root2, l2);
15+
vector<int> res;
16+
int i = 0, j = 0;
17+
while (i < l1.size() && j < l2.size()) {
18+
if (l1[i] < l2[j]) {
19+
res.push_back(l1[i++]);
20+
} else {
21+
res.push_back(l2[j++]);
22+
}
23+
}
24+
while (i < l1.size()) {
25+
res.push_back(l1[i++]);
26+
}
27+
while (j < l2.size()) {
28+
res.push_back(l2[j++]);
29+
}
30+
return res;
31+
}
32+
};

‎check-if-n-and-its-double-exist.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime: 4 ms
2+
// Memory Usage: 9.2 MB
3+
class Solution {
4+
public:
5+
bool checkIfExist(vector<int>& arr) {
6+
set<int> s;
7+
for (int a : arr) {
8+
if (s.find(a * 2) != s.end() || (a % 2 == 0 && s.find(a / 2) != s.end())) {
9+
return true;
10+
}
11+
s.insert(a);
12+
}
13+
return false;
14+
}
15+
};

‎contiguous-array.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Runtime: 176 ms
2+
// Memory Usage: 17.7 MB
3+
class Solution {
4+
public:
5+
int findMaxLength(vector<int>& nums) {
6+
int count = 0;
7+
int res = 0;
8+
unordered_map<int, int> mp;
9+
for (int i = 0; i < nums.size(); i++) {
10+
count += nums[i] == 0 ? -1 : 1;
11+
if (count == 0) {
12+
res = i + 1;
13+
} else if (mp.find(count) != mp.end()) {
14+
res = max(res, i - mp[count]);
15+
} else {
16+
mp[count] = i;
17+
}
18+
}
19+
return res;
20+
}
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Runtime: 0 ms
2+
// Memory Usage: 8.5 MB
3+
class Solution {
4+
public:
5+
vector<int> getNoZeroIntegers(int n) {
6+
vector<int> res;
7+
for (int i = 1; i < n; i++) {
8+
string s = to_string(i) + to_string(n - i);
9+
if (s.find('0') == string::npos) {
10+
res.push_back(n - i);
11+
res.push_back(i);
12+
return res;
13+
}
14+
}
15+
return res;
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Runtime: 20 ms
2+
// Memory Usage: 10.3 MB
3+
class Solution {
4+
public:
5+
int countNegatives(vector<vector<int>>& grid) {
6+
int res = 0;
7+
for (int i = grid.size() - 1; i >= 0 && grid[i][grid[i].size() - 1] < 0; i--) {
8+
for (int j = grid[i].size() - 1; j >= 0 && grid[i][j] < 0; j--) {
9+
res++;
10+
}
11+
}
12+
return res;
13+
}
14+
};

‎count-servers-that-communicate.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Runtime: 56 ms
2+
// Memory Usage: 15.1 MB
3+
class Solution {
4+
public:
5+
int countServers(vector<vector<int>>& grid) {
6+
vector<int> row(grid.size(), 0);
7+
vector<int> col(grid[0].size(), 0);
8+
9+
for (int i = 0; i < grid.size(); i++) {
10+
for (int j = 0; j < grid[0].size(); j++) {
11+
if (grid[i][j]) {
12+
row[i]++;
13+
col[j]++;
14+
}
15+
}
16+
}
17+
int res = 0;
18+
for (int i = 0; i < grid.size(); i++) {
19+
for (int j = 0; j < grid[i].size(); j++) {
20+
if (grid[i][j] && (row[i] > 1 || col[j] > 1)) {
21+
res++;
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Runtime: 0 ms
2+
// Memory Usage: 8.5 MB
3+
class Solution {
4+
public:
5+
string freqAlphabets(string s) {
6+
string res;
7+
for (int i = 0; i < s.size(); i++) {
8+
if (i + 2 < s.size() && s[i + 1] != '#' && s[i + 2] == '#') {
9+
res += 'a' - 1 + ((s[i++] - '0') * 10 + s[i++] - '0');
10+
} else {
11+
res += 'a' - 1 + (s[i] - '0');
12+
}
13+
}
14+
return res;
15+
}
16+
};

‎deepest-leaves-sum.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime: 48 ms
2+
// Memory Usage: 31.3 MB
3+
class Solution {
4+
public:
5+
void solve(TreeNode* root, int &res, int d, int &md) {
6+
if (!root) return;
7+
solve(root->left, res, d + 1, md);
8+
solve(root->right, res, d + 1, md);
9+
if (!root->left && !root->right) {
10+
if (d == md) {
11+
res += root->val;
12+
} else if (d > md) {
13+
res = root->val;
14+
}
15+
}
16+
md = max(d, md);
17+
}
18+
int deepestLeavesSum(TreeNode* root) {
19+
int res = 0;
20+
int md = 0;
21+
solve(root, res, 0, md);
22+
return res;
23+
}
24+
};

‎delete-leaves-with-a-given-value.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime: 20 ms
2+
// Memory Usage: 21.5 MB
3+
class Solution {
4+
public:
5+
TreeNode* removeLeafNodes(TreeNode* root, int target) {
6+
if (!root) return root;
7+
root->left = removeLeafNodes(root->left, target);
8+
root->right = removeLeafNodes(root->right, target);
9+
10+
if (!root->left && !root->right && root->val == target) {
11+
return NULL;
12+
}
13+
return root;
14+
}
15+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime: 52 ms
2+
// Memory Usage: 30.1 MB
3+
class FindElements {
4+
public:
5+
6+
set<int> st;
7+
8+
void solve(TreeNode* root, int val) {
9+
if (!root) return;
10+
11+
root->val = val;
12+
st.insert(val);
13+
solve(root->left, val * 2 + 1);
14+
solve(root->right, val * 2 + 2);
15+
}
16+
17+
FindElements(TreeNode* root) {
18+
solve(root, 0);
19+
}
20+
21+
bool find(int target) {
22+
return st.find(target) != st.end();
23+
}
24+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /