diff --git a/.idea/.idea.Leetcode/.idea/workspace.xml b/.idea/.idea.Leetcode/.idea/workspace.xml index 23258ae..0c61c7b 100644 --- a/.idea/.idea.Leetcode/.idea/workspace.xml +++ b/.idea/.idea.Leetcode/.idea/workspace.xml @@ -30,6 +30,7 @@ 206. Reverse Linked List/206. Reverse Linked List.csproj 21. Merge Two Sorted Lists/21. Merge Two Sorted Lists.csproj 217. Contains Duplicate/217. Contains Duplicate.csproj + 225. Implement Stack using Queues/225. Implement Stack using Queues.csproj 226. Invert Binary Tree/226. Invert Binary Tree.csproj 231. Power of Two/231. Power of Two.csproj 232. Implement Queue using Stacks/232. Implement Queue using Stacks.csproj @@ -82,22 +83,14 @@ 98. Validate Binary Search Tree/98. Validate Binary Search Tree.csproj 994. Rotting Oranges/994. Rotting Oranges.csproj + + - - - - - - - - - - - - - - + + + @@ -106,13 +99,23 @@ + + - - + + + + + + + @@ -123,24 +126,30 @@ - + + + + + + + + + + @@ -1592,6 +1642,7 @@ 1678030074860 + diff --git a/14. Longest Common Prefix/14. Longest Common Prefix.csproj b/14. Longest Common Prefix/14. Longest Common Prefix.csproj new file mode 100644 index 0000000..7af9c89 --- /dev/null +++ b/14. Longest Common Prefix/14. Longest Common Prefix.csproj @@ -0,0 +1,11 @@ + + + + Exe + net8.0 + _14._Longest_Common_Prefix + enable + enable + + + diff --git a/14. Longest Common Prefix/Program.cs b/14. Longest Common Prefix/Program.cs new file mode 100644 index 0000000..4dbd757 --- /dev/null +++ b/14. Longest Common Prefix/Program.cs @@ -0,0 +1,6 @@ +// See https://aka.ms/new-console-template for more information + +using _14._Longest_Common_Prefix; + +string[] strs = ["flower", "flow", "flight"]; +Console.WriteLine(new Solution().LongestCommonPrefix(strs)); \ No newline at end of file diff --git a/14. Longest Common Prefix/Solution.cs b/14. Longest Common Prefix/Solution.cs new file mode 100644 index 0000000..66b8f2d --- /dev/null +++ b/14. Longest Common Prefix/Solution.cs @@ -0,0 +1,32 @@ +using System.Text; + +namespace _14._Longest_Common_Prefix; + +public class Solution +{ + public string LongestCommonPrefix(string[] strs) + { + if (strs.Length == 1) + return strs[0]; + StringBuilder sb = new StringBuilder(256); + int maxLen = strs.Select(s => s.Length).Min(); + for (int i = 0; i < maxLen; i++) + { + char letter; + if ((letter = strs[0][i]) != strs[1][i]) + break; + for (int j = 2; j < strs.Length; j++) + { + if (letter != strs[j][i]) + { + maxLen = -1; + break; + } + } + if(maxLen != -1) + sb.Append(letter); + } + + return sb.ToString(); + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index a7f1be6..b20c052 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -165,6 +165,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "15. 3Sum", "15. 3Sum\15. 3S EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "225. Implement Stack using Queues", "225. Implement Stack using Queues\225. Implement Stack using Queues.csproj", "{4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "14. Longest Common Prefix", "14. Longest Common Prefix\14. Longest Common Prefix.csproj", "{E1C1A088-861F-4E6D-A3D4-631C3B207A5F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -495,6 +497,10 @@ Global {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Debug|Any CPU.Build.0 = Debug|Any CPU {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.ActiveCfg = Release|Any CPU {4DF0467F-C3C9-4F92-9EE7-D21AD30F559D}.Release|Any CPU.Build.0 = Release|Any CPU + {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E1C1A088-861F-4E6D-A3D4-631C3B207A5F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE