Skip to content

Commit 035447a

Browse files
committed
auto ok
1 parent 3048baa commit 035447a

File tree

6 files changed

+116
-27
lines changed

6 files changed

+116
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
// Source : https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
3+
// Author : codeyu
4+
5+
// Date : 5/11/18 6:26:33 PM
6+
7+
/**********************************************************************************
8+
*
9+
*
10+
* Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
11+
*
12+
* For example:
13+
*
14+
* Given binary tree [3,9,20,null,null,15,7],
15+
*
16+
* 3
17+
*
18+
* / \
19+
*
20+
* 9 20
21+
*
22+
* / \
23+
*
24+
* 15 7
25+
*
26+
* return its bottom-up level order traversal as:
27+
*
28+
* [
29+
*
30+
* [15,7],
31+
*
32+
* [9,20],
33+
*
34+
* [3]
35+
*
36+
* ]
37+
*
38+
*
39+
*
40+
**********************************************************************************/
41+
using System;
42+
using System.Collections.Generic;
43+
using Algorithms.Utils;
44+
namespace Algorithms
45+
{
46+
public class Solution107
47+
{
48+
public static IList<IList<int>> LevelOrderBottom(TreeNode root)
49+
{
50+
throw new NotImplementedException();
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
using System;
3+
using System.Collections.Generic;
4+
using Algorithms;
5+
using Algorithms.Utils;
6+
using Xunit;
7+
namespace AlgorithmsTest
8+
{
9+
public class BinaryTreeLevelOrderTraversalIITest
10+
{
11+
[Theory]
12+
[InlineData()]
13+
public void TestMethod(TreeNode root,IList<IList<int>> output)
14+
{
15+
Assert.Equal(output, Solution107.LevelOrderBottom(root));
16+
}
17+
}
18+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ LeetCode
115115
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) | [C#](./Algorithms/MaximumDepthofBinaryTree.cs)|Easy|
116116
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | [C#](./Algorithms/ConstructBinaryTreefromPreorderandInorderTraversal.cs)|Medium|
117117
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal) | [C#](./Algorithms/ConstructBinaryTreefromInorderandPostorderTraversal.cs)|Medium|
118+
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [C#](./Algorithms/BinaryTreeLevelOrderTraversalII.cs)|Easy|

Scripts/graphql/CSharpCode.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
13
namespace graphql
24
{
35
public class CSharpCode
46
{
57
public string MethodName {get;set;}
8+
9+
public string ReturnType {get;set;}
10+
11+
public string ParamsTxt {get; set;}
12+
13+
public List<TypeName> Params {get; set;}
614
}
715
}

Scripts/graphql/QuestionDetail.cs

+5-20
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ public partial class QuestionDetail
4343
public string QuestionUrl => $"{Leetcode.BaseUrl}{QuestionDetailUrl}";
4444
public List<CodeDefinition> CodeDefinitions => JsonConvert.DeserializeObject<List<CodeDefinition>>(CodeDefinition);
4545

46-
public string[] ContentLines => HtmlToText(Content).Split('\n');
46+
public string[] ContentLines => HtmlToText(Content).Replace("\r","").Split('\n', StringSplitOptions.RemoveEmptyEntries);
4747
public MethodData MethodData => JsonConvert.DeserializeObject<MethodData>(MetaData);
4848

49-
public string CSharpCode => CodeDefinitions.Where(x=>x.Value=="csharp").First().DefaultCode;
49+
public string CSharpCodeTxt => CodeDefinitions.Where(x=>x.Value=="csharp").First().DefaultCode;
50+
5051
}
5152

5253
public partial class QuestionDetail
@@ -55,24 +56,8 @@ public partial class QuestionDetail
5556

5657
private static string HtmlToText(string html)
5758
{
58-
const string tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";//matches one or more (white space or line breaks) between '>' and '<'
59-
const string stripFormatting = @"<[^>]*(>|$)";//match any character between '<' and '>', even when end tag is missing
60-
const string lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";//matches: <br>,<br/>,<br />,<BR>,<BR/>,<BR />
61-
var lineBreakRegex = new Regex(lineBreak, RegexOptions.Multiline);
62-
var stripFormattingRegex = new Regex(stripFormatting, RegexOptions.Multiline);
63-
var tagWhiteSpaceRegex = new Regex(tagWhiteSpace, RegexOptions.Multiline);
64-
65-
var text = html;
66-
//Decode html specific characters
67-
text = System.Net.WebUtility.HtmlDecode(text);
68-
//Remove tag whitespace/line breaks
69-
text = tagWhiteSpaceRegex.Replace(text, "><");
70-
//Replace <br /> with line breaks
71-
text = lineBreakRegex.Replace(text, Environment.NewLine);
72-
//Strip formatting
73-
text = stripFormattingRegex.Replace(text, string.Empty);
74-
75-
return text;
59+
var regex = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
60+
return System.Web.HttpUtility.HtmlDecode((regex.Replace(html, "")));
7661
}
7762
}
7863

Scripts/graphql/TemplateOpt.cs

+31-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using Scriban;
55
using Scriban.Runtime;
66
using Scriban.Parsing;
7+
using System.Text.RegularExpressions;
8+
using System.Linq;
9+
710
namespace graphql
811
{
912
public class TemplateOpt
@@ -13,16 +16,18 @@ public class TemplateOpt
1316
private string _algorithmsTestPath;
1417
private string _readmePath = @"../../README.md";
1518
private string _author;
19+
20+
private CSharpCode _csharpCode;
1621
public string AlgorithmsTmp =>@"
1722
// Source : {{detail.question_url}}
1823
// Author : {{author}}
1924
// Date : {{date}}
2025
2126
/**********************************************************************************
2227
*
23-
{{for contentLine in detail.content_lines}}
28+
*{{for contentLine in detail.content_lines}}
2429
* {{contentLine}}
25-
{{end}}
30+
*{{end}}
2631
*
2732
*
2833
**********************************************************************************/
@@ -33,7 +38,7 @@ namespace Algorithms
3338
{
3439
public class Solution{{detail.question_id}}
3540
{
36-
public static {{m.return.type}} {{m.name}}({{ for param in m.params }} {{ param.type }} {{ param.name }},{{ end }})
41+
public static {{csharp.return_type}} {{csharp.method_name}}({{csharp.params_txt}})
3742
{
3843
throw new NotImplementedException();
3944
}
@@ -51,16 +56,17 @@ public class {{detail.question_name}}Test
5156
{
5257
[Theory]
5358
[InlineData({{detail.SampleTestCase}})]
54-
public void TestMethod({{ for param in m.params }} {{ param.type }} {{ param.name }},{{ end }} {{m.return.type}} output)
59+
public void TestMethod({{csharp.params_txt}},{{csharp.return_type}} output)
5560
{
56-
Assert.Equal(output, Solution{{detail.question_id}}.{{m.name}}({{ for param in m.params }} {{ param.name }},{{ end }}));
61+
Assert.Equal(output, Solution{{detail.question_id}}.{{csharp.method_name}}({{ for param in csharp.params }}{{ param.name }}{{if !for.last }}, {{end}}{{ end }}));
5762
}
5863
}
5964
}";
6065
public string ReadmeTmp =>@"|{{detail.question_id}}|[{{detail.question_title}}]({{detail.question_url}}) | [C#](./Algorithms/{{detail.question_name}}.cs)|{{detail.difficulty}}|";
6166
public TemplateOpt(QuestionDetail detail)
6267
{
6368
_detail = detail;
69+
_csharpCode = BuilderCSharpCodeModel(detail.CSharpCodeTxt);
6470
dynamic cmd = new Cmd();
6571
_author = cmd.git.config(get: "user.name");
6672
_algorithmsPath = $"../../Algorithms/{_detail.QuestionName}.cs";
@@ -70,15 +76,33 @@ public TemplateOpt(QuestionDetail detail)
7076
public bool Save()
7177
{
7278
var template = Template.Parse(AlgorithmsTmp);
73-
var algorithmsText = template.Render(new { Author = _author, Date = DateTime.Now, Detail = _detail, M = _detail.MethodData});
79+
var algorithmsText = template.Render(new { Author = _author, Date = DateTime.Now, Detail = _detail, Csharp = _csharpCode});
7480
File.WriteAllText(_algorithmsPath, algorithmsText);
7581
template = Template.Parse(AlgorithmsTestTmp);
76-
var algorithmsTestText = template.Render(new { Detail = _detail, M = _detail.MethodData});
82+
var algorithmsTestText = template.Render(new { Detail = _detail, Csharp = _csharpCode});
7783
File.WriteAllText(_algorithmsTestPath, algorithmsTestText);
7884
template = Template.Parse(ReadmeTmp);
7985
var readmeText = template.Render(new { Detail = _detail});
8086
File.AppendAllLines(_readmePath, new[]{readmeText});
8187
return true;
8288
}
89+
private CSharpCode BuilderCSharpCodeModel(string chsarpCodeTxt)
90+
{
91+
var pattern = @"(?s)\bSolution\b\s\{(.*?)\{";
92+
var match = Regex.Matches(chsarpCodeTxt, pattern).FirstOrDefault();
93+
var csharp = new CSharpCode();
94+
if(match != null)
95+
{
96+
var methodDeclare = match.Groups[1].Value.Trim();
97+
csharp.MethodName = methodDeclare.Split(' ')[2].Substring(0, methodDeclare.Split(' ')[2].IndexOf('('));
98+
csharp.ReturnType = methodDeclare.Split(' ')[1];
99+
csharp.ParamsTxt = methodDeclare.Substring(methodDeclare.IndexOf('(') + 1, methodDeclare.IndexOf(')') - methodDeclare.IndexOf('(') - 1);
100+
csharp.Params = csharp.ParamsTxt.Split(',', StringSplitOptions.RemoveEmptyEntries)
101+
.Select(x=>new TypeName{type=x.Split(' ')[0],name=x.Split(' ')[1]})
102+
.ToList();
103+
104+
}
105+
return csharp;
106+
}
83107
}
84108
}

0 commit comments

Comments
 (0)