十四周周赛 1022 感谢您的阅读,请指正出错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
static const auto __ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();

class {
public:
string removeOuterParentheses(string S) {
int left = 0;
int right = 0;
int pre = 0;
string ans = "";

for (int i = 0; i < S.length(); ++i) {
if (S[i] == '(') {
if (left > 0)
ans += S[i];
++left;
}
else if (S[i] == ')') {
++right;
if (right == left) {
left = 0;
right = 0;
} else {
ans += S[i];
}
}
}

return ans;
}
};

1022

Sum of Root To Leaf Binary Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class  {
public:
bool dfs(TreeNode* root,int last,int & sum){
if(!root){
return true;
}

last = ((last<<1) + root->val)%MOD;
if(!root->right&&!root->left){
sum = (sum + last)%MOD;
}else{
dfs(root->left,last,sum);
dfs(root->right,last,sum);
}

return true;
}

int sumRootToLeaf(TreeNode* root) {
int ans = 0;
int last = 0;
dfs(root,last,ans);

return ans;
}
};

to be 2ontinued
2019年4月7日 23点05分


感谢您的阅读,请指正出错误