Skip to content

Commit 3743a6c

Browse files
committed
add 102, 965, 1876
1 parent 2f3fbf5 commit 3743a6c

4 files changed

+109
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
102. Binary Tree Level Order Traversal
3+
4+
Submitted: January 13, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.24 MB (beats 14.20%)
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
vector<vector<int>> levelOrder(TreeNode* root) {
24+
if (root == nullptr) return {};
25+
vector<vector<int>> res;
26+
deque<TreeNode*> queue;
27+
queue.push_front(root);
28+
while (!queue.empty()) {
29+
vector<int> v;
30+
for (int i = 0, n = queue.size(); i < n; ++i) {
31+
TreeNode* p = queue.back();
32+
queue.pop_back();
33+
v.push_back(p->val);
34+
if (p->left != nullptr) queue.push_front(p->left);
35+
if (p->right != nullptr) queue.push_front(p->right);
36+
}
37+
res.push_back(v);
38+
}
39+
return res;
40+
}
41+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
1876. Substrings of Size Three with Distinct Characters
3+
4+
Submitted: January 13, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 8.37 MB (beats 73.21%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int countGoodSubstrings(string s) {
13+
if (s.size() < 3) return 0;
14+
// char arr[3];
15+
int res = 0;
16+
for (int i = 0, n = s.size() - 2; i < n; ++i) {
17+
res += (
18+
s[i] != s[i + 1] && s[i] != s[i + 2] &&
19+
s[i + 1] != s[i] && s[i + 1] != s[i + 2] &&
20+
s[i + 2] != s[i] && s[i + 2] != s[i + 1]
21+
);
22+
}
23+
return res;
24+
}
25+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
1876. Substrings of Size Three with Distinct Characters
3+
4+
Submitted: January 13, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.72 MB (beats 21.15%)
8+
"""
9+
10+
class Solution:
11+
def countGoodSubstrings(self, s: str) -> int:
12+
return sum(len(set(s[x:x+3])) == 3 for x in range(0, len(s) - 2))

965-univalued-binary-tree.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
965. Univalued Binary Tree
3+
4+
Submitted: January 13, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 12.69 MB (beats 29.98%)
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
19+
* };
20+
*/
21+
22+
class Solution {
23+
public:
24+
int val = -1;
25+
bool isUnivalTree(TreeNode* root) {
26+
if (root == nullptr) return true;
27+
if (val == -1) val = root->val;
28+
if (root->val != val) return false;
29+
return isUnivalTree(root->left) && isUnivalTree(root->right);
30+
}
31+
};

0 commit comments

Comments
 (0)