From 75d2db5b75c643504789415f149e60f663cf9e70 Mon Sep 17 00:00:00 2001 From: nullptroma Date: Thu, 6 Apr 2023 22:17:31 +0300 Subject: [PATCH] Two Tasks --- .idea/.idea.Leetcode/.idea/workspace.xml | 67 ++++++++++++++++++- .../.idea/encodings.xml | 4 ++ .../.idea/indexLayout.xml | 8 +++ .../.idea/projectSettingsUpdater.xml | 6 ++ .../.idea/workspace.xml | 64 ++++++++++++++++++ 15. 3Sum/15. 3Sum.csproj | 8 +++ 15. 3Sum/Program.cs | 15 +++++ 15. 3Sum/Solution.cs | 63 +++++++++++++++++ .../.idea/encodings.xml | 4 ++ .../.idea/indexLayout.xml | 8 +++ .../.idea/projectSettingsUpdater.xml | 6 ++ .../.idea/workspace.xml | 64 ++++++++++++++++++ .../8. String to Integer (atoi).csproj | 8 +++ 8. String to Integer (atoi)/Program.cs | 11 +++ 8. String to Integer (atoi)/Solution.cs | 27 ++++++++ Leetcode.sln | 12 ++++ 16 files changed, 373 insertions(+), 2 deletions(-) create mode 100644 15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml create mode 100644 15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml create mode 100644 15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml create mode 100644 15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml create mode 100644 15. 3Sum/15. 3Sum.csproj create mode 100644 15. 3Sum/Program.cs create mode 100644 15. 3Sum/Solution.cs create mode 100644 8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml create mode 100644 8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml create mode 100644 8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml create mode 100644 8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml create mode 100644 8. String to Integer (atoi)/8. String to Integer (atoi).csproj create mode 100644 8. String to Integer (atoi)/Program.cs create mode 100644 8. String to Integer (atoi)/Solution.cs diff --git a/.idea/.idea.Leetcode/.idea/workspace.xml b/.idea/.idea.Leetcode/.idea/workspace.xml index a3c422c..23258ae 100644 --- a/.idea/.idea.Leetcode/.idea/workspace.xml +++ b/.idea/.idea.Leetcode/.idea/workspace.xml @@ -15,6 +15,7 @@ 144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.csproj 145. Binary Tree Postorder Traversal/145. Binary Tree Postorder Traversal.csproj 1480. Running Sum of 1d Array/1480. Running Sum of 1d Array.csproj + 15. 3Sum/15. 3Sum.csproj 167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.csproj 189. Rotate Array/189. Rotate Array.csproj 19. Remove Nth Node From End of List/19. Remove Nth Node From End of List.csproj @@ -72,6 +73,7 @@ 746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.csproj 77. Combinations/77. Combinations.csproj 784. Letter Case Permutation/784. Letter Case Permutation.csproj + 8. String to Integer (atoi)/8. String to Integer (atoi).csproj 83. Remove Duplicates from Sorted List/83. Remove Duplicates from Sorted List.csproj 876. Middle of the Linked List/876. Middle of the Linked List.csproj 88. Merge Sorted Array/88. Merge Sorted Array.csproj @@ -82,6 +84,20 @@ + + + + + + + + + + + + + + @@ -90,20 +106,29 @@ + + + + + + + - + + + + + @@ -1538,6 +1600,7 @@ + \ No newline at end of file diff --git a/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml new file mode 100644 index 0000000..4e22bfc --- /dev/null +++ b/15. 3Sum/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml @@ -0,0 +1,64 @@ + + + + _15._3Sum.csproj + + + + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "WebServerToolWindowFactoryState": "false", + "settings.editor.selected.configurable": "SolutionBuilderGeneralOptionsPage", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + 1679665701935 + + + + + + + \ No newline at end of file diff --git a/15. 3Sum/15. 3Sum.csproj b/15. 3Sum/15. 3Sum.csproj new file mode 100644 index 0000000..82cad6d --- /dev/null +++ b/15. 3Sum/15. 3Sum.csproj @@ -0,0 +1,8 @@ + + + + Exe + net7.0 + + + diff --git a/15. 3Sum/Program.cs b/15. 3Sum/Program.cs new file mode 100644 index 0000000..bcfb1b1 --- /dev/null +++ b/15. 3Sum/Program.cs @@ -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)); + } + } +} diff --git a/15. 3Sum/Solution.cs b/15. 3Sum/Solution.cs new file mode 100644 index 0000000..d54d630 --- /dev/null +++ b/15. 3Sum/Solution.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace _15._3Sum; + +public class Solution { + + class SequenceComparer:IEqualityComparer> + { + public bool Equals(IEnumerable seq1,IEnumerable seq2) + { + return seq1.SequenceEqual(seq2); + } + + public int GetHashCode(IEnumerable seq) + { + int hash = 1234567; + foreach(T elem in seq) + hash = unchecked(hash * 37 + elem.GetHashCode()); + return hash; + } + } + + public IList> ThreeSum(int[] nums) + { + Dictionary dict = new Dictionary(); + foreach (var num in nums) + { + if (dict.ContainsKey(num)) + dict[num]++; + else + dict.Add(num, 1); + } + HashSet> answer = new HashSet>(new SequenceComparer()); + 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() { v1, v2, v3 }; + list.Sort(); + answer.Add(list); + } + countI = dict[keys[i]]-1; + } + } + + return answer.ToList(); + } +} \ No newline at end of file diff --git a/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml new file mode 100644 index 0000000..4bb9f4d --- /dev/null +++ b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/projectSettingsUpdater.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml new file mode 100644 index 0000000..5f2a93a --- /dev/null +++ b/8. String to Integer (atoi)/.idea/.idea.OldConsoleTemplate.dir/.idea/workspace.xml @@ -0,0 +1,64 @@ + + + + _8._String_to_Integer__atoi_.csproj + + + + + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "WebServerToolWindowFactoryState": "false", + "settings.editor.selected.configurable": "SolutionBuilderGeneralOptionsPage", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + 1679665701935 + + + + + + + \ No newline at end of file diff --git a/8. String to Integer (atoi)/8. String to Integer (atoi).csproj b/8. String to Integer (atoi)/8. String to Integer (atoi).csproj new file mode 100644 index 0000000..82cad6d --- /dev/null +++ b/8. String to Integer (atoi)/8. String to Integer (atoi).csproj @@ -0,0 +1,8 @@ + + + + Exe + net7.0 + + + diff --git a/8. String to Integer (atoi)/Program.cs b/8. String to Integer (atoi)/Program.cs new file mode 100644 index 0000000..cb85a23 --- /dev/null +++ b/8. String to Integer (atoi)/Program.cs @@ -0,0 +1,11 @@ +using System; + +namespace _8._String_to_Integer__atoi_; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().MyAtoi(" ")); + } +} \ No newline at end of file diff --git a/8. String to Integer (atoi)/Solution.cs b/8. String to Integer (atoi)/Solution.cs new file mode 100644 index 0000000..a85714c --- /dev/null +++ b/8. String to Integer (atoi)/Solution.cs @@ -0,0 +1,27 @@ +using System.Numerics; + +namespace _8._String_to_Integer__atoi_; + +public class Solution { + public int MyAtoi(string s) + { + if (string.IsNullOrEmpty(s)) + return 0; + BigInteger res = 0; + int index = 0; + while (index int.MaxValue) + return int.MaxValue; + if (res < int.MinValue) + return int.MinValue; + return (int)res; + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index b3afe4a..5007126 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -159,6 +159,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "4. Median of Two Sorted Arr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "6. Zigzag Conversion", "6. Zigzag Conversion\6. Zigzag Conversion.csproj", "{4C15CB37-485C-4CD0-9BD4-D713CC744ECC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "8. String to Integer (atoi)", "8. String to Integer (atoi)\8. String to Integer (atoi).csproj", "{D6AEC02C-3EF2-4BE8-9C13-7F511FC9F9E7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "15. 3Sum", "15. 3Sum\15. 3Sum.csproj", "{4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -477,6 +481,14 @@ Global {4C15CB37-485C-4CD0-9BD4-D713CC744ECC}.Debug|Any CPU.Build.0 = Debug|Any CPU {4C15CB37-485C-4CD0-9BD4-D713CC744ECC}.Release|Any CPU.ActiveCfg = Release|Any CPU {4C15CB37-485C-4CD0-9BD4-D713CC744ECC}.Release|Any CPU.Build.0 = Release|Any CPU + {D6AEC02C-3EF2-4BE8-9C13-7F511FC9F9E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6AEC02C-3EF2-4BE8-9C13-7F511FC9F9E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6AEC02C-3EF2-4BE8-9C13-7F511FC9F9E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6AEC02C-3EF2-4BE8-9C13-7F511FC9F9E7}.Release|Any CPU.Build.0 = Release|Any CPU + {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4FAFB0B1-A240-44B9-8305-3CA7FED8AE3C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE