Skip to content

Commit 5fe19a4

Browse files
committed
AddBinary and ExcelColumnTitle
1 parent cca833c commit 5fe19a4

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Math/Math.Lib/AddBinarySln.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* ==============================================================================
2+
* 功能描述:AddBinary
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/23 12:57:28
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace Math.Lib
12+
{
13+
// Given two binary strings, return their sum (also a binary string).
14+
//For example,
15+
//a = "11"
16+
//b = "1"
17+
//Return "100".
18+
/// <summary>
19+
/// AddBinary
20+
/// </summary>
21+
public class AddBinarySln
22+
{
23+
public string AddBinary(string a, string b)
24+
{
25+
StringBuilder sb = new StringBuilder();
26+
int carry = 0, acnt = a.Length, bcnt = b.Length;
27+
for (int i = acnt - 1, j = bcnt - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
28+
int sum2 = 0;
29+
if (i < 0 && j < 0){ //overflow solving
30+
sb.Append(carry);
31+
carry = 0;
32+
continue;
33+
}
34+
//discuss three conditions according to i and j
35+
if (i < 0) sum2 = b[j] - '0';
36+
else if (j < 0) sum2 = a[i] - '0';
37+
else sum2 = a[i] - '0' + b[j] - '0';
38+
if (sum2 + carry < 2){
39+
sb.Append(sum2 + carry);
40+
carry = 0;
41+
}
42+
else {
43+
sb.Append(sum2 + carry - 2);
44+
carry = 1;
45+
}
46+
}
47+
//reverse the sb
48+
IEnumerable<char> rtnchars = sb.ToString().Reverse();
49+
sb.Clear();
50+
foreach (var ch in rtnchars) sb.Append(ch);
51+
return sb.ToString();
52+
}
53+
}
54+
}

Math/Math.Lib/ExcelColumnTitle.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* ==============================================================================
2+
* 功能描述:Class1
3+
* 创 建 者:gz
4+
* 创建日期:2017/5/23 12:49:30
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace Math.Lib
12+
{
13+
/// <summary>
14+
/// ExcelColumnTitle
15+
/// </summary>
16+
public class ExcelColumnTitle
17+
{
18+
public string ConvertToTitle(int n)
19+
{
20+
//A~Z:26
21+
//AA~ZZ:26*26
22+
//...
23+
if (n == 1) return "A";
24+
char[] chdict = {'A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
25+
StringBuilder sb = new StringBuilder();
26+
while (n > 0)
27+
{
28+
n--; //beacuse our chdict's index begins Zero.
29+
sb.Append(chdict[n % 26]);
30+
n = n / 26;
31+
}
32+
IEnumerable<char> rtnchars = sb.ToString().Reverse();
33+
sb.Clear();
34+
foreach (var ch in rtnchars) sb.Append(ch);
35+
return sb.ToString();
36+
}
37+
38+
}
39+
}

0 commit comments

Comments
 (0)