Two Tasks
This commit is contained in:
4
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml
generated
Normal file
4
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||
</project>
|
||||
8
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml
generated
Normal file
8
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="UserContentModel">
|
||||
<attachedFolders />
|
||||
<explicitIncludes />
|
||||
<explicitExcludes />
|
||||
</component>
|
||||
</project>
|
||||
6
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml
generated
Normal file
6
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RiderProjectSettingsUpdater">
|
||||
<option name="vcsConfiguration" value="2" />
|
||||
</component>
|
||||
</project>
|
||||
64
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml
generated
Normal file
64
15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml
generated
Normal file
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AutoGeneratedRunConfigurationManager">
|
||||
<projectFile>_15._3Sum.csproj</projectFile>
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="82866301-1c54-48b2-9c86-912478ff5746" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="ProjectId" id="2NSm8ypYphrjURy5OrzoEQDZIRZ" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent">{
|
||||
"keyToString": {
|
||||
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"WebServerToolWindowFactoryState": "false",
|
||||
"settings.editor.selected.configurable": "SolutionBuilderGeneralOptionsPage",
|
||||
"vue.rearranger.settings.migration": "true"
|
||||
}
|
||||
}</component>
|
||||
<component name="RunManager">
|
||||
<configuration name="_15._3Sum" type="DotNetProject" factoryName=".NET Project">
|
||||
<option name="EXE_PATH" value="" />
|
||||
<option name="PROGRAM_PARAMETERS" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="" />
|
||||
<option name="PASS_PARENT_ENVS" value="1" />
|
||||
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||
<option name="USE_MONO" value="0" />
|
||||
<option name="RUNTIME_ARGUMENTS" value="" />
|
||||
<option name="PROJECT_PATH" value="$PROJECT_DIR$/_15._3Sum.csproj" />
|
||||
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
|
||||
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
|
||||
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
|
||||
<option name="PROJECT_KIND" value="DotNetCore" />
|
||||
<option name="PROJECT_TFM" value="" />
|
||||
<method v="2">
|
||||
<option name="Build" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="82866301-1c54-48b2-9c86-912478ff5746" name="Changes" comment="" />
|
||||
<created>1679665701935</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1679665701935</updated>
|
||||
<workItem from="1679665703808" duration="5000" />
|
||||
<workItem from="1680292231454" duration="4000" />
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="UnityUnitTestConfiguration" currentTestLauncher="NUnit" />
|
||||
<component name="VcsManagerConfiguration">
|
||||
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
|
||||
</component>
|
||||
</project>
|
||||
8
15. 3Sum/15. 3Sum.csproj
Normal file
8
15. 3Sum/15. 3Sum.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
15
15. 3Sum/Program.cs
Normal file
15
15. 3Sum/Program.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace _15._3Sum;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().ThreeSum(new int[]{-3,0,-5,2,-4,-2,1,-2,-1,3,1,3,1,3,-3,-5,1});
|
||||
foreach (var list in res)
|
||||
{
|
||||
Console.WriteLine(String.Join(" ", list));
|
||||
}
|
||||
}
|
||||
}
|
||||
63
15. 3Sum/Solution.cs
Normal file
63
15. 3Sum/Solution.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace _15._3Sum;
|
||||
|
||||
public class Solution {
|
||||
|
||||
class SequenceComparer<T>:IEqualityComparer<IEnumerable<T>>
|
||||
{
|
||||
public bool Equals(IEnumerable<T> seq1,IEnumerable<T> seq2)
|
||||
{
|
||||
return seq1.SequenceEqual(seq2);
|
||||
}
|
||||
|
||||
public int GetHashCode(IEnumerable<T> seq)
|
||||
{
|
||||
int hash = 1234567;
|
||||
foreach(T elem in seq)
|
||||
hash = unchecked(hash * 37 + elem.GetHashCode());
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
public IList<IList<int>> ThreeSum(int[] nums)
|
||||
{
|
||||
Dictionary<int, int> dict = new Dictionary<int, int>();
|
||||
foreach (var num in nums)
|
||||
{
|
||||
if (dict.ContainsKey(num))
|
||||
dict[num]++;
|
||||
else
|
||||
dict.Add(num, 1);
|
||||
}
|
||||
HashSet<IList<int>> answer = new HashSet<IList<int>>(new SequenceComparer<int>());
|
||||
int[] keys = dict.Keys.ToArray();
|
||||
for (int i = 0; i < keys.Length; i++)
|
||||
{
|
||||
int countI = dict[keys[i]]-1;
|
||||
for (int j = i; j < keys.Length; j++)
|
||||
{
|
||||
int countJ = j == i ? (countI-1) : (dict[keys[j]]-1);
|
||||
int v1 = keys[i];
|
||||
int v2 = keys[j];
|
||||
int v3 = -(v1+v2);
|
||||
if (v3 == v1)
|
||||
countI--;
|
||||
if (v3 == v2)
|
||||
countJ--;
|
||||
bool can = dict.ContainsKey(v3);
|
||||
if (can && countI >= 0 && countJ >= 0)
|
||||
{
|
||||
var list = new List<int>() { v1, v2, v3 };
|
||||
list.Sort();
|
||||
answer.Add(list);
|
||||
}
|
||||
countI = dict[keys[i]]-1;
|
||||
}
|
||||
}
|
||||
|
||||
return answer.ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user