File tree Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Original file line number Diff line number Diff line change
1
+ int sumEvenGrandparent (TreeNode* root) {
2
+ return search (root);
3
+ }
4
+
5
+ int search (TreeNode* node)
6
+ {
7
+ if (node == nullptr )
8
+ return 0 ;
9
+ else if (node->val % 2 == 1 )
10
+ return search (node->left ) + search (node->right );
11
+ else
12
+ {
13
+ int sum = 0 ;
14
+ TreeNode* l1 = node->left ;
15
+ TreeNode* l2 = node->right ;
16
+ if (l1 != nullptr )
17
+ {
18
+ TreeNode* ll1 = l1->left ;
19
+ TreeNode* ll2 = l1->right ;
20
+ if (ll1 != nullptr )
21
+ sum += ll1->val ;
22
+ if (ll2 != nullptr )
23
+ sum += ll2->val ;
24
+ }
25
+ if (l2 != nullptr )
26
+ {
27
+ TreeNode* lr1 = l2->left ;
28
+ TreeNode* lr2 = l2->right ;
29
+ if (lr1 != nullptr )
30
+ sum += lr1->val ;
31
+ if (lr2 != nullptr )
32
+ sum += lr2->val ;
33
+ }
34
+
35
+ return sum + search (l1) + search (l2);
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ int countOdds (int low, int high) {
2
+ if (low%2 == 1 && high%2 == 1 )
3
+ return (high-low)/2 + 1 ;
4
+ else
5
+ return (high-low+1 )/2 ;
6
+ }
Original file line number Diff line number Diff line change
1
+ string makeGood (string s) {
2
+ int len = s.size ();
3
+ string res (len, ' ' );
4
+ int i = 0 , j = 0 ;
5
+ res[j] = s[i];
6
+ ++i;
7
+ ++j;
8
+ while (i < len)
9
+ {
10
+ if (j == 0 )
11
+ {
12
+ res[j++] = s[i++];
13
+ }
14
+ else if ((islower (res[j-1 ]) && isupper (s[i]) && res[j-1 ] == tolower (s[i]))
15
+ || (isupper (res[j-1 ]) && islower (s[i]) && res[j-1 ] == toupper (s[i])))
16
+ {
17
+ --j;
18
+ ++i;
19
+ }
20
+ else
21
+ {
22
+ res[j++] = s[i++];
23
+ }
24
+ }
25
+
26
+ return res.substr (0 , j);
27
+ }
Original file line number Diff line number Diff line change
1
+ bool canFormArray (vector<int >& arr, vector<vector<int >>& pieces) {
2
+ int len = arr.size ();
3
+ struct Elem
4
+ {
5
+ int pos;
6
+ int length;
7
+ Elem (int p = -1 , int l = 0 ): pos(p), length(l){}
8
+ };
9
+
10
+ int n = pieces.size ();
11
+ map<int , Elem> mp;
12
+ int sum = 0 ;
13
+ for (int i = 0 ; i < n; ++i)
14
+ {
15
+ int m = pieces[i].size ();
16
+ mp.insert (make_pair (pieces[i][0 ], Elem (i, m)));
17
+ sum += m;
18
+ }
19
+
20
+ if (len != sum)
21
+ return false ;
22
+
23
+ int i = 0 ;
24
+ while (i < len)
25
+ {
26
+ auto iter = mp.find (arr[i]);
27
+ if (iter == mp.end ())
28
+ return false ;
29
+ for (int k = 0 ; k < (iter->second ).length ; ++k)
30
+ {
31
+ if (arr[i] != pieces[(iter->second ).pos ][k])
32
+ return false ;
33
+ else
34
+ ++i;
35
+ }
36
+ }
37
+
38
+ return true ;
39
+ }
Original file line number Diff line number Diff line change
1
+ int countMatches (vector<vector<string>>& items, string ruleKey, string ruleValue) {
2
+ int len = items.size ();
3
+ int pos = -1 ;
4
+ if (ruleKey == " type" )
5
+ pos = 0 ;
6
+ else if (ruleKey == " color" )
7
+ pos = 1 ;
8
+ else
9
+ pos = 2 ;
10
+
11
+ int ct = 0 ;
12
+ for (int i = 0 ; i < len; ++i)
13
+ {
14
+ if (items[i][pos] == ruleValue)
15
+ ++ct;
16
+ }
17
+
18
+ return ct;
19
+ }
You can’t perform that action at this time.
0 commit comments