Skip to content

Commit 4489eb4

Browse files
committed
新增方法一
1 parent 7115420 commit 4489eb4

File tree

6 files changed

+179
-2
lines changed

6 files changed

+179
-2
lines changed

ConatainsDuplicate/ConatainsDuplicate/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ static void Main(string[] args)
2323
Console.WriteLine("该数组有重复的元素!");
2424
else
2525
Console.WriteLine("该数组没有重复的元素!");
26-
2726
Console.Read();
2827
}
2928

@@ -46,7 +45,7 @@ static bool ContainsDupliacate(int[] nums)
4645
// 为什么除以8 ? 考虑到 byte 是8位
4746
int i = item / 8;
4847
int j = item % 8;
49-
byte check =(byte) (1 << j);
48+
byte check = (byte)(1 << j);
5049
if ((table[i] & check) != 0)
5150
{
5251
return true;

MiniMoves_453/MiniMoves_453.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniMoves_453", "MiniMoves_453\MiniMoves_453.csproj", "{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}"
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+
{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
</configuration>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{9BAE2DDC-1B47-4C88-A679-EA65D46F9AFE}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>MiniMoves_453</RootNamespace>
11+
<AssemblyName>MiniMoves_453</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
/**
8+
* Given a non-empty integer array of size n,
9+
* find the minimum number of moves required to make all array elements equal,
10+
* where a move is incrementing n - 1 elements by 1.
11+
* Example:
12+
13+
Input:
14+
[1,2,3]
15+
16+
Output:
17+
3
18+
19+
Explanation:
20+
Only three moves are needed (remember each move increments two elements):
21+
22+
[1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
23+
*/
24+
25+
namespace MiniMoves_453
26+
{
27+
class Program
28+
{
29+
static void Main(string[] args)
30+
{
31+
int[] nums = new int[] { 5, 2, 3, 2, 6 };
32+
Console.WriteLine("要使数组元素都相等,至少需要移动{0}步!", MiniMoves(nums));
33+
Console.Read();
34+
}
35+
36+
static int MiniMoves(int[] nums)
37+
{
38+
/*方法一*/
39+
// 将每次 n - 1个元素相加转化成
40+
// 每次将一个元素相减
41+
if (nums.Length <= 1)
42+
{
43+
return 0;
44+
}
45+
Array.Sort(nums);
46+
int sum = 0;
47+
for (int i = 1; i < nums.Length; i++)
48+
{
49+
sum += nums[i] - nums[0];
50+
}
51+
return sum;
52+
}
53+
}
54+
}
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("MiniMoves_453")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("MiniMoves_453")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
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("9bae2ddc-1b47-4c88-a679-ea65d46f9afe")]
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")]

0 commit comments

Comments
 (0)