Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f3b301

Browse files
check in source code
1 parent 310de6e commit 9f3b301

File tree

4 files changed

+343
-0
lines changed

4 files changed

+343
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode273_IntegerToEnglishWords
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
var words = NumberToWords(12345);
14+
}
15+
16+
public static string[] Thousands = new string[] { "Billion", "Million", "Thousand","" };
17+
public static string[] Digits = new string[] {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
18+
public static string[] TwoDigits = new string[] {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
19+
public static string[] Bigger20 = new string[] { "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
20+
21+
public static string NumberToWords(int number)
22+
{
23+
if (number == 0)
24+
return "Zero";
25+
26+
var words = new StringBuilder();
27+
28+
// 10^9, 10^6, 10^3
29+
var dividends = new int[] {1000 * 1000 * 1000, 1000 * 1000, 1000, 1 };
30+
var workingNumber = number;
31+
32+
for (int i = 0; i < dividends.Length; i++)
33+
{
34+
var currentDividend = dividends[i];
35+
36+
var current = workingNumber / currentDividend;
37+
38+
// current three digits only
39+
// zero - ignore
40+
if (current == 0)
41+
continue;
42+
43+
words.Append(toThreeDigitsWord(current) + " ");
44+
45+
workingNumber = workingNumber - current * currentDividend;
46+
47+
words.Append(Thousands[i] + " ");
48+
}
49+
50+
return words.ToString().Trim();
51+
}
52+
53+
/// <summary>
54+
/// test cases:
55+
/// 100
56+
/// 85
57+
/// 900
58+
/// 13
59+
/// 345
60+
/// </summary>
61+
/// <param name="current"></param>
62+
/// <returns></returns>
63+
private static string toThreeDigitsWord(int current)
64+
{
65+
var words = new StringBuilder();
66+
67+
var thirdDigit = current / 100; // 13 -> 0
68+
if (thirdDigit > 0)
69+
words.Append(Digits[thirdDigit] + " Hundred "); //
70+
71+
var residue = current - thirdDigit * 100; // 13
72+
73+
var secondDigit = residue / 10; // 1
74+
var biggerThan19 = secondDigit >= 2;
75+
var fromTenTo19 = secondDigit == 1;
76+
var lessThan10 = secondDigit == 0;
77+
78+
if (biggerThan19)
79+
{
80+
words.Append(Bigger20[secondDigit - 1] + " "); // Eighty
81+
82+
residue = residue - secondDigit * 10; // 5
83+
if(residue > 0)
84+
words.Append(Digits[residue] + " "); // 5 -> Five
85+
}
86+
87+
if (fromTenTo19)
88+
{
89+
words.Append(TwoDigits[residue - 10] + " "); // 13 - 10 = 3
90+
}
91+
92+
if (lessThan10 && residue > 0)
93+
{
94+
words.Append(Digits[residue] + " ");
95+
}
96+
97+
return words.ToString().TrimEnd();
98+
}
99+
}
100+
}

‎2017 July -/Leetcode113_PathSumII.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode113_PathSumII
8+
{
9+
/// <summary>
10+
/// Leetcode 113 - path sum II
11+
///
12+
/// July 18, 2018
13+
/// </summary>
14+
class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
}
19+
20+
public class TreeNode {
21+
public int val;
22+
public TreeNode left;
23+
public TreeNode right;
24+
public TreeNode(int x) { val = x; }
25+
}
26+
27+
public class Solution
28+
{
29+
public IList<IList<int>> PathSum(TreeNode root, int sum)
30+
{
31+
if (root == null)
32+
return new List<IList<int>>();
33+
34+
var currentPath = new List<int>();
35+
var paths = new List<IList<int>>();
36+
37+
pathSumHelper(root, sum, currentPath, paths);
38+
39+
return paths;
40+
}
41+
42+
/// <summary>
43+
///
44+
/// </summary>
45+
/// <param name="root"></param>
46+
/// <param name="residue"></param>
47+
/// <param name="currentPath"></param>
48+
/// <param name="paths"></param>
49+
private static void pathSumHelper(TreeNode root, int residue, IList<int> currentPath, IList<IList<int>> paths)
50+
{
51+
if (root == null)
52+
return;
53+
54+
var currentValue = root.val;
55+
currentPath.Add(currentValue);
56+
57+
if (root.left == null && root.right == null)
58+
{
59+
if(residue == currentValue)
60+
paths.Add(currentPath);
61+
}
62+
else
63+
{
64+
pathSumHelper(root.left, residue - currentValue, new List<int>(currentPath), paths);
65+
pathSumHelper(root.right, residue - currentValue, new List<int>(currentPath), paths);
66+
}
67+
}
68+
}
69+
}
70+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode273_IntegerToEnglishWords
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
var words = NumberToWords(12345);
14+
}
15+
16+
public static string[] Thousands = new string[] { "Billion", "Million", "Thousand","" };
17+
public static string[] Digits = new string[] {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
18+
public static string[] TwoDigits = new string[] {"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
19+
public static string[] Bigger20 = new string[] { "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
20+
21+
public static string NumberToWords(int number)
22+
{
23+
if (number == 0)
24+
return "Zero";
25+
26+
var words = new StringBuilder();
27+
28+
// 10^9, 10^6, 10^3
29+
var dividends = new int[] {1000 * 1000 * 1000, 1000 * 1000, 1000, 1 };
30+
var workingNumber = number;
31+
32+
for (int i = 0; i < dividends.Length; i++)
33+
{
34+
var currentDividend = dividends[i];
35+
36+
var current = workingNumber / currentDividend;
37+
38+
// current three digits only
39+
// zero - ignore
40+
if (current == 0)
41+
continue;
42+
43+
words.Append(toThreeDigitsWord(current));
44+
45+
workingNumber = workingNumber - current * currentDividend;
46+
47+
if(i < 3)
48+
words.Append(" ");
49+
50+
words.Append(Thousands[i] + " ");
51+
}
52+
53+
return words.ToString().Trim();
54+
}
55+
56+
/// <summary>
57+
/// test cases:
58+
/// 100
59+
/// 85
60+
/// 900
61+
/// 13
62+
/// 345
63+
/// </summary>
64+
/// <param name="current"></param>
65+
/// <returns></returns>
66+
private static string toThreeDigitsWord(int current)
67+
{
68+
var words = new StringBuilder();
69+
70+
var thirdDigit = current / 100; // 13 -> 0
71+
if (thirdDigit > 0)
72+
words.Append(Digits[thirdDigit] + " Hundred "); //
73+
74+
var residue = current - thirdDigit * 100; // 13
75+
76+
var secondDigit = residue / 10; // 1
77+
var biggerThan19 = secondDigit >= 2;
78+
var fromTenTo19 = secondDigit == 1;
79+
var lessThan10 = secondDigit == 0;
80+
81+
if (biggerThan19)
82+
{
83+
words.Append(Bigger20[secondDigit - 1] + " "); // Eighty
84+
85+
residue = residue - secondDigit * 10; // 5
86+
if(residue > 0)
87+
words.Append(Digits[residue] + " "); // 5 -> Five
88+
}
89+
90+
if (fromTenTo19)
91+
{
92+
words.Append(TwoDigits[residue - 10] + " "); // 13 - 10 = 3
93+
}
94+
95+
if (lessThan10 && residue > 0)
96+
{
97+
words.Append(Digits[residue] + " ");
98+
}
99+
100+
return words.ToString().TrimEnd();
101+
}
102+
}
103+
}

‎Leetcode113_PathSumII.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode113_PathSumII
8+
{
9+
/// <summary>
10+
/// Leetcode 113 - path sum II
11+
///
12+
/// July 18, 2018
13+
/// </summary>
14+
class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
}
19+
20+
public class TreeNode {
21+
public int val;
22+
public TreeNode left;
23+
public TreeNode right;
24+
public TreeNode(int x) { val = x; }
25+
}
26+
27+
public class Solution
28+
{
29+
public IList<IList<int>> PathSum(TreeNode root, int sum)
30+
{
31+
if (root == null)
32+
return new List<IList<int>>();
33+
34+
var currentPath = new List<int>();
35+
var paths = new List<IList<int>>();
36+
37+
pathSumHelper(root, sum, currentPath, paths);
38+
39+
return paths;
40+
}
41+
42+
/// <summary>
43+
///
44+
/// </summary>
45+
/// <param name="root"></param>
46+
/// <param name="residue"></param>
47+
/// <param name="currentPath"></param>
48+
/// <param name="paths"></param>
49+
private static void pathSumHelper(TreeNode root, int residue, IList<int> currentPath, IList<IList<int>> paths)
50+
{
51+
if (root == null)
52+
return;
53+
54+
var currentValue = root.val;
55+
currentPath.Add(currentValue);
56+
57+
if (root.left == null && root.right == null)
58+
{
59+
if(residue == currentValue)
60+
paths.Add(currentPath);
61+
}
62+
else
63+
{
64+
pathSumHelper(root.left, residue - currentValue, new List<int>(currentPath), paths);
65+
pathSumHelper(root.right, residue - currentValue, new List<int>(currentPath), paths);
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /