Skip to content

Commit e93f530

Browse files
author
atwood
committed
first commit
0 parents  commit e93f530

15 files changed

+871
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.vs/
2+
/bin/
3+
/obj/

LeetCode.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

LeetCode.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29411.108
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCode", "LeetCode.csproj", "{FFEEB876-767D-41EE-ABA9-027E68F2620F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FFEEB876-767D-41EE-ABA9-027E68F2620F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FFEEB876-767D-41EE-ABA9-027E68F2620F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FFEEB876-767D-41EE-ABA9-027E68F2620F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FFEEB876-767D-41EE-ABA9-027E68F2620F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C5A6A089-C86A-4CB1-A8A5-DAC22668C201}
24+
EndGlobalSection
25+
EndGlobal

No13_Math.cs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_13
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// //int input = int.Parse(Console.ReadLine());
13+
// string input = Console.ReadLine();
14+
// var res = solution.RomanToInt(input);
15+
// Console.WriteLine(res);
16+
// }
17+
//}
18+
19+
public class Solution
20+
{
21+
/// <summary>
22+
/// 第一印象,遍历解(没有看到更好的做法,)
23+
/// </summary>
24+
/// <param name="s"></param>
25+
/// <returns></returns>
26+
public int RomanToInt(string s)
27+
{
28+
int sum = 0;
29+
for (int i = 0; i < s.Length; i++)
30+
{
31+
switch (s[i])
32+
{
33+
case 'I':
34+
{
35+
if (i != s.Length - 1 && s[i + 1] == 'V')
36+
{
37+
sum += 4;
38+
i++;
39+
}
40+
else if (i != s.Length - 1 && s[i + 1] == 'X')
41+
{
42+
sum += 9;
43+
i++;
44+
}
45+
else
46+
{
47+
sum += 1;
48+
}
49+
break;
50+
}
51+
case 'V':
52+
sum += 5; break;
53+
case 'X':
54+
{
55+
if (i != s.Length - 1 && s[i + 1] == 'L')
56+
{
57+
sum += 40;
58+
i++;
59+
}
60+
else if (i != s.Length - 1 && s[i + 1] == 'C')
61+
{
62+
sum += 90;
63+
i++;
64+
}
65+
else
66+
{
67+
sum += 10;
68+
}
69+
break;
70+
}
71+
case 'L':
72+
sum += 50; break;
73+
case 'C':
74+
{
75+
if (i != s.Length - 1 && s[i + 1] == 'D')
76+
{
77+
sum += 400;
78+
i++;
79+
}
80+
else if (i != s.Length - 1 && s[i + 1] == 'M')
81+
{
82+
sum += 900;
83+
i++;
84+
}
85+
else
86+
{
87+
sum += 100;
88+
}
89+
break;
90+
}
91+
case 'D':
92+
sum += 500; break;
93+
case 'M': sum += 1000; break;
94+
default: break;
95+
}
96+
}
97+
return sum;
98+
}
99+
}
100+
}

No168_Math.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_168
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// int input = int.Parse(Console.ReadLine());
13+
// //string input = Console.ReadLine();
14+
// //string input2 = Console.ReadLine();
15+
// var res = solution.ConvertToTitle(input);
16+
// Console.WriteLine(res);
17+
// }
18+
//}
19+
public class Solution
20+
{
21+
public string ConvertToTitle(int n)
22+
{
23+
int len = (int)Math.Log(n, 26) + 1;
24+
char[] res = new char[len];
25+
//A的ASCII码为65
26+
for (int i = 0; n > 0; i++)
27+
{
28+
int reminder = n % 26;
29+
n /= 26;
30+
31+
if (reminder == 0)
32+
{
33+
res[i] = (char)(64 + 26);
34+
//此题重点:因为没有0的表示形态,所以当整除的时候要-1,因为后面的Z已经代表了26
35+
n -= 1;
36+
}
37+
else
38+
res[i] = (char)(64 + reminder);
39+
}
40+
Array.Reverse(res);
41+
return new string(res).Trim('\0');
42+
}
43+
}
44+
}

No171_Math.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_171
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// //int input = int.Parse(Console.ReadLine());
13+
// string input = Console.ReadLine();
14+
// //string input2 = Console.ReadLine();
15+
// var res = solution.TitleToNumber(input);
16+
// Console.WriteLine(res);
17+
// }
18+
//}
19+
public class Solution
20+
{
21+
public int TitleToNumber(string s)
22+
{
23+
int sum = 0;
24+
for (int i = s.Length - 1; i >= 0; i--)
25+
{
26+
sum += (s[i] - ('A' - 1)) * (int)Math.Pow(26, s.Length - 1 - i);
27+
}
28+
return sum;
29+
}
30+
}
31+
}

No172_Math.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_172
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// int input = int.Parse(Console.ReadLine());
13+
// //string input = Console.ReadLine();
14+
// //string input2 = Console.ReadLine();
15+
// var res = solution.TrailingZeroes(input);
16+
// Console.WriteLine(res);
17+
// }
18+
//}
19+
public class Solution
20+
{
21+
/// <summary>
22+
/// 更高维度拆解分式,通过数学推出,大概思路就是分解25就是分解两次5,往上也是一样,所以通过不断分解5就可以得到答案
23+
/// 时间复杂度O(log n),因为是不断除以5,所以是5的幂,即为O(log 5 n),循环内的除法是32位整形内的,所以是O(1),忽略
24+
/// 空间复杂度O(1),只用了一个zeroCount计算。
25+
/// </summary>
26+
/// <param name="n"></param>
27+
/// <returns></returns>
28+
public int TrailingZeroes(int n)
29+
{
30+
int zeroCount = 0;
31+
while (n > 0)
32+
{
33+
n /= 5;
34+
zeroCount += n;
35+
}
36+
return zeroCount;
37+
}
38+
39+
/// <summary>
40+
/// 尾数的零一定是由2乘以5构成的,所以可以分解成有多少个2和5的组合就能获得答案,因为分解出来的2是远多于5的,
41+
/// 所以忽略掉2,只考虑5。5还有一种特殊情况就是,例如25这种,可以分解出不只一个5,这种就要特殊考虑。
42+
/// 时间复杂度O(n),循环步长为5,所以是1/5n,还有部分进入计算的也可以忽略不计
43+
/// 空间复杂度O(1),只用了一个zeroCount计数。
44+
/// </summary>
45+
/// <param name="n"></param>
46+
/// <returns></returns>
47+
//public int TrailingZeroes(int n)
48+
//{
49+
// //写到一半突然觉得不对,笔记本上写了一下,发现了规律。降维打击成就达成(然而并没有,哈哈哈)
50+
// int zeroCount = 0;
51+
// for (int i = 5; i <= n; i += 5)
52+
// {
53+
// int powerOfFive = 5;
54+
// while (i % powerOfFive == 0)
55+
// {
56+
// zeroCount++;
57+
// powerOfFive *= 5;
58+
// }
59+
// }
60+
// return zeroCount;
61+
//}
62+
}
63+
}

No202_Math.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCode_202
6+
{
7+
//static void Main(string[] args)
8+
//{
9+
// var solution = new Solution();
10+
// while (true)
11+
// {
12+
// int input = int.Parse(Console.ReadLine());
13+
// //string input = Console.ReadLine();
14+
// //string input2 = Console.ReadLine();
15+
// var res = solution.IsHappy(input);
16+
// Console.WriteLine(res);
17+
// }
18+
//}
19+
20+
public class Solution
21+
{
22+
23+
public int GetNextNum(int n)
24+
{
25+
int num = 0;
26+
while (n > 0)
27+
{
28+
num += (int)Math.Pow(n % 10, 2);
29+
n /= 10;
30+
}
31+
return num;
32+
}
33+
34+
/// <summary>
35+
/// 快慢指针来查看是否有循环,这样就不用存走过的路途了(弗洛伊德循环查找算法)
36+
/// 时间复杂度O(log n)
37+
/// 空间复杂度O(1),因为不用存走过的路径,只需要不断计算就可以了
38+
/// </summary>
39+
/// <param name="n"></param>
40+
/// <returns></returns>
41+
public bool IsHappy(int n)
42+
{
43+
int rabbit = n;
44+
int tortoise = n;
45+
do
46+
{
47+
rabbit = GetNextNum(GetNextNum(rabbit));
48+
tortoise = GetNextNum(tortoise);
49+
}
50+
while (rabbit != 1 && rabbit != tortoise);//&& tortoise != 1
51+
52+
//如果没有循环兔子会最先到达
53+
return rabbit == 1;// || tortoise == 1;
54+
}
55+
56+
/// <summary>
57+
///hash set记录已经走过的路径,如果有已经走过的,就一定不是happy number
58+
///时间复杂度:O(log n)
59+
///空间复杂度:O(log n)
60+
/// </summary>
61+
/// <param name="n"></param>
62+
/// <returns></returns>
63+
//public bool IsHappy(int n)
64+
//{
65+
// HashSet<int> hs = new HashSet<int>();
66+
// while (n != 1 && !hs.Contains(n))
67+
// {
68+
// hs.Add(n);
69+
// n = GetNextNum(n);
70+
// }
71+
// return n == 1;
72+
//}
73+
}
74+
}

0 commit comments

Comments
 (0)