Skip to content

Commit d16e640

Browse files
committed
Hash table tag
1 parent d737638 commit d16e640

File tree

11 files changed

+492
-0
lines changed

11 files changed

+492
-0
lines changed

HashTable/HashTable.Lib/Boomerangs.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* ==============================================================================
2+
* 功能描述:Boomerangs
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:24:56
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
//Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
11+
12+
//Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
13+
14+
//Example:
15+
16+
//Input:
17+
//[[0,0],[1,0],[2,0]]
18+
19+
//Output:
20+
//2
21+
22+
//Explanation:
23+
//The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
24+
namespace HashTable.Lib
25+
{
26+
/// <summary>
27+
/// Boomerangs
28+
/// </summary>
29+
public class Boomerangs
30+
{
31+
32+
33+
#region 公有方法
34+
public static int NumberOfBoomerangs(int[,] points)
35+
{
36+
Dictionary<double, int> dict = new Dictionary<double, int>();
37+
int len = points.GetUpperBound(0);
38+
int rtnCnt = 0;
39+
for (int i = 0; i <= len; i++)
40+
{
41+
//3点变2点
42+
for (int j = 0; j <= len; j++)
43+
{
44+
if (i == j) continue;
45+
double d = distance(points[i, 0], points[j, 0], points[i, 1], points[j, 1]);
46+
if (dict.ContainsKey(d))
47+
dict[d]++;
48+
else dict.Add(d, 1);
49+
}
50+
foreach (var item in dict)
51+
{
52+
if (item.Value > 1)
53+
{
54+
//如果找到了value个,因为有顺序,所以排序
55+
rtnCnt += item.Value * (item.Value - 1);
56+
}
57+
}
58+
dict.Clear();
59+
}
60+
return rtnCnt;
61+
}
62+
63+
private static double distance(int x1, int x2, int y1, int y2)
64+
{
65+
int x = x1 - x2;
66+
int y = y1 - y2;
67+
68+
return Math.Sqrt(x * x + y * y);
69+
}
70+
#endregion
71+
72+
}
73+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{2DAA0642-6344-4AAA-A001-A72C0E416B99}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>HashTable.Lib</RootNamespace>
12+
<AssemblyName>HashTable.Lib</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="Boomerangs.cs" />
44+
<Compile Include="LongestPalindrome.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<Compile Include="SingleOneSln.cs" />
47+
<Compile Include="TwoSumSln.cs" />
48+
</ItemGroup>
49+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
50+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
51+
Other similar extension points exist, see Microsoft.Common.targets.
52+
<Target Name="BeforeBuild">
53+
</Target>
54+
<Target Name="AfterBuild">
55+
</Target>
56+
-->
57+
</Project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* ==============================================================================
2+
* 功能描述:LongestPalindrome
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:28:39
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
//Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
11+
12+
//This is case sensitive, for example “Aa” is not considered a palindrome here.
13+
14+
//Note:
15+
//Assume the length of given string will not exceed 1,010.
16+
17+
//Example:
18+
19+
//Input:
20+
//"abccccdd"
21+
22+
//Output:
23+
//7
24+
25+
//Explanation:
26+
//One longest palindrome that can be built is "dccaccd", whose length is 7.
27+
namespace HashTable.Lib
28+
{
29+
/// <summary>
30+
/// LongestPalindrome
31+
/// </summary>
32+
public class LongestPalindrome
33+
{
34+
35+
#region 公有方法
36+
//借助哈希表,或字典,求某个字符出现的个数,若为偶数,则取字符串中此字符所有个数,
37+
//若为奇数,个数减1即可,只保留一个奇数不减去1。
38+
public int NumberOfPalindrome(string s)
39+
{
40+
HashSet<int> hash = new HashSet<int>();
41+
int count = 0;
42+
foreach (var item in s)
43+
{
44+
if (hash.Contains(item))
45+
{
46+
hash.Remove(item); //配对成功,
47+
count++; //count加1
48+
}
49+
else
50+
hash.Add(item);
51+
}
52+
return hash.Count > 0 ? count * 2 + 1 : count * 2;
53+
}
54+
55+
public int NumberOfPalindrome2(string s)
56+
{
57+
Dictionary<char, int> dict = s.GroupBy(item => item).ToDictionary(g => g.Key, g => g.ToArray().Count());
58+
int rtn = dict.Where(item => item.Value % 2 == 0).Sum(item => item.Value);
59+
var oddList = dict.Where(item => item.Value % 2 == 1);
60+
int oddCount = oddList.Count();
61+
if (oddCount > 0)
62+
return rtn + oddList.Sum(r => r.Value) - oddCount + 1;
63+
return rtn;
64+
}
65+
66+
#endregion
67+
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的常规信息通过以下
6+
// 特性集控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("HashTable.Lib")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("HashTable.Lib")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 使此程序集中的类型
18+
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
19+
// 则将该类型上的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("668e90bd-68c0-45eb-b664-592ec4a86b6b")]
24+
25+
// 程序集的版本信息由下面四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 内部版本号
30+
// 修订号
31+
//
32+
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* ==============================================================================
2+
* 功能描述:Solution
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:18:07
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace HashTable.Lib
12+
{
13+
/// <summary>
14+
/// Solution
15+
/// </summary>
16+
public class SingleOneSln
17+
{
18+
19+
#region 公有方法
20+
//Given an array of integers, every element appears twice except for one. Find that single one.
21+
public static int SingleNumber1(int[] nums)
22+
{
23+
HashSet<int> hash = new HashSet<int>();
24+
foreach (int item in nums)
25+
{
26+
if (hash.Contains(item))
27+
hash.Remove(item);
28+
else
29+
hash.Add(item);
30+
}
31+
return hash.Min();
32+
}
33+
34+
public static int SingleNumber2(int[] nums)
35+
{
36+
int rslt = nums[0];
37+
for (int i = 1; i < nums.Length; i++)
38+
{
39+
rslt ^= nums[i];
40+
}
41+
return rslt;
42+
}
43+
44+
45+
46+
#endregion
47+
48+
}
49+
}

HashTable/HashTable.Lib/TwoSumSln.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* ==============================================================================
2+
* 功能描述:TwoSum
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/28 18:20:39
5+
* ==============================================================================*/
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
11+
namespace HashTable.Lib
12+
{
13+
//Given an array of integers, return indices of the two numbers such that they add up to a specific target.
14+
15+
//You may assume that each input would have exactly one solution, and you may not use the same element twice.
16+
/// <summary>
17+
/// TwoSum
18+
/// </summary>
19+
public class TwoSumSln
20+
{
21+
22+
#region 公有方法
23+
24+
public int[] TwoSum1(int[] nums, int target)
25+
{
26+
Dictionary<int, int> dict = new Dictionary<int, int>();
27+
for (int i = 0; i < nums.Length; i++)
28+
{
29+
if (dict.ContainsKey(target - nums[i]))
30+
return new int[] { dict[target - nums[i]], i };
31+
else
32+
{
33+
if (!dict.ContainsKey(nums[i])) //同一个元素不能用两次
34+
dict.Add(nums[i], i);
35+
}
36+
37+
}
38+
return null;
39+
}
40+
41+
//暴力版本
42+
public int[] TwoSum2(int[] nums, int target)
43+
{
44+
for (int i = 0; i < nums.Length; i++)
45+
{
46+
for (int j = i + 1; j < nums.Length; j++)
47+
{
48+
if (nums[i] + nums[j] == target)
49+
return new int[] { i, j };
50+
}
51+
}
52+
return null;
53+
54+
}
55+
56+
#endregion
57+
58+
}
59+
}

HashTable/HashTable.sln

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HashTable", "HashTable\HashTable.csproj", "{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HashTable.Lib", "HashTable.Lib\HashTable.Lib.csproj", "{2DAA0642-6344-4AAA-A001-A72C0E416B99}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|Mixed Platforms = Debug|Mixed Platforms
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|Mixed Platforms = Release|Mixed Platforms
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Debug|Any CPU.ActiveCfg = Debug|x86
19+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
20+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Debug|Mixed Platforms.Build.0 = Debug|x86
21+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Debug|x86.ActiveCfg = Debug|x86
22+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Debug|x86.Build.0 = Debug|x86
23+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Release|Any CPU.ActiveCfg = Release|x86
24+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Release|Mixed Platforms.ActiveCfg = Release|x86
25+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Release|Mixed Platforms.Build.0 = Release|x86
26+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Release|x86.ActiveCfg = Release|x86
27+
{FE7A16ED-A5A1-40D0-A2C6-D690473A6D39}.Release|x86.Build.0 = Release|x86
28+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
31+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
32+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Debug|x86.ActiveCfg = Debug|Any CPU
33+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
36+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Release|Mixed Platforms.Build.0 = Release|Any CPU
37+
{2DAA0642-6344-4AAA-A001-A72C0E416B99}.Release|x86.ActiveCfg = Release|Any CPU
38+
EndGlobalSection
39+
GlobalSection(SolutionProperties) = preSolution
40+
HideSolutionNode = FALSE
41+
EndGlobalSection
42+
EndGlobal

HashTable/HashTable.suo

26.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)