File tree Expand file tree Collapse file tree 8 files changed +176
-0
lines changed Expand file tree Collapse file tree 8 files changed +176
-0
lines changed Original file line number Diff line number Diff line change
1
+ int countEven (int num) {
2
+ int ct = 0 ;
3
+ for (int i = 1 ; i <= num; ++i)
4
+ {
5
+ int n = i;
6
+ int sum = 0 ;
7
+ while (n > 0 )
8
+ {
9
+ sum += n%10 ;
10
+ n /= 10 ;
11
+ }
12
+ if (sum%2 == 0 )
13
+ ++ct;
14
+ }
15
+
16
+ return ct;
17
+ }
Original file line number Diff line number Diff line change
1
+ TreeNode* createBinaryTree (vector<vector<int >>& descriptions) {
2
+ map<int , int > nodeflag;
3
+ map<int , pair<int ,int >> nodepos;
4
+ int len = descriptions.size ();
5
+
6
+ for (int i = 0 ; i < len; ++i)
7
+ {
8
+ nodeflag[descriptions[i][0 ]] |= 1 ;
9
+ nodeflag[descriptions[i][1 ]] |= 2 ;
10
+ if (descriptions[i][2 ] == 1 )
11
+ nodepos[descriptions[i][0 ]].first = descriptions[i][1 ];
12
+ else
13
+ nodepos[descriptions[i][0 ]].second = descriptions[i][1 ];
14
+ }
15
+
16
+ int headval = 0 ;
17
+ auto iterLast = nodeflag.end ();
18
+ for (auto iter = nodeflag.begin (); iter != iterLast; ++iter)
19
+ {
20
+ if (iter->second == 1 )
21
+ {
22
+ headval = iter->first ;
23
+ break ;
24
+ }
25
+ }
26
+
27
+ TreeNode* head = new TreeNode (headval);
28
+ deque<TreeNode*> dq;
29
+ dq.push_back (head);
30
+ int k = 1 ;
31
+ while (k != 0 )
32
+ {
33
+ int n = 0 ;
34
+ for (int i = 0 ; i < k; ++i)
35
+ {
36
+ TreeNode* node = dq[i];
37
+ if (nodepos[node->val ].first != 0 )
38
+ {
39
+ node->left = new TreeNode (nodepos[node->val ].first );
40
+ dq.push_back (node->left );
41
+ ++n;
42
+ }
43
+ if (nodepos[node->val ].second != 0 )
44
+ {
45
+ node->right = new TreeNode (nodepos[node->val ].second );
46
+ dq.push_back (node->right );
47
+ ++n;
48
+ }
49
+ }
50
+ dq.erase (dq.begin (), dq.begin ()+k);
51
+ k = n;
52
+ }
53
+
54
+ return head;
55
+ }
Original file line number Diff line number Diff line change
1
+ vector<vector<int >> findDifference (vector<int >& nums1, vector<int >& nums2) {
2
+ int arr1[2001 ] = {0 };
3
+ int arr2[2001 ] = {0 };
4
+ int len1 = nums1.size (), len2 = nums2.size ();
5
+ for (int i = 0 ; i < len1; ++i)
6
+ arr1[nums1[i]+1000 ] = 1 ;
7
+ for (int i = 0 ; i < len2; ++i)
8
+ arr2[nums2[i]+1000 ] = 1 ;
9
+
10
+ vector<vector<int >> res (2 , vector<int >());
11
+ for (int i = 0 ; i < 2001 ; ++i)
12
+ {
13
+ if (arr1[i] != 0 && arr2[i] == 0 )
14
+ res[0 ].push_back (i-1000 );
15
+ if (arr1[i] == 0 && arr2[i] != 0 )
16
+ res[1 ].push_back (i-1000 );
17
+ }
18
+
19
+ return res;
20
+ }
Original file line number Diff line number Diff line change
1
+ int countNodes (TreeNode* root) {
2
+ int ct = 0 ;
3
+ search (root, ct);
4
+
5
+ return ct;
6
+ }
7
+
8
+ void search (TreeNode* node, int & n)
9
+ {
10
+ if (node != nullptr )
11
+ {
12
+ ++n;
13
+ search (node->left , n);
14
+ search (node->right , n);
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ int minBitFlips (int start, int goal) {
2
+ bitset<32 > bs (start), bg (goal);
3
+ int ct = 0 ;
4
+ for (int i = 0 ; i < 32 ; ++i)
5
+ {
6
+ if (bs[i] != bg[i])
7
+ ++ct;
8
+ }
9
+
10
+ return ct;
11
+ }
Original file line number Diff line number Diff line change
1
+ int triangularSum (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ while (len > 1 )
4
+ {
5
+ for (int i = 0 ; i < len-1 ; ++i)
6
+ {
7
+ nums[i] = (nums[i]+nums[i+1 ])%10 ;
8
+ }
9
+ --len;
10
+ }
11
+
12
+ return nums[0 ];
13
+ }
Original file line number Diff line number Diff line change
1
+ int convertTime (string current, string correct) {
2
+ int curMinutes = ((current[0 ]-' 0' )*10 +(current[1 ]-' 0' ))*60 + ((current[3 ]-' 0' )*10 +(current[4 ]-' 0' ));
3
+ int corrMinutes = ((correct[0 ]-' 0' )*10 +(correct[1 ]-' 0' ))*60 + ((correct[3 ]-' 0' )*10 +(correct[4 ]-' 0' ));
4
+ int delta = corrMinutes - curMinutes;
5
+ int ct = 0 ;
6
+ if (delta >= 60 )
7
+ {
8
+ ct += delta/60 ;
9
+ delta %= 60 ;
10
+ }
11
+ if (delta >= 15 )
12
+ {
13
+ ct += delta/15 ;
14
+ delta %= 15 ;
15
+ }
16
+ if (delta >= 5 )
17
+ {
18
+ ct += delta/5 ;
19
+ delta %= 5 ;
20
+ }
21
+
22
+ return ct + delta;
23
+ }
Original file line number Diff line number Diff line change
1
+ vector<vector<int >> findWinners (vector<vector<int >>& matches) {
2
+ map<int , pair<int ,int >> mp;
3
+ int len = matches.size ();
4
+ for (int i = 0 ; i < len; ++i)
5
+ {
6
+ mp[matches[i][0 ]].first ++;
7
+ mp[matches[i][1 ]].second ++;
8
+ }
9
+
10
+ vector<vector<int >> res (2 , vector<int >{});
11
+ auto iterLast = mp.end ();
12
+ for (auto iter = mp.begin (); iter != iterLast; ++iter)
13
+ {
14
+ if (iter->second .second == 0 )
15
+ res[0 ].push_back (iter->first );
16
+ if (iter->second .second == 1 )
17
+ res[1 ].push_back (iter->first );
18
+ }
19
+
20
+ return res;
21
+ }
You can’t perform that action at this time.
0 commit comments