Skip to content

Commit 54eefa0

Browse files
committed
panlindrome number; excel column number of excel
1 parent cca833c commit 54eefa0

File tree

5 files changed

+101
-58
lines changed

5 files changed

+101
-58
lines changed

Math/Math.Lib/ExcelColumnNumberSln.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Math.Lib
7+
{
8+
public class ExcelColumnNumberSln
9+
{
10+
public int TitleToNumber(string s)
11+
{
12+
char[] chs= {'A','B','C','D','E','F','G','H','I','J','K',
13+
'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
14+
Dictionary<char,int> dict = new Dictionary<char,int>();
15+
for(int i=0; i<chs.Length;i++) dict.Add(chs[i],i+1);
16+
int scnt = s.Length, rtnsum=0;
17+
for (int i = scnt - 1,j=0; i >= 0; i--,j++)
18+
rtnsum += dict[s[i]] * (int)System.Math.Pow(26, j);
19+
return rtnsum;
20+
}
21+
}
22+
}

Math/Math.Lib/ExlColumnTitleSln.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Math.Lib
7+
{
8+
// Given a positive integer, return its corresponding column title as appear in an Excel sheet.
9+
10+
//For example:
11+
12+
// 1 -> A
13+
// 2 -> B
14+
// 3 -> C
15+
// ...
16+
// 26 -> Z
17+
// 27 -> AA
18+
// 28 -> AB
19+
public class ExlColumnTitleSln
20+
{
21+
public string ConvertToTitle(int n)
22+
{
23+
//A~Z:26
24+
//AA~ZZ:26*26
25+
//...
26+
if (n == 0) return "A";
27+
char[] chdict = new char[]{'A','B','C','D','E','F','G','H','I','J','K',
28+
'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
29+
StringBuilder sb = new StringBuilder();
30+
while (n > 0)
31+
{
32+
if (n % 26 == 0)
33+
sb.Append('Z');
34+
else sb.Append(chdict[n % 26 - 1]);
35+
n = n / 26;
36+
}
37+
IEnumerable<char> rtnchars = sb.ToString().Reverse();
38+
sb.Clear();
39+
foreach (var ch in rtnchars)
40+
sb.Append(ch);
41+
return sb.ToString();
42+
}
43+
}
44+
}

Math/Math.Lib/IssPalindromeSln.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Math.Lib
7+
{
8+
public class IssPalindromeSln
9+
{
10+
public bool IsPalindrome(int x)
11+
{
12+
int palindromex = 0, tmp = x;
13+
int sumbit = 0;
14+
//calcuate bit count
15+
while (tmp > 0)
16+
{
17+
sumbit++;
18+
tmp /= 10;
19+
}
20+
tmp = x;
21+
//get a number reversely
22+
while (tmp > 0)
23+
{
24+
//if palindromex happens overflow, it would return false;
25+
palindromex += tmp % 10 * (int)System.Math.Pow(10, --sumbit);
26+
tmp /= 10;
27+
}
28+
return palindromex == x;
29+
}
30+
}
31+
}

Math/Math.Lib/Math.Lib.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@
4141
</ItemGroup>
4242
<ItemGroup>
4343
<Compile Include="AddStringsSln.cs" />
44+
<Compile Include="ExcelColumnNumberSln.cs" />
45+
<Compile Include="ExlColumnTitleSln.cs" />
4446
<Compile Include="HappyNumber.cs" />
47+
<Compile Include="IssPalindromeSln.cs" />
4548
<Compile Include="MinimumMovesSln.cs" />
4649
<Compile Include="MissingNumberSln.cs" />
4750
<Compile Include="Nthdigit.cs" />
4851
<Compile Include="PerfectNumberSln.cs" />
4952
<Compile Include="PowerOfTwoSln.cs" />
5053
<Compile Include="Properties\AssemblyInfo.cs" />
51-
<Compile Include="ReverseIntegarSln - Copy.cs" />
5254
<Compile Include="ReverseIntegarSln.cs" />
55+
<Compile Include="Sqrtx.cs" />
5356
</ItemGroup>
5457
<ItemGroup>
5558
<Content Include="Floyd%27sTortoiseandHare.jpg" />

Math/Math.Lib/ReverseIntegarSln - Copy.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)