Skip to content

Commit d31b00c

Browse files
committed
🚀 09-Sep-2020
1 parent 1b31163 commit d31b00c

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include<bits/stdc++.h>
2+
using namesapce std;
3+
4+
int main(){
5+
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Given a binary tree, each node has value 0 or 1.
2+
Each root-to-leaf path represents a binary number starting with the most significant bit.
3+
For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
4+
5+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
6+
7+
Return the sum of these numbers.
8+
9+
10+
11+
Example 1:
12+
13+
14+
15+
Input: [1,0,1,0,1,0,1]
16+
Output: 22
17+
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
18+
19+
20+
Note:
21+
22+
The number of nodes in the tree is between 1 and 1000.
23+
node.val is 0 or 1.
24+
The answer will not exceed 2^31 - 1.
25+
Hide Hint #1
26+
Find each path, then transform that path to an integer in base 10.
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
/**
39+
* Definition for a binary tree node.
40+
* struct TreeNode {
41+
* int val;
42+
* TreeNode *left;
43+
* TreeNode *right;
44+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
45+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
46+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
47+
* };
48+
*/
49+
class Solution {
50+
public:
51+
52+
void solve(TreeNode* root, int num, int &sum){
53+
if(!root) return;
54+
55+
num=2 * num + root->val;
56+
57+
solve(root->left, num, sum);
58+
solve(root->right, num, sum);
59+
60+
if(!root->left && !root->right)
61+
sum+=num;
62+
63+
64+
65+
}
66+
67+
int sumRootToLeaf(TreeNode* root) {
68+
int sum=0;
69+
if(!root) return 0;
70+
solve(root, 0, sum); // <root, decimal number, sum>
71+
72+
return sum;
73+
}
74+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Given a binary tree, each node has value 0 or 1.
2+
Each root-to-leaf path represents a binary number starting with the most significant bit.
3+
For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
4+
5+
For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
6+
7+
Return the sum of these numbers.
8+
9+
10+
11+
Example 1:
12+
13+
14+
15+
Input: [1,0,1,0,1,0,1]
16+
Output: 22
17+
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
18+
19+
20+
Note:
21+
22+
The number of nodes in the tree is between 1 and 1000.
23+
node.val is 0 or 1.
24+
The answer will not exceed 2^31 - 1.
25+
Hide Hint #1
26+
Find each path, then transform that path to an integer in base 10.
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
/**
39+
* Definition for a binary tree node.
40+
* struct TreeNode {
41+
* int val;
42+
* TreeNode *left;
43+
* TreeNode *right;
44+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
45+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
46+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
47+
* };
48+
*/
49+
class Solution {
50+
public:
51+
52+
void solve(TreeNode* root, int num, int &sum){
53+
if(!root) return;
54+
55+
num=2 * num + root->val;
56+
57+
solve(root->left, num, sum);
58+
solve(root->right, num, sum);
59+
60+
if(!root->left && !root->right)
61+
sum+=num;
62+
63+
64+
65+
}
66+
67+
int sumRootToLeaf(TreeNode* root) {
68+
int sum=0;
69+
if(!root) return 0;
70+
solve(root, 0, sum); // <root, decimal number, sum>
71+
72+
return sum;
73+
}
74+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Compare two version numbers version1 and version2.
2+
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
3+
4+
You may assume that the version strings are non-empty and contain only digits and the . character.
5+
6+
The . character does not represent a decimal point and is used to separate number sequences.
7+
8+
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
9+
10+
You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.
11+
12+
13+
14+
Example 1:
15+
16+
Input: version1 = "0.1", version2 = "1.1"
17+
Output: -1
18+
Example 2:
19+
20+
Input: version1 = "1.0.1", version2 = "1"
21+
Output: 1
22+
Example 3:
23+
24+
Input: version1 = "7.5.2.4", version2 = "7.5.3"
25+
Output: -1
26+
Example 4:
27+
28+
Input: version1 = "1.01", version2 = "1.001"
29+
Output: 0
30+
Explanation: Ignoring leading zeroes, both “01and001" represent the same number “1”
31+
Example 5:
32+
33+
Input: version1 = "1.0", version2 = "1.0.0"
34+
Output: 0
35+
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
36+
37+
38+
Note:
39+
40+
Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
41+
Version strings do not start or end with dots, and they will not be two consecutive dots.
42+
43+
44+
45+
46+
47+
48+
class Solution {
49+
public:
50+
int compareVersion(string ver1, string ver2) {
51+
int n1=ver1.length();
52+
int n2=ver2.length();
53+
54+
int i=0, j=0;
55+
56+
while(i<n1 || j<n2){
57+
int num1=0, num2=0;
58+
while(i<n1 && ver1[i]!='.'){
59+
num1=(num1*10)+(ver1[i]-'0');
60+
i++;
61+
}
62+
while(j<n2 && ver2[j]!='.'){
63+
num2=(num2*10)+(ver2[j]-'0');
64+
j++;
65+
}
66+
67+
if(num1>num2) return 1;
68+
if(num1<num2) return -1;
69+
70+
i++;
71+
j++;
72+
}
73+
return 0;
74+
}
75+
};

0 commit comments

Comments
 (0)