diff --git a/167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.csproj b/167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.csproj new file mode 100644 index 0000000..fc07939 --- /dev/null +++ b/167. Two Sum II - Input Array Is Sorted/167. Two Sum II - Input Array Is Sorted.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _167._Two_Sum_II___Input_Array_Is_Sorted + enable + enable + + + diff --git a/167. Two Sum II - Input Array Is Sorted/Program.cs b/167. Two Sum II - Input Array Is Sorted/Program.cs new file mode 100644 index 0000000..48cf9cd --- /dev/null +++ b/167. Two Sum II - Input Array Is Sorted/Program.cs @@ -0,0 +1,12 @@ +namespace _167._Two_Sum_II___Input_Array_Is_Sorted +{ + internal class Program + { + static void Main(string[] args) + { + var arr = new int[] { -2,-3,4 }; + var res = new Solution().TwoSum(arr, -5); + Console.WriteLine(String.Join(" ", res)); + } + } +} \ No newline at end of file diff --git a/167. Two Sum II - Input Array Is Sorted/Solution.cs b/167. Two Sum II - Input Array Is Sorted/Solution.cs new file mode 100644 index 0000000..13259e1 --- /dev/null +++ b/167. Two Sum II - Input Array Is Sorted/Solution.cs @@ -0,0 +1,46 @@ +namespace _167._Two_Sum_II___Input_Array_Is_Sorted; + +public class Solution +{ + public int[]? TwoSum(int[] numbers, int target) + { + for(int i = 0; i < numbers.Length; i++) + { + int cur = numbers[i]; + int nextTar = target - cur; + int index = Search(numbers, nextTar); + if (i == index || index < 0 || index >= numbers.Length) + continue; + int val = numbers[index]; + if (cur + val == target) + { + if(i>index) + { + i = i + index; + index = i - index; + i = i - index; + } + return new int[] { i + 1, index + 1 }; + } + } + return null; + } + + public int Search(int[] nums, int target) + { + int l = -1; + int r = nums.Length; + while (l < r - 1) + { + int i = l + (r - l) / 2; + int cur = nums[i]; + if (cur == target) + return i; + if (cur > target) + r = i; + else + l = i; + } + return -1; + } +} \ No newline at end of file diff --git a/240. Search a 2D Matrix II/240. Search a 2D Matrix II.csproj b/240. Search a 2D Matrix II/240. Search a 2D Matrix II.csproj new file mode 100644 index 0000000..9047a66 --- /dev/null +++ b/240. Search a 2D Matrix II/240. Search a 2D Matrix II.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _240._Search_a_2D_Matrix_II + enable + enable + + + diff --git a/240. Search a 2D Matrix II/Program.cs b/240. Search a 2D Matrix II/Program.cs new file mode 100644 index 0000000..01973cb --- /dev/null +++ b/240. Search a 2D Matrix II/Program.cs @@ -0,0 +1,11 @@ +namespace _240._Search_a_2D_Matrix_II +{ + internal class Program + { + static void Main(string[] args) + { + int[][] matrix = new int[][] { new int[] { 1, 1,1,1 } }; + Console.WriteLine(new Solution().SearchMatrix(matrix, 2)); + } + } +} \ No newline at end of file diff --git a/240. Search a 2D Matrix II/Solution.cs b/240. Search a 2D Matrix II/Solution.cs new file mode 100644 index 0000000..3374c31 --- /dev/null +++ b/240. Search a 2D Matrix II/Solution.cs @@ -0,0 +1,30 @@ +namespace _240._Search_a_2D_Matrix_II; + +public class Solution +{ + public bool SearchMatrix(int[][] matrix, int target) + { + for (int row = 0; row < matrix.Length; row++) + { + int column = SearchInsert((i) => matrix[row][i], matrix[row].Length, target); + if (column < matrix[row].Length && matrix[row][column] == target) + return true; + } + return false; + } + + private int SearchInsert(Func getByIndex, int len, int target) + { + int l = -1; + int r = len; + while (l != r - 1) + { + int i = (r + l) / 2; + if (getByIndex(i) >= target) + r = i; + else + l = i; + } + return l + 1; + } +} \ No newline at end of file diff --git a/283. Move Zeroes/283. Move Zeroes.csproj b/283. Move Zeroes/283. Move Zeroes.csproj new file mode 100644 index 0000000..da0c6f1 --- /dev/null +++ b/283. Move Zeroes/283. Move Zeroes.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _283._Move_Zeroes + enable + enable + + + diff --git a/283. Move Zeroes/Program.cs b/283. Move Zeroes/Program.cs new file mode 100644 index 0000000..ad49729 --- /dev/null +++ b/283. Move Zeroes/Program.cs @@ -0,0 +1,12 @@ +namespace _283._Move_Zeroes +{ + internal class Program + { + static void Main(string[] args) + { + var test = new int[] { 0, 1, 0, 3, 12 }; + new Solution().MoveZeroes(test); + Console.WriteLine(String.Join(" ", test)); + } + } +} \ No newline at end of file diff --git a/283. Move Zeroes/Solution.cs b/283. Move Zeroes/Solution.cs new file mode 100644 index 0000000..83a89bf --- /dev/null +++ b/283. Move Zeroes/Solution.cs @@ -0,0 +1,20 @@ +namespace _283._Move_Zeroes; + +public class Solution +{ + public void MoveZeroes(int[] nums) + { + for(int i = 0; i < nums.Length-1; i++) + { + if (nums[i] != 0) + continue; + int nonZeroIndex = i + 1; + while (nonZeroIndex < nums.Length && nums[nonZeroIndex] == 0) + nonZeroIndex++; + if (nonZeroIndex >= nums.Length) + break; + nums[i] = nums[nonZeroIndex]; + nums[nonZeroIndex] = 0; + } + } +} diff --git a/35. Search Insert Position/Program.cs b/35. Search Insert Position/Program.cs index 548b75d..2d84832 100644 --- a/35. Search Insert Position/Program.cs +++ b/35. Search Insert Position/Program.cs @@ -4,7 +4,6 @@ { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); } } } \ No newline at end of file diff --git a/36. Valid Sudoku/36. Valid Sudoku.csproj b/36. Valid Sudoku/36. Valid Sudoku.csproj new file mode 100644 index 0000000..ffc53b5 --- /dev/null +++ b/36. Valid Sudoku/36. Valid Sudoku.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _36._Valid_Sudoku + enable + enable + + + diff --git a/36. Valid Sudoku/Program.cs b/36. Valid Sudoku/Program.cs new file mode 100644 index 0000000..d8c003c --- /dev/null +++ b/36. Valid Sudoku/Program.cs @@ -0,0 +1,19 @@ +namespace _36._Valid_Sudoku +{ + internal class Program + { + static void Main(string[] args) + { + var arr = new char[][] { new char[]{'.', '.', '4', '.', '.', '.', '6', '3', '.'}, + new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'}, + new char[]{'5', '.', '.', '.', '.', '.', '.', '9', '.'}, + new char[]{'.', '.', '.', '5', '6', '.', '.', '.', '.'}, + new char[]{'4', '.', '3', '.', '.', '.', '.', '.', '1'}, + new char[]{'.', '.', '.', '7', '.', '.', '.', '.', '.'}, + new char[]{'.', '.', '.', '5', '.', '.', '.', '.', '.'}, + new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'}, + new char[]{'.', '.', '.', '.', '.', '.', '.', '.', '.'}}; + Console.WriteLine(new Solution().IsValidSudoku(arr)); + } + } +} \ No newline at end of file diff --git a/36. Valid Sudoku/Solution.cs b/36. Valid Sudoku/Solution.cs new file mode 100644 index 0000000..e8e0a5d --- /dev/null +++ b/36. Valid Sudoku/Solution.cs @@ -0,0 +1,80 @@ +namespace _36._Valid_Sudoku; + +public class Solution +{ + public bool IsValidSudoku(char[][] board) + { + return CheckRows(board) && CheckColumns(board) && CheckSubBoxes(board); + } + + private bool CheckRows(char[][] board) + { + foreach (char[] row in board) + { + HashSet rowSet = new HashSet(); + foreach (char c in row) + { + if(c != '.') + { + int prevCount = rowSet.Count; + rowSet.Add(c); + if (prevCount == rowSet.Count) + return false; + } + } + } + return true; + } + + private bool CheckColumns(char[][] board) + { + for(int columnIndex = 0; columnIndex < board[0].Length; columnIndex++) + { + HashSet column = new HashSet(); + for(int rowIndex = 0; rowIndex < board[columnIndex].Length; rowIndex++) + { + char ch = board[rowIndex][columnIndex]; + if (ch != '.') + { + int prevCount = column.Count; + column.Add(ch); + if (prevCount == column.Count) + return false; + } + } + } + return true; + } + + private bool CheckSubBoxes(char[][] board) + { + for(int i = 0; i < 27; i+=3) + { + int y = i / 9 * 3; + int x = i % 9; + if (CheckSubBox(board, y, x) == false) + return false; + } + return true; + } + + private bool CheckSubBox(char[][] board, int startY, int startX) + { + HashSet set = new HashSet(); + for (int y = startY; y < startY+3; y++) + { + for(int x = startX; x < startX+3; x++) + { + char ch = board[y][x]; + if(ch!='.') + { + int prev = set.Count; + set.Add(ch); + if (prev == set.Count) + return false; + } + } + } + return true; + } +} diff --git a/409. Longest Palindrome/409. Longest Palindrome.csproj b/409. Longest Palindrome/409. Longest Palindrome.csproj new file mode 100644 index 0000000..5283c84 --- /dev/null +++ b/409. Longest Palindrome/409. Longest Palindrome.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _409._Longest_Palindrome + enable + enable + + + diff --git a/409. Longest Palindrome/Program.cs b/409. Longest Palindrome/Program.cs new file mode 100644 index 0000000..f5dc844 --- /dev/null +++ b/409. Longest Palindrome/Program.cs @@ -0,0 +1,10 @@ +namespace _409._Longest_Palindrome +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine(new Solution().LongestPalindrome("bb")); + } + } +} \ No newline at end of file diff --git a/409. Longest Palindrome/Solution.cs b/409. Longest Palindrome/Solution.cs new file mode 100644 index 0000000..39fe09c --- /dev/null +++ b/409. Longest Palindrome/Solution.cs @@ -0,0 +1,25 @@ +namespace _409._Longest_Palindrome; + +public class Solution +{ + public int LongestPalindrome(string s) + { + Dictionary dict = new Dictionary(); + foreach(var ch in s) + { + if(dict.ContainsKey(ch)) + dict[ch]++; + else + dict.Add(ch, 1); + } + int sum = 0; + bool hasOdd = false; + foreach(var v in dict.Values) + { + sum += v / 2; + if(v % 2 == 1) + hasOdd = true; + } + return sum*2 + (hasOdd ? 1 : 0); + } +} diff --git a/74. Search a 2D Matrix/74. Search a 2D Matrix.csproj b/74. Search a 2D Matrix/74. Search a 2D Matrix.csproj new file mode 100644 index 0000000..78e5991 --- /dev/null +++ b/74. Search a 2D Matrix/74. Search a 2D Matrix.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _74._Search_a_2D_Matrix + enable + enable + + + diff --git a/74. Search a 2D Matrix/Program.cs b/74. Search a 2D Matrix/Program.cs new file mode 100644 index 0000000..3b81f46 --- /dev/null +++ b/74. Search a 2D Matrix/Program.cs @@ -0,0 +1,11 @@ +namespace _74._Search_a_2D_Matrix +{ + internal class Program + { + static void Main(string[] args) + { + int[][] matrix = new int[][] { new int[] { 1 }}; + Console.WriteLine(new Solution().SearchMatrix(matrix, 1)); + } + } +} \ No newline at end of file diff --git a/74. Search a 2D Matrix/Solution.cs b/74. Search a 2D Matrix/Solution.cs new file mode 100644 index 0000000..6912cda --- /dev/null +++ b/74. Search a 2D Matrix/Solution.cs @@ -0,0 +1,30 @@ +namespace _74._Search_a_2D_Matrix; + +public class Solution +{ + public bool SearchMatrix(int[][] matrix, int target) + { + int row = SearchInsert((i) => matrix[i][0], matrix.Length, target); + if (row < matrix.Length && matrix[row][0] == target) + return true; + if (row > 0) + row--; + int column = SearchInsert((i) => matrix[row][i], matrix[row].Length, target); + return column < matrix[row].Length && matrix[row][column] == target; + } + + private int SearchInsert(Func getByIndex, int len, int target) + { + int l = -1; + int r = len; + while (l != r - 1) + { + int i = (r + l) / 2; + if (getByIndex(i) >= target) + r = i; + else + l = i; + } + return l + 1; + } +} diff --git a/Leetcode.sln b/Leetcode.sln index 80d2b4d..392e742 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -33,17 +33,29 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "278. First Bad Version", "2 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "35. Search Insert Position", "35. Search Insert Position\35. Search Insert Position.csproj", "{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "876. Middle of the Linked List", "876. Middle of the Linked List\876. Middle of the Linked List.csproj", "{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "876. Middle of the Linked List", "876. Middle of the Linked List\876. Middle of the Linked List.csproj", "{2510B3B5-87EE-44AB-A2A7-BCAE9E6E91C6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "566. Reshape the Matrix", "566. Reshape the Matrix\566. Reshape the Matrix.csproj", "{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "566. Reshape the Matrix", "566. Reshape the Matrix\566. Reshape the Matrix.csproj", "{DD4A53CF-D840-48BF-8F8F-33E39D492BD3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "118. Pascal's Triangle", "118. Pascal's Triangle\118. Pascal's Triangle.csproj", "{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "118. Pascal's Triangle", "118. Pascal's Triangle\118. Pascal's Triangle.csproj", "{E1BB66AE-E4E6-441F-9B5C-1B7FFDF7912C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "977. Squares of a Sorted Array", "977. Squares of a Sorted Array\977. Squares of a Sorted Array.csproj", "{F0B9DD20-6230-4381-8814-072C2E159624}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "977. Squares of a Sorted Array", "977. Squares of a Sorted Array\977. Squares of a Sorted Array.csproj", "{F0B9DD20-6230-4381-8814-072C2E159624}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "189. Rotate Array", "189. Rotate Array\189. Rotate Array.csproj", "{CAF5FFD6-9598-412D-8569-EB62136D34C8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "189. Rotate Array", "189. Rotate Array\189. Rotate Array.csproj", "{CAF5FFD6-9598-412D-8569-EB62136D34C8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "142. Linked List Cycle II", "142. Linked List Cycle II\142. Linked List Cycle II.csproj", "{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "142. Linked List Cycle II", "142. Linked List Cycle II\142. Linked List Cycle II.csproj", "{EBA96217-EAD0-456C-B1C6-79A7A227ED0A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "283. Move Zeroes", "283. Move Zeroes\283. Move Zeroes.csproj", "{E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "167. Two Sum II - Input Array Is Sorted", "167. Two Sum II - Input Array Is Sorted\167. Two Sum II - Input Array Is Sorted.csproj", "{9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "74. Search a 2D Matrix", "74. Search a 2D Matrix\74. Search a 2D Matrix.csproj", "{8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "240. Search a 2D Matrix II", "240. Search a 2D Matrix II\240. Search a 2D Matrix II.csproj", "{F47BA875-AF2F-4D00-AB75-A62A6E31387C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "409. Longest Palindrome", "409. Longest Palindrome\409. Longest Palindrome.csproj", "{F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "36. Valid Sudoku", "36. Valid Sudoku\36. Valid Sudoku.csproj", "{C1759C38-D2E4-4869-B6C2-259B26586E11}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -135,6 +147,30 @@ Global {EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Debug|Any CPU.Build.0 = Debug|Any CPU {EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Release|Any CPU.ActiveCfg = Release|Any CPU {EBA96217-EAD0-456C-B1C6-79A7A227ED0A}.Release|Any CPU.Build.0 = Release|Any CPU + {E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E3D6C525-24EE-4FA3-B2E4-AC8397F7BB1F}.Release|Any CPU.Build.0 = Release|Any CPU + {9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F76EF2C-CAF7-470E-A3A4-195D10B9FCD3}.Release|Any CPU.Build.0 = Release|Any CPU + {8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8B14DDDD-DCEF-4EF6-B2B4-5A6D525E1E18}.Release|Any CPU.Build.0 = Release|Any CPU + {F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F47BA875-AF2F-4D00-AB75-A62A6E31387C}.Release|Any CPU.Build.0 = Release|Any CPU + {F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F40EDCD6-7BEC-4F1E-BE95-8CD91389122F}.Release|Any CPU.Build.0 = Release|Any CPU + {C1759C38-D2E4-4869-B6C2-259B26586E11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C1759C38-D2E4-4869-B6C2-259B26586E11}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C1759C38-D2E4-4869-B6C2-259B26586E11}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C1759C38-D2E4-4869-B6C2-259B26586E11}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE