Skip to content

Commit 980d16a

Browse files
authored
Merge pull request #281 from hellomrsun/master
Add C# solutions
2 parents cbfdca6 + 98d7b74 commit 980d16a

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Solutions in various programming languages are provided. Enjoy it.
1313
4. [Partition Labels](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/04-Partition-Labels)
1414
5. [All Elements in Two Binary Search Trees](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/05-All-Elements-in-Two-Binary-Search-Trees)
1515
6. [Image Overlap](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/06-Image-Overlap)
16+
7. [Word Pattern](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/07-Word-Pattern)
17+
8. [Sum of Root To Leaf Binary Numbers](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/08-Sum-of-Root-To-Leaf-Binary-Numbers)
18+
9. [Compare Version Numbers](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/09-Compare-Version-Numbers)
1619

1720
## August LeetCoding Challenge
1821
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Solution {
2+
public bool WordPattern(string pattern, string str) {
3+
var words = str.Split(" ");
4+
5+
if (pattern.Length != words.Length) return false;
6+
7+
var dict = new Dictionary<char, string>();
8+
9+
for (int i = 0; i < words.Length; i++)
10+
{
11+
char c = pattern[i]; //Positionning char
12+
13+
if (dict.ContainsKey(c))
14+
{
15+
if (dict[c] != words[i])
16+
return false;
17+
}
18+
else
19+
{
20+
if (dict.ContainsValue(words[i]))
21+
return false;
22+
23+
dict.Add(c, words[i]);
24+
}
25+
}
26+
27+
return true;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
public int SumRootToLeaf(TreeNode root) {
16+
if(root == null) return 0;
17+
18+
return Traverse(root, 0);
19+
}
20+
21+
private int Traverse(TreeNode node, int v){
22+
if(node == null) {
23+
return 0;
24+
}
25+
26+
v = v * 2 + node.val;
27+
28+
return node.left == node.right ? v : Traverse(node.left, v) + Traverse(node.right, v);
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
public int CompareVersion(string version1, string version2) {
3+
var v1parts = version1.Split(".").ToList();
4+
var v2parts = version2.Split(".").ToList();
5+
6+
if(v1parts.Count != v2parts.Count){
7+
while(v1parts.Count < v2parts.Count)
8+
{
9+
v1parts.Add("0");
10+
}
11+
while(v1parts.Count > v2parts.Count)
12+
{
13+
v2parts.Add("0");
14+
}
15+
}
16+
17+
for(int i=0; i<v1parts.Count; i++){
18+
var v1 = int.Parse(v1parts[i]);
19+
var v2 = int.Parse(v2parts[i]);
20+
if(v1 > v2){
21+
return 1;
22+
}else if(v1 == v2){
23+
continue;
24+
}else if(v1 < v2){
25+
return -1;
26+
}
27+
}
28+
29+
return 0;
30+
}
31+
}

0 commit comments

Comments
 (0)