From 284b6ff10c0e41ca06d6dc3b3063aac69fba16fb Mon Sep 17 00:00:00 2001 From: Electrominch <64524860+Electrominch@users.noreply.github.com> Date: Tue, 18 Oct 2022 00:45:31 +0300 Subject: [PATCH] Day i do not know --- 1. Two Sum/Program.cs | 2 +- 1. Two Sum/Solution.cs | 25 +++- .../101. Symmetric Tree.csproj | 11 ++ 101. Symmetric Tree/Program.cs | 13 ++ 101. Symmetric Tree/Solution.cs | 20 +++ 101. Symmetric Tree/TreeNode.cs | 17 +++ .../104. Maximum Depth of Binary Tree.csproj | 11 ++ 104. Maximum Depth of Binary Tree/Program.cs | 9 ++ 104. Maximum Depth of Binary Tree/Solution.cs | 26 ++++ 104. Maximum Depth of Binary Tree/TreeNode.cs | 15 ++ 112. Path Sum/112. Path Sum.csproj | 11 ++ 112. Path Sum/Program.cs | 9 ++ 112. Path Sum/Solution.cs | 21 +++ 112. Path Sum/TreeNode.cs | 16 ++ 136. Single Number/136. Single Number.csproj | 11 ++ 136. Single Number/Program.cs | 9 ++ 136. Single Number/Solution.cs | 12 ++ 190. Reverse Bits/190. Reverse Bits.csproj | 11 ++ 190. Reverse Bits/Program.cs | 9 ++ 190. Reverse Bits/Solution.cs | 17 +++ .../191. Number of 1 Bits.csproj | 11 ++ 191. Number of 1 Bits/Program.cs | 9 ++ 191. Number of 1 Bits/Solution.cs | 15 ++ 198. House Robber/198. House Robber.csproj | 11 ++ 198. House Robber/Program.cs | 9 ++ 198. House Robber/Solution.cs | 5 + .../226. Invert Binary Tree.csproj | 11 ++ 226. Invert Binary Tree/Program.cs | 9 ++ 226. Invert Binary Tree/Solution.cs | 23 +++ 226. Invert Binary Tree/TreeNode.cs | 16 ++ 231. Power of Two/231. Power of Two.csproj | 11 ++ 231. Power of Two/Program.cs | 9 ++ 231. Power of Two/Solution.cs | 10 ++ .../299. Bulls and Cows.csproj | 11 ++ 299. Bulls and Cows/Program.cs | 9 ++ 299. Bulls and Cows/Solution.cs | 50 +++++++ ...est Repeating Character Replacement.csproj | 11 ++ .../Program.cs | 9 ++ .../Solution.cs | 56 +++++++ .../438. Find All Anagrams in a String.csproj | 11 ++ 438. Find All Anagrams in a String/Program.cs | 10 ++ .../Solution.cs | 35 +++++ 46. Permutations/46. Permutations.csproj | 11 ++ 46. Permutations/Program.cs | 12 ++ 46. Permutations/Solution.cs | 30 ++++ 542. 01 Matrix/542. 01 Matrix.csproj | 11 ++ 542. 01 Matrix/Program.cs | 15 ++ 542. 01 Matrix/Solution.cs | 35 +++++ 62. Unique Paths/62. Unique Paths.csproj | 11 ++ 62. Unique Paths/Program.cs | 9 ++ 62. Unique Paths/Solution.cs | 29 ++++ ...700. Search in a Binary Search Tree.csproj | 11 ++ .../Program.cs | 9 ++ .../Solution.cs | 22 +++ .../TreeNode.cs | 15 ++ ...1. Insert into a Binary Search Tree.csproj | 11 ++ .../Program.cs | 9 ++ .../Solution.cs | 34 +++++ .../TreeNode.cs | 15 ++ .../746. Min Cost Climbing Stairs.csproj | 11 ++ 746. Min Cost Climbing Stairs/Program.cs | 9 ++ 746. Min Cost Climbing Stairs/Solution.cs | 17 +++ 77. Combinations/77. Combinations.csproj | 11 ++ 77. Combinations/Program.cs | 11 ++ 77. Combinations/Solution.cs | 26 ++++ .../784. Letter Case Permutation.csproj | 11 ++ 784. Letter Case Permutation/Program.cs | 9 ++ 784. Letter Case Permutation/Solution.cs | 28 ++++ .../994. Rotting Oranges.csproj | 11 ++ 994. Rotting Oranges/Program.cs | 9 ++ 994. Rotting Oranges/Solution.cs | 60 ++++++++ Leetcode.sln | 140 +++++++++++++++++- 72 files changed, 1256 insertions(+), 12 deletions(-) create mode 100644 101. Symmetric Tree/101. Symmetric Tree.csproj create mode 100644 101. Symmetric Tree/Program.cs create mode 100644 101. Symmetric Tree/Solution.cs create mode 100644 101. Symmetric Tree/TreeNode.cs create mode 100644 104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.csproj create mode 100644 104. Maximum Depth of Binary Tree/Program.cs create mode 100644 104. Maximum Depth of Binary Tree/Solution.cs create mode 100644 104. Maximum Depth of Binary Tree/TreeNode.cs create mode 100644 112. Path Sum/112. Path Sum.csproj create mode 100644 112. Path Sum/Program.cs create mode 100644 112. Path Sum/Solution.cs create mode 100644 112. Path Sum/TreeNode.cs create mode 100644 136. Single Number/136. Single Number.csproj create mode 100644 136. Single Number/Program.cs create mode 100644 136. Single Number/Solution.cs create mode 100644 190. Reverse Bits/190. Reverse Bits.csproj create mode 100644 190. Reverse Bits/Program.cs create mode 100644 190. Reverse Bits/Solution.cs create mode 100644 191. Number of 1 Bits/191. Number of 1 Bits.csproj create mode 100644 191. Number of 1 Bits/Program.cs create mode 100644 191. Number of 1 Bits/Solution.cs create mode 100644 198. House Robber/198. House Robber.csproj create mode 100644 198. House Robber/Program.cs create mode 100644 198. House Robber/Solution.cs create mode 100644 226. Invert Binary Tree/226. Invert Binary Tree.csproj create mode 100644 226. Invert Binary Tree/Program.cs create mode 100644 226. Invert Binary Tree/Solution.cs create mode 100644 226. Invert Binary Tree/TreeNode.cs create mode 100644 231. Power of Two/231. Power of Two.csproj create mode 100644 231. Power of Two/Program.cs create mode 100644 231. Power of Two/Solution.cs create mode 100644 299. Bulls and Cows/299. Bulls and Cows.csproj create mode 100644 299. Bulls and Cows/Program.cs create mode 100644 299. Bulls and Cows/Solution.cs create mode 100644 424. Longest Repeating Character Replacement/424. Longest Repeating Character Replacement.csproj create mode 100644 424. Longest Repeating Character Replacement/Program.cs create mode 100644 424. Longest Repeating Character Replacement/Solution.cs create mode 100644 438. Find All Anagrams in a String/438. Find All Anagrams in a String.csproj create mode 100644 438. Find All Anagrams in a String/Program.cs create mode 100644 438. Find All Anagrams in a String/Solution.cs create mode 100644 46. Permutations/46. Permutations.csproj create mode 100644 46. Permutations/Program.cs create mode 100644 46. Permutations/Solution.cs create mode 100644 542. 01 Matrix/542. 01 Matrix.csproj create mode 100644 542. 01 Matrix/Program.cs create mode 100644 542. 01 Matrix/Solution.cs create mode 100644 62. Unique Paths/62. Unique Paths.csproj create mode 100644 62. Unique Paths/Program.cs create mode 100644 62. Unique Paths/Solution.cs create mode 100644 700. Search in a Binary Search Tree/700. Search in a Binary Search Tree.csproj create mode 100644 700. Search in a Binary Search Tree/Program.cs create mode 100644 700. Search in a Binary Search Tree/Solution.cs create mode 100644 700. Search in a Binary Search Tree/TreeNode.cs create mode 100644 701. Insert into a Binary Search Tree/701. Insert into a Binary Search Tree.csproj create mode 100644 701. Insert into a Binary Search Tree/Program.cs create mode 100644 701. Insert into a Binary Search Tree/Solution.cs create mode 100644 701. Insert into a Binary Search Tree/TreeNode.cs create mode 100644 746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.csproj create mode 100644 746. Min Cost Climbing Stairs/Program.cs create mode 100644 746. Min Cost Climbing Stairs/Solution.cs create mode 100644 77. Combinations/77. Combinations.csproj create mode 100644 77. Combinations/Program.cs create mode 100644 77. Combinations/Solution.cs create mode 100644 784. Letter Case Permutation/784. Letter Case Permutation.csproj create mode 100644 784. Letter Case Permutation/Program.cs create mode 100644 784. Letter Case Permutation/Solution.cs create mode 100644 994. Rotting Oranges/994. Rotting Oranges.csproj create mode 100644 994. Rotting Oranges/Program.cs create mode 100644 994. Rotting Oranges/Solution.cs diff --git a/1. Two Sum/Program.cs b/1. Two Sum/Program.cs index 308b0eb..7c39998 100644 --- a/1. Two Sum/Program.cs +++ b/1. Two Sum/Program.cs @@ -4,6 +4,6 @@ internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + Console.WriteLine(String.Join(" ", new Solution().TwoSum(new int[] {3,2,4}, 6)??new int[0])); } } \ No newline at end of file diff --git a/1. Two Sum/Solution.cs b/1. Two Sum/Solution.cs index eaf8570..24cca64 100644 --- a/1. Two Sum/Solution.cs +++ b/1. Two Sum/Solution.cs @@ -4,10 +4,27 @@ public class Solution { public int[]? TwoSum(int[] nums, int target) { - for (int i = 0; i < nums.Length; i++) - for (int j = i + 1; j < nums.Length; j++) - if (nums[i] + nums[j] == target) - return new int[] { i, j }; + Dictionary dict = new Dictionary(); + int[] halfIndexes = new int[] { -1, -1 }; + int curHalfIndex = 0; + for(int i = 0; i < nums.Length; i++) + { + if (dict.ContainsKey(nums[i]) == false) + dict.Add(nums[i], i); + if (nums[i] == target / 2) + { + halfIndexes[curHalfIndex++] = i; + if (target % 2 == 0 && curHalfIndex == 2) + return halfIndexes; + } + } + + foreach (var kp in dict) + { + int need = target - kp.Key; + if (dict.ContainsKey(need) && (target % 2 == 1 || need != target/2)) + return new int[] { kp.Value, dict[need] }; + } return null; } } diff --git a/101. Symmetric Tree/101. Symmetric Tree.csproj b/101. Symmetric Tree/101. Symmetric Tree.csproj new file mode 100644 index 0000000..5457b27 --- /dev/null +++ b/101. Symmetric Tree/101. Symmetric Tree.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _101._Symmetric_Tree + enable + enable + + + diff --git a/101. Symmetric Tree/Program.cs b/101. Symmetric Tree/Program.cs new file mode 100644 index 0000000..5871a4d --- /dev/null +++ b/101. Symmetric Tree/Program.cs @@ -0,0 +1,13 @@ +namespace _101._Symmetric_Tree; + +internal class Program +{ + static void Main(string[] args) + { + var t = new TreeNode(3, + new TreeNode(4, new TreeNode(5, new TreeNode(6), null), null), + new TreeNode(4, null, new TreeNode(5, null, new TreeNode(6))) + ); + Console.WriteLine(new Solution().IsSymmetric(t)); + } +} diff --git a/101. Symmetric Tree/Solution.cs b/101. Symmetric Tree/Solution.cs new file mode 100644 index 0000000..45b9449 --- /dev/null +++ b/101. Symmetric Tree/Solution.cs @@ -0,0 +1,20 @@ +namespace _101._Symmetric_Tree; + +public class Solution +{ + public bool IsSymmetric(TreeNode root) + { + return Recursion(root.left, root.right); + } + + private bool Recursion(TreeNode? left, TreeNode? right) + { + if ((left == null) != (right == null)) + return false; + if (left == null || right == null) + return true; + if (left.val != right.val) + return false; + return Recursion(left.left, right.right) && Recursion(left.right, right.left); + } +} \ No newline at end of file diff --git a/101. Symmetric Tree/TreeNode.cs b/101. Symmetric Tree/TreeNode.cs new file mode 100644 index 0000000..b00e595 --- /dev/null +++ b/101. Symmetric Tree/TreeNode.cs @@ -0,0 +1,17 @@ +namespace _101._Symmetric_Tree; + + +//Definition for a binary tree node. +public class TreeNode +{ + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null) + { + this.val = val; + this.left = left; + this.right = right; + } +} + diff --git a/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.csproj b/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.csproj new file mode 100644 index 0000000..51a68bc --- /dev/null +++ b/104. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _104._Maximum_Depth_of_Binary_Tree + enable + enable + + + diff --git a/104. Maximum Depth of Binary Tree/Program.cs b/104. Maximum Depth of Binary Tree/Program.cs new file mode 100644 index 0000000..5d29885 --- /dev/null +++ b/104. Maximum Depth of Binary Tree/Program.cs @@ -0,0 +1,9 @@ +namespace _104._Maximum_Depth_of_Binary_Tree; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/104. Maximum Depth of Binary Tree/Solution.cs b/104. Maximum Depth of Binary Tree/Solution.cs new file mode 100644 index 0000000..974ea64 --- /dev/null +++ b/104. Maximum Depth of Binary Tree/Solution.cs @@ -0,0 +1,26 @@ +namespace _104._Maximum_Depth_of_Binary_Tree; + +public class Solution +{ + public int MaxDepth(TreeNode root) + { + var res = Recursion(0, root); + if (root != null) + res++; + return res; + } + + private int Recursion(int count, TreeNode node) + { + if(node == null) + return 0; + var list = new List(); + if (node.left != null) + list.Add(Recursion(count + 1, node.left)); + if (node.right != null) + list.Add(Recursion(count + 1, node.right)); + if (list.Count > 0) + return list.Max(); + return count; + } +} diff --git a/104. Maximum Depth of Binary Tree/TreeNode.cs b/104. Maximum Depth of Binary Tree/TreeNode.cs new file mode 100644 index 0000000..d177004 --- /dev/null +++ b/104. Maximum Depth of Binary Tree/TreeNode.cs @@ -0,0 +1,15 @@ +namespace _104._Maximum_Depth_of_Binary_Tree; + + +//Definition for a binary tree node. +public class TreeNode { + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val=0, TreeNode? left=null, TreeNode? right =null) { + this.val = val; + this.left = left; + this.right = right; + } +} + diff --git a/112. Path Sum/112. Path Sum.csproj b/112. Path Sum/112. Path Sum.csproj new file mode 100644 index 0000000..caab52c --- /dev/null +++ b/112. Path Sum/112. Path Sum.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _112._Path_Sum + enable + enable + + + diff --git a/112. Path Sum/Program.cs b/112. Path Sum/Program.cs new file mode 100644 index 0000000..28ab1e8 --- /dev/null +++ b/112. Path Sum/Program.cs @@ -0,0 +1,9 @@ +namespace _112._Path_Sum; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/112. Path Sum/Solution.cs b/112. Path Sum/Solution.cs new file mode 100644 index 0000000..0b5676b --- /dev/null +++ b/112. Path Sum/Solution.cs @@ -0,0 +1,21 @@ +namespace _112._Path_Sum; + +public class Solution +{ + public bool HasPathSum(TreeNode root, int targetSum) + => root != null ? Recursion(root, root.val, targetSum) : false; + + private bool Recursion(TreeNode? node, int curSum, int targetSum) + { + if (curSum == targetSum && node != null && node.left == null && node.right == null) + return true; + if (node == null) + return false; + bool res = false; + if(node.left != null) + res |= Recursion(node.left, curSum + node.left.val, targetSum); + if (node.right != null) + res |= Recursion(node.right, curSum + node.right.val, targetSum); + return res; + } +} \ No newline at end of file diff --git a/112. Path Sum/TreeNode.cs b/112. Path Sum/TreeNode.cs new file mode 100644 index 0000000..e91ee93 --- /dev/null +++ b/112. Path Sum/TreeNode.cs @@ -0,0 +1,16 @@ +namespace _112._Path_Sum; + + +//Definition for a binary tree node. +public class TreeNode +{ + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null) + { + this.val = val; + this.left = left; + this.right = right; + } +} diff --git a/136. Single Number/136. Single Number.csproj b/136. Single Number/136. Single Number.csproj new file mode 100644 index 0000000..2c699ef --- /dev/null +++ b/136. Single Number/136. Single Number.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _136._Single_Number + enable + enable + + + diff --git a/136. Single Number/Program.cs b/136. Single Number/Program.cs new file mode 100644 index 0000000..77be710 --- /dev/null +++ b/136. Single Number/Program.cs @@ -0,0 +1,9 @@ +namespace _136._Single_Number; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/136. Single Number/Solution.cs b/136. Single Number/Solution.cs new file mode 100644 index 0000000..e80be2b --- /dev/null +++ b/136. Single Number/Solution.cs @@ -0,0 +1,12 @@ +namespace _136._Single_Number; + +public class Solution +{ + public int SingleNumber(int[] nums) + { + int res = 0; + foreach (int num in nums) + res ^= num; + return res; + } +} diff --git a/190. Reverse Bits/190. Reverse Bits.csproj b/190. Reverse Bits/190. Reverse Bits.csproj new file mode 100644 index 0000000..e69ba77 --- /dev/null +++ b/190. Reverse Bits/190. Reverse Bits.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _190._Reverse_Bits + enable + enable + + + diff --git a/190. Reverse Bits/Program.cs b/190. Reverse Bits/Program.cs new file mode 100644 index 0000000..9d1f707 --- /dev/null +++ b/190. Reverse Bits/Program.cs @@ -0,0 +1,9 @@ +namespace _190._Reverse_Bits; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().reverseBits(2)); + } +} diff --git a/190. Reverse Bits/Solution.cs b/190. Reverse Bits/Solution.cs new file mode 100644 index 0000000..324e77f --- /dev/null +++ b/190. Reverse Bits/Solution.cs @@ -0,0 +1,17 @@ +namespace _190._Reverse_Bits; + +public class Solution +{ + public uint reverseBits(uint n) + { + uint res = 0; + int count = 32; + while(count-- > 0) + { + res <<= 1; + res += n & 1; + n >>= 1; + } + return res; + } +} \ No newline at end of file diff --git a/191. Number of 1 Bits/191. Number of 1 Bits.csproj b/191. Number of 1 Bits/191. Number of 1 Bits.csproj new file mode 100644 index 0000000..16b121c --- /dev/null +++ b/191. Number of 1 Bits/191. Number of 1 Bits.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _191._Number_of_1_Bits + enable + enable + + + diff --git a/191. Number of 1 Bits/Program.cs b/191. Number of 1 Bits/Program.cs new file mode 100644 index 0000000..6a54753 --- /dev/null +++ b/191. Number of 1 Bits/Program.cs @@ -0,0 +1,9 @@ +namespace _191._Number_of_1_Bits; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/191. Number of 1 Bits/Solution.cs b/191. Number of 1 Bits/Solution.cs new file mode 100644 index 0000000..7edd155 --- /dev/null +++ b/191. Number of 1 Bits/Solution.cs @@ -0,0 +1,15 @@ +namespace _191._Number_of_1_Bits; + +public class Solution +{ + public int HammingWeight(uint n) + { + int count = 0; + while (n > 0) + { + count+=(int)(n & 1); + n >>= 1; + } + return count; + } +} \ No newline at end of file diff --git a/198. House Robber/198. House Robber.csproj b/198. House Robber/198. House Robber.csproj new file mode 100644 index 0000000..625e99a --- /dev/null +++ b/198. House Robber/198. House Robber.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _198._House_Robber + enable + enable + + + diff --git a/198. House Robber/Program.cs b/198. House Robber/Program.cs new file mode 100644 index 0000000..379e453 --- /dev/null +++ b/198. House Robber/Program.cs @@ -0,0 +1,9 @@ +namespace _198._House_Robber; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/198. House Robber/Solution.cs b/198. House Robber/Solution.cs new file mode 100644 index 0000000..1de508c --- /dev/null +++ b/198. House Robber/Solution.cs @@ -0,0 +1,5 @@ +namespace _198._House_Robber; + +internal class Solution +{ +} diff --git a/226. Invert Binary Tree/226. Invert Binary Tree.csproj b/226. Invert Binary Tree/226. Invert Binary Tree.csproj new file mode 100644 index 0000000..2c7e27a --- /dev/null +++ b/226. Invert Binary Tree/226. Invert Binary Tree.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _226._Invert_Binary_Tree + enable + enable + + + diff --git a/226. Invert Binary Tree/Program.cs b/226. Invert Binary Tree/Program.cs new file mode 100644 index 0000000..782c0ac --- /dev/null +++ b/226. Invert Binary Tree/Program.cs @@ -0,0 +1,9 @@ +namespace _226._Invert_Binary_Tree; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/226. Invert Binary Tree/Solution.cs b/226. Invert Binary Tree/Solution.cs new file mode 100644 index 0000000..9518074 --- /dev/null +++ b/226. Invert Binary Tree/Solution.cs @@ -0,0 +1,23 @@ +namespace _226._Invert_Binary_Tree; + +public class Solution +{ + public TreeNode? InvertTree(TreeNode root) + { + Stack stack = new Stack(); + if (root != null) + stack.Push(root); + while (stack.Count > 0) + { + TreeNode cur = stack.Pop(); + TreeNode? buf = cur.left; + cur.left = cur.right; + cur.right = buf; + if (cur.right != null) + stack.Push(cur.right); + if (cur.left != null) + stack.Push(cur.left); + } + return root; + } +} \ No newline at end of file diff --git a/226. Invert Binary Tree/TreeNode.cs b/226. Invert Binary Tree/TreeNode.cs new file mode 100644 index 0000000..83c7dc0 --- /dev/null +++ b/226. Invert Binary Tree/TreeNode.cs @@ -0,0 +1,16 @@ +namespace _226._Invert_Binary_Tree; + + +//Definition for a binary tree node. +public class TreeNode +{ + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null) + { + this.val = val; + this.left = left; + this.right = right; + } +} diff --git a/231. Power of Two/231. Power of Two.csproj b/231. Power of Two/231. Power of Two.csproj new file mode 100644 index 0000000..20c3f78 --- /dev/null +++ b/231. Power of Two/231. Power of Two.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _231._Power_of_Two + enable + enable + + + diff --git a/231. Power of Two/Program.cs b/231. Power of Two/Program.cs new file mode 100644 index 0000000..1c72ff2 --- /dev/null +++ b/231. Power of Two/Program.cs @@ -0,0 +1,9 @@ +namespace _231._Power_of_Two; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().IsPowerOfTwo(int.Parse(Console.ReadLine()!))); + } +} diff --git a/231. Power of Two/Solution.cs b/231. Power of Two/Solution.cs new file mode 100644 index 0000000..400a885 --- /dev/null +++ b/231. Power of Two/Solution.cs @@ -0,0 +1,10 @@ +namespace _231._Power_of_Two; + +public class Solution +{ + public bool IsPowerOfTwo(int n) + { + double log = Math.Log(n, 2); + return Math.Round(log, 12) == (int)log; + } +} diff --git a/299. Bulls and Cows/299. Bulls and Cows.csproj b/299. Bulls and Cows/299. Bulls and Cows.csproj new file mode 100644 index 0000000..59ce8d9 --- /dev/null +++ b/299. Bulls and Cows/299. Bulls and Cows.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _299._Bulls_and_Cows + enable + enable + + + diff --git a/299. Bulls and Cows/Program.cs b/299. Bulls and Cows/Program.cs new file mode 100644 index 0000000..77efd0b --- /dev/null +++ b/299. Bulls and Cows/Program.cs @@ -0,0 +1,9 @@ +namespace _299._Bulls_and_Cows; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().GetHint("1123", "0111")); + } +} diff --git a/299. Bulls and Cows/Solution.cs b/299. Bulls and Cows/Solution.cs new file mode 100644 index 0000000..51061ae --- /dev/null +++ b/299. Bulls and Cows/Solution.cs @@ -0,0 +1,50 @@ +using System.Text; + +namespace _299._Bulls_and_Cows; + +public class Solution +{ + public string GetHint(string secret, string guess) + { + StringBuilder secretSb = new StringBuilder(secret); + StringBuilder guessSb = new StringBuilder(guess); + + Dictionary secretCount = new Dictionary(); + Dictionary guessCount = new Dictionary(); + int A = 0; + int B = 0; + for(int i = 0; i < secret.Length + 1; i++) + { + if (i < secret.Length && secretSb[i] == guessSb[i]) + { + secretSb[i] = '*'; + guessSb[i] = '*'; + A++; + } + if (i > 0) + { + int index = i - 1; + if (secretSb[index] == '*') + continue; + char key1 = secretSb[index]; + if (secretCount.ContainsKey(key1)) + secretCount[key1]++; + else + secretCount.Add(key1, 1); + + char key2 = guessSb[index]; + if (guessCount.ContainsKey(key2)) + guessCount[key2]++; + else + guessCount.Add(key2, 1); + } + } + foreach (var kp in secretCount) + { + if (guessCount.ContainsKey(kp.Key) == false) + continue; + B += Math.Min(kp.Value, guessCount[kp.Key]); + } + return $"{A}A{B}B"; + } +} \ No newline at end of file diff --git a/424. Longest Repeating Character Replacement/424. Longest Repeating Character Replacement.csproj b/424. Longest Repeating Character Replacement/424. Longest Repeating Character Replacement.csproj new file mode 100644 index 0000000..7dc56ef --- /dev/null +++ b/424. Longest Repeating Character Replacement/424. Longest Repeating Character Replacement.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _424._Longest_Repeating_Character_Replacement + enable + enable + + + diff --git a/424. Longest Repeating Character Replacement/Program.cs b/424. Longest Repeating Character Replacement/Program.cs new file mode 100644 index 0000000..9d94d7a --- /dev/null +++ b/424. Longest Repeating Character Replacement/Program.cs @@ -0,0 +1,9 @@ +namespace _424._Longest_Repeating_Character_Replacement; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().CharacterReplacement("ABAB", 2)); + } +} diff --git a/424. Longest Repeating Character Replacement/Solution.cs b/424. Longest Repeating Character Replacement/Solution.cs new file mode 100644 index 0000000..f936589 --- /dev/null +++ b/424. Longest Repeating Character Replacement/Solution.cs @@ -0,0 +1,56 @@ +namespace _424._Longest_Repeating_Character_Replacement; + +public class Solution +{ + public int CharacterReplacement(string s, int k) + { + List counters = new List(); + int max = 0; + for (int i = 0; i < s.Length; i++) + { + char ch = s[i]; + for (int cIndex = 0; cIndex < counters.Count; cIndex++) + { + Counter curC = counters[cIndex]; + curC.Add(ch); + if (curC.IsValid == false) + { + max = Math.Max(max, curC.ValidLength); + counters.RemoveAt(cIndex); + cIndex--; + } + } + counters.Add(new Counter(ch, k)); + } + foreach (Counter curC in counters) + max = Math.Max(max, curC.ValidLength); + return max; + } + + private class Counter + { + public int Length { get; private set; } + public bool IsValid => _maxMisses >= Length- _maxCount; + public int ValidLength => _maxCount + Math.Min(Length- _maxCount, _maxMisses); + private int[] _letters = new int[26]; + private int _maxCount => _letters[_maxLetter-65]; + private char _maxLetter; + private readonly int _maxMisses; + + public Counter(char ch, int k) + { + _maxMisses = k; + Length = 1; + _letters[ch - 65] = 1; + _maxLetter = ch; + } + + public void Add(char ch) + { + _letters[ch - 65]+=1; + if (_letters[ch - 65] > _maxCount) + _maxLetter = ch; + Length++; + } + } +} \ No newline at end of file diff --git a/438. Find All Anagrams in a String/438. Find All Anagrams in a String.csproj b/438. Find All Anagrams in a String/438. Find All Anagrams in a String.csproj new file mode 100644 index 0000000..6a26b6e --- /dev/null +++ b/438. Find All Anagrams in a String/438. Find All Anagrams in a String.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _438._Find_All_Anagrams_in_a_String + enable + enable + + + diff --git a/438. Find All Anagrams in a String/Program.cs b/438. Find All Anagrams in a String/Program.cs new file mode 100644 index 0000000..67c7f25 --- /dev/null +++ b/438. Find All Anagrams in a String/Program.cs @@ -0,0 +1,10 @@ +namespace _438._Find_All_Anagrams_in_a_String; + +internal class Program +{ + static void Main(string[] args) + { + var res = new Solution().FindAnagrams("cbaebabacd", "abc"); + Console.WriteLine(String.Join(" ", res)); + } +} diff --git a/438. Find All Anagrams in a String/Solution.cs b/438. Find All Anagrams in a String/Solution.cs new file mode 100644 index 0000000..e7306e8 --- /dev/null +++ b/438. Find All Anagrams in a String/Solution.cs @@ -0,0 +1,35 @@ +namespace _438._Find_All_Anagrams_in_a_String; + +public class Solution +{ + public IList FindAnagrams(string s, string target) + { + List result = new List(); + int[] array = new int[26]; + int[] buf = new int[26]; + foreach(var ch in target) + array[ch-97]+=1; + for(int i = 0; i <= s.Length-target.Length; i++) + { + int chIndex = s[i]-97; + if (array[chIndex] != 0) + { + Array.Copy(array, buf, array.Length); + bool win = true; + for (int j = i; j < i + target.Length; j++) + { + int chIndex2 = s[j] - 97; + buf[chIndex2] -= 1; + if (buf[chIndex2] < 0) + { + win = false; + break; + } + } + if (win) + result.Add(i); + } + } + return result; + } +} diff --git a/46. Permutations/46. Permutations.csproj b/46. Permutations/46. Permutations.csproj new file mode 100644 index 0000000..54a7526 --- /dev/null +++ b/46. Permutations/46. Permutations.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _46._Permutations + enable + enable + + + diff --git a/46. Permutations/Program.cs b/46. Permutations/Program.cs new file mode 100644 index 0000000..e92e0a1 --- /dev/null +++ b/46. Permutations/Program.cs @@ -0,0 +1,12 @@ +namespace _46._Permutations; + +internal class Program +{ + static void Main(string[] args) + { + var arr = new int[] { 1,2,3 }; + var l = new Solution().Permute(arr); + foreach (var row in l) + Console.WriteLine(String.Join(" ", row)); + } +} diff --git a/46. Permutations/Solution.cs b/46. Permutations/Solution.cs new file mode 100644 index 0000000..20b5b5b --- /dev/null +++ b/46. Permutations/Solution.cs @@ -0,0 +1,30 @@ +namespace _46._Permutations; + +public class Solution +{ + public IList> Permute(int[] nums) + { + List> l = new List>(); + Recursion(l, new List(), nums.ToHashSet()); + return l; + } + + private void Recursion(List> result, List cur, HashSet aviable) + { + if(aviable.Count == 0) + { + result.Add(cur); + return; + } + + foreach(var av in aviable) + { + var newAviable = aviable.ToHashSet(); + newAviable.Remove(av); + + var newCur = cur.ToList(); + newCur.Add(av); + Recursion(result, newCur, newAviable); + } + } +} \ No newline at end of file diff --git a/542. 01 Matrix/542. 01 Matrix.csproj b/542. 01 Matrix/542. 01 Matrix.csproj new file mode 100644 index 0000000..4839287 --- /dev/null +++ b/542. 01 Matrix/542. 01 Matrix.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _542._01_Matrix + enable + enable + + + diff --git a/542. 01 Matrix/Program.cs b/542. 01 Matrix/Program.cs new file mode 100644 index 0000000..0ede11a --- /dev/null +++ b/542. 01 Matrix/Program.cs @@ -0,0 +1,15 @@ +namespace _542._01_Matrix; + +internal class Program +{ + static void Main(string[] args) + { + int[][] mat = new int[][] { new int[] { 0,0,0, }, + new int[] { 0,1,0, }, + new int[] { 1,1,1, }, + }; + var res = new Solution().UpdateMatrix(mat); + foreach(var row in res) + Console.WriteLine(String.Join(" ", row)); + } +} diff --git a/542. 01 Matrix/Solution.cs b/542. 01 Matrix/Solution.cs new file mode 100644 index 0000000..20d384a --- /dev/null +++ b/542. 01 Matrix/Solution.cs @@ -0,0 +1,35 @@ +namespace _542._01_Matrix; + +public class Solution +{ + public int[][] UpdateMatrix(int[][] mat) + { + bool hasChanges = true; + while(hasChanges) + { + hasChanges = false; + for (int y = 0; y < mat.Length; y++) + { + for (int x = 0; x < mat[y].Length; x++) + { + if (mat[y][x] == 0) + continue; + int min = int.MaxValue; + for (int i = 1; i < 9; i += 2) + { + int newY = i / 3 + y - 1; + int newX = i % 3 + x - 1; + if (newY >= 0 && newY < mat.Length && newX >= 0 && newX < mat[newY].Length) + { + min = Math.Min(min, mat[newY][newX]); + } + } + if(mat[y][x] != min + 1) + hasChanges = true; + mat[y][x] = min + 1; + } + } + } + return mat; + } +} \ No newline at end of file diff --git a/62. Unique Paths/62. Unique Paths.csproj b/62. Unique Paths/62. Unique Paths.csproj new file mode 100644 index 0000000..577b2c1 --- /dev/null +++ b/62. Unique Paths/62. Unique Paths.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _62._Unique_Paths + enable + enable + + + diff --git a/62. Unique Paths/Program.cs b/62. Unique Paths/Program.cs new file mode 100644 index 0000000..8545cce --- /dev/null +++ b/62. Unique Paths/Program.cs @@ -0,0 +1,9 @@ +namespace _62._Unique_Paths; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().UniquePaths(10,10)); + } +} diff --git a/62. Unique Paths/Solution.cs b/62. Unique Paths/Solution.cs new file mode 100644 index 0000000..eb93082 --- /dev/null +++ b/62. Unique Paths/Solution.cs @@ -0,0 +1,29 @@ +using System.Numerics; + +namespace _62._Unique_Paths; + +public class Solution +{ + public int UniquePaths(int min, int max) + { + min--; + max--; + if (min == 0 || max == 0) + return 1; + if(min > max) + { + min = min + max; + max = min - max; + min = min - max; + } + return (int)(Fact(max + 1, min + max) / Fact(1, min)); + } + + private BigInteger Fact(int start, int end) + { + BigInteger res = start; + while (++start <= end) + res *= start; + return res; + } +} diff --git a/700. Search in a Binary Search Tree/700. Search in a Binary Search Tree.csproj b/700. Search in a Binary Search Tree/700. Search in a Binary Search Tree.csproj new file mode 100644 index 0000000..415ae08 --- /dev/null +++ b/700. Search in a Binary Search Tree/700. Search in a Binary Search Tree.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _700._Search_in_a_Binary_Search_Tree + enable + enable + + + diff --git a/700. Search in a Binary Search Tree/Program.cs b/700. Search in a Binary Search Tree/Program.cs new file mode 100644 index 0000000..7aa74c2 --- /dev/null +++ b/700. Search in a Binary Search Tree/Program.cs @@ -0,0 +1,9 @@ +namespace _700._Search_in_a_Binary_Search_Tree; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/700. Search in a Binary Search Tree/Solution.cs b/700. Search in a Binary Search Tree/Solution.cs new file mode 100644 index 0000000..6dc1b7b --- /dev/null +++ b/700. Search in a Binary Search Tree/Solution.cs @@ -0,0 +1,22 @@ +namespace _700._Search_in_a_Binary_Search_Tree; + +public class Solution +{ + public TreeNode? SearchBST(TreeNode? root, int val) + { + Stack stack = new Stack(); + if (root != null) + stack.Push(root); + while (stack.Count > 0) + { + TreeNode cur = stack.Pop(); + if (cur.right != null) + stack.Push(cur.right); + if (cur.left != null) + stack.Push(cur.left); + if(cur.val == val) + return cur; + } + return null; + } +} \ No newline at end of file diff --git a/700. Search in a Binary Search Tree/TreeNode.cs b/700. Search in a Binary Search Tree/TreeNode.cs new file mode 100644 index 0000000..c53dd1f --- /dev/null +++ b/700. Search in a Binary Search Tree/TreeNode.cs @@ -0,0 +1,15 @@ +namespace _700._Search_in_a_Binary_Search_Tree; + + +//Definition for a binary tree node. +public class TreeNode { + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val=0, TreeNode? left=null, TreeNode? right =null) { + this.val = val; + this.left = left; + this.right = right; + } +} + diff --git a/701. Insert into a Binary Search Tree/701. Insert into a Binary Search Tree.csproj b/701. Insert into a Binary Search Tree/701. Insert into a Binary Search Tree.csproj new file mode 100644 index 0000000..28196fc --- /dev/null +++ b/701. Insert into a Binary Search Tree/701. Insert into a Binary Search Tree.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _701._Insert_into_a_Binary_Search_Tree + enable + enable + + + diff --git a/701. Insert into a Binary Search Tree/Program.cs b/701. Insert into a Binary Search Tree/Program.cs new file mode 100644 index 0000000..1484eb7 --- /dev/null +++ b/701. Insert into a Binary Search Tree/Program.cs @@ -0,0 +1,9 @@ +namespace _701._Insert_into_a_Binary_Search_Tree; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/701. Insert into a Binary Search Tree/Solution.cs b/701. Insert into a Binary Search Tree/Solution.cs new file mode 100644 index 0000000..7b0f070 --- /dev/null +++ b/701. Insert into a Binary Search Tree/Solution.cs @@ -0,0 +1,34 @@ +namespace _701._Insert_into_a_Binary_Search_Tree; + +public class Solution +{ + public TreeNode InsertIntoBST(TreeNode root, int val) + { + if(root == null) + return new TreeNode(val); + TreeNode cur = root; + while(true) + { + int curVal = cur.val; + if(val < curVal) + { + if (cur.left == null) + { + cur.left = new TreeNode(val); + break; + } + cur = cur.left; + } + else + { + if (cur.right == null) + { + cur.right = new TreeNode(val); + break; + } + cur = cur.right; + } + } + return root; + } +} \ No newline at end of file diff --git a/701. Insert into a Binary Search Tree/TreeNode.cs b/701. Insert into a Binary Search Tree/TreeNode.cs new file mode 100644 index 0000000..02553c1 --- /dev/null +++ b/701. Insert into a Binary Search Tree/TreeNode.cs @@ -0,0 +1,15 @@ +namespace _701._Insert_into_a_Binary_Search_Tree; + + +//Definition for a binary tree node. +public class TreeNode { + public int val; + public TreeNode? left; + public TreeNode? right; + public TreeNode(int val=0, TreeNode? left=null, TreeNode? right =null) { + this.val = val; + this.left = left; + this.right = right; + } +} + diff --git a/746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.csproj b/746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.csproj new file mode 100644 index 0000000..f9c5030 --- /dev/null +++ b/746. Min Cost Climbing Stairs/746. Min Cost Climbing Stairs.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _746._Min_Cost_Climbing_Stairs + enable + enable + + + diff --git a/746. Min Cost Climbing Stairs/Program.cs b/746. Min Cost Climbing Stairs/Program.cs new file mode 100644 index 0000000..5a67524 --- /dev/null +++ b/746. Min Cost Climbing Stairs/Program.cs @@ -0,0 +1,9 @@ +namespace _746._Min_Cost_Climbing_Stairs; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().MinCostClimbingStairs(new int[] { 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 })); + } +} diff --git a/746. Min Cost Climbing Stairs/Solution.cs b/746. Min Cost Climbing Stairs/Solution.cs new file mode 100644 index 0000000..2e50fea --- /dev/null +++ b/746. Min Cost Climbing Stairs/Solution.cs @@ -0,0 +1,17 @@ +namespace _746._Min_Cost_Climbing_Stairs; + +public class Solution +{ + public int MinCostClimbingStairs(int[] cost) + { + int prev = cost[0]; + int cur = cost[1]; + for(int i = 2; i < cost.Length; i++) + { + int next = cost[i] + Math.Min(prev, cur); + prev = cur; + cur = next; + } + return Math.Min(prev, cur); + } +} diff --git a/77. Combinations/77. Combinations.csproj b/77. Combinations/77. Combinations.csproj new file mode 100644 index 0000000..be69202 --- /dev/null +++ b/77. Combinations/77. Combinations.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _77._Combinations + enable + enable + + + diff --git a/77. Combinations/Program.cs b/77. Combinations/Program.cs new file mode 100644 index 0000000..d0268de --- /dev/null +++ b/77. Combinations/Program.cs @@ -0,0 +1,11 @@ +namespace _77._Combinations; + +internal class Program +{ + static void Main(string[] args) + { + var res = new Solution().Combine(4, 2); + foreach(var row in res) + Console.WriteLine(string.Join(" ", row)); + } +} diff --git a/77. Combinations/Solution.cs b/77. Combinations/Solution.cs new file mode 100644 index 0000000..fe9db20 --- /dev/null +++ b/77. Combinations/Solution.cs @@ -0,0 +1,26 @@ +namespace _77._Combinations; + +public class Solution +{ + public IList> Combine(int n, int k) + { + List> res = new List>(); + Recursion(res, new List(), 1, n, k); + return res; + } + + private void Recursion(List> result, List cur, int minNum, int maxNum, int count) + { + if (cur.Count == count) + { + result.Add(cur.ToList()); + return; + } + for (int toAdd = minNum; toAdd <= maxNum; toAdd++) + { + cur.Add(toAdd); + Recursion(result, cur, toAdd + 1, maxNum, count); + cur.RemoveAt(cur.Count-1); + } + } +} \ No newline at end of file diff --git a/784. Letter Case Permutation/784. Letter Case Permutation.csproj b/784. Letter Case Permutation/784. Letter Case Permutation.csproj new file mode 100644 index 0000000..6dfe4a5 --- /dev/null +++ b/784. Letter Case Permutation/784. Letter Case Permutation.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _784._Letter_Case_Permutation + enable + enable + + + diff --git a/784. Letter Case Permutation/Program.cs b/784. Letter Case Permutation/Program.cs new file mode 100644 index 0000000..ec55a04 --- /dev/null +++ b/784. Letter Case Permutation/Program.cs @@ -0,0 +1,9 @@ +namespace _784._Letter_Case_Permutation; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(String.Join(" ", new Solution().LetterCasePermutation("hello"))); + } +} diff --git a/784. Letter Case Permutation/Solution.cs b/784. Letter Case Permutation/Solution.cs new file mode 100644 index 0000000..987a74e --- /dev/null +++ b/784. Letter Case Permutation/Solution.cs @@ -0,0 +1,28 @@ +using System.Text; + +namespace _784._Letter_Case_Permutation; + +public class Solution +{ + public IList LetterCasePermutation(string s) + { + HashSet set = new HashSet(); + StringBuilder sb = new StringBuilder(s); + for (int i = 0; i < Math.Pow(2, s.Length); i++) + { + UppercasePerMask(sb, i); + set.Add(sb.ToString()); + } + return set.ToList(); + } + + private void UppercasePerMask(StringBuilder sb, int mask) + { + for (int i = sb.Length - 1; i >= 0; i--) + { + if (char.IsLetter(sb[i])) + sb[i] = mask % 2 == 1 ? char.ToUpper(sb[i]) : char.ToLower(sb[i]); + mask >>= 1; + } + } +} \ No newline at end of file diff --git a/994. Rotting Oranges/994. Rotting Oranges.csproj b/994. Rotting Oranges/994. Rotting Oranges.csproj new file mode 100644 index 0000000..69772c3 --- /dev/null +++ b/994. Rotting Oranges/994. Rotting Oranges.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _994._Rotting_Oranges + enable + enable + + + diff --git a/994. Rotting Oranges/Program.cs b/994. Rotting Oranges/Program.cs new file mode 100644 index 0000000..728c00c --- /dev/null +++ b/994. Rotting Oranges/Program.cs @@ -0,0 +1,9 @@ +namespace _994._Rotting_Oranges; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/994. Rotting Oranges/Solution.cs b/994. Rotting Oranges/Solution.cs new file mode 100644 index 0000000..0a9b0e2 --- /dev/null +++ b/994. Rotting Oranges/Solution.cs @@ -0,0 +1,60 @@ +namespace _994._Rotting_Oranges; + +public class Solution +{ + public int OrangesRotting(int[][] mat) + { + int w = mat[0].Length; + int h = mat.Length; + + Work(mat, i => + { + int y = h - i / w - 1; + int x = i % w; + return (y, x); + }); + Work(mat, i => + { + int y = i / w; + int x = w - i % w - 1; + return (y, x); + }); + Work(mat, i => + { + int y = i / w; + int x = i % w; + return (y, x); + }); + int max = Work(mat, i => + { + int y = h - i / w - 1; + int x = w - i % w - 1; + return (y, x); + }); + return max; + } + + private int Work(int[][] mat, Func getXY) + { + int absMax = 0; + for (int i = 0; i < mat.Length * mat[0].Length; i++) + { + var dot = getXY(i); + int x = dot.x; + int y = dot.y; + if (mat[y][x] == 0) + continue; + int max = int.MinValue; + for (int shift = 1; shift < 9; shift += 2) + { + int newY = shift / 3 + y - 1; + int newX = shift % 3 + x - 1; + if (newY >= 0 && newY < mat.Length && newX >= 0 && newX < mat[newY].Length) + max = Math.Max(max, mat[newY][newX]); + } + mat[y][x] = max + 1; + absMax = Math.Max(absMax, mat[y][x]); + } + return absMax; + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 753fc8a..5d2d2b1 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -97,19 +97,61 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "235. Lowest Common Ancestor EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "200. Number of Islands", "200. Number of Islands\200. Number of Islands.csproj", "{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "617. Merge Two Binary Trees", "617. Merge Two Binary Trees\617. Merge Two Binary Trees.csproj", "{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "617. Merge Two Binary Trees", "617. Merge Two Binary Trees\617. Merge Two Binary Trees.csproj", "{79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "116. Populating Next Right Pointers in Each Node", "116. Populating Next Right Pointers in Each Node\116. Populating Next Right Pointers in Each Node.csproj", "{DD19137E-1A2F-4803-910B-A63826F65159}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "116. Populating Next Right Pointers in Each Node", "116. Populating Next Right Pointers in Each Node\116. Populating Next Right Pointers in Each Node.csproj", "{DD19137E-1A2F-4803-910B-A63826F65159}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "144. Binary Tree Preorder Traversal", "144. Binary Tree Preorder Traversal\144. Binary Tree Preorder Traversal.csproj", "{0DA14E84-9F56-4384-9743-BA7FD7539113}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "144. Binary Tree Preorder Traversal", "144. Binary Tree Preorder Traversal\144. Binary Tree Preorder Traversal.csproj", "{0DA14E84-9F56-4384-9743-BA7FD7539113}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "94. Binary Tree Inorder Traversal", "94. Binary Tree Inorder Traversal\94. Binary Tree Inorder Traversal.csproj", "{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "94. Binary Tree Inorder Traversal", "94. Binary Tree Inorder Traversal\94. Binary Tree Inorder Traversal.csproj", "{60E8258E-4BBC-42C7-96CD-C67DB629E0E1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "145. Binary Tree Postorder Traversal", "145. Binary Tree Postorder Traversal\145. Binary Tree Postorder Traversal.csproj", "{6E954C2F-608C-4294-91CF-62B8E49CC415}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "145. Binary Tree Postorder Traversal", "145. Binary Tree Postorder Traversal\145. Binary Tree Postorder Traversal.csproj", "{6E954C2F-608C-4294-91CF-62B8E49CC415}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "509. Fibonacci Number", "509. Fibonacci Number\509. Fibonacci Number.csproj", "{73450FFA-253B-42CC-B5E7-8769CFF1BE96}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "509. Fibonacci Number", "509. Fibonacci Number\509. Fibonacci Number.csproj", "{73450FFA-253B-42CC-B5E7-8769CFF1BE96}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "70. Climbing Stairs", "70. Climbing Stairs\70. Climbing Stairs.csproj", "{625340C0-FC67-4555-B49F-FB28E3177F72}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "70. Climbing Stairs", "70. Climbing Stairs\70. Climbing Stairs.csproj", "{625340C0-FC67-4555-B49F-FB28E3177F72}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "746. Min Cost Climbing Stairs", "746. Min Cost Climbing Stairs\746. Min Cost Climbing Stairs.csproj", "{624EA332-8762-4EE9-AD73-617AEFD359DF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "62. Unique Paths", "62. Unique Paths\62. Unique Paths.csproj", "{00701676-F5C4-4368-B09F-2CFB65E899D9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "104. Maximum Depth of Binary Tree", "104. Maximum Depth of Binary Tree\104. Maximum Depth of Binary Tree.csproj", "{35BDD2F1-D1A7-45BD-A335-1315E3EC45E8}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "438. Find All Anagrams in a String", "438. Find All Anagrams in a String\438. Find All Anagrams in a String.csproj", "{E7DE4A8E-1D9A-4BF6-8E64-FB160233FA36}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "424. Longest Repeating Character Replacement", "424. Longest Repeating Character Replacement\424. Longest Repeating Character Replacement.csproj", "{CBCDA63E-A784-44B6-89E8-EEB650D04F14}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "226. Invert Binary Tree", "226. Invert Binary Tree\226. Invert Binary Tree.csproj", "{5ACA65D5-C137-4F12-B371-38BC72C826D0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "112. Path Sum", "112. Path Sum\112. Path Sum.csproj", "{82B65579-1A16-4BE6-965B-40C70C6116F0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "101. Symmetric Tree", "101. Symmetric Tree\101. Symmetric Tree.csproj", "{5F3B8ABB-CADF-49D4-87E6-84D9853F18C9}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "542. 01 Matrix", "542. 01 Matrix\542. 01 Matrix.csproj", "{A8925C2A-E733-43BD-A2B0-5F7E6E447573}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "299. Bulls and Cows", "299. Bulls and Cows\299. Bulls and Cows.csproj", "{A06F4EAA-D75E-45E3-86CD-D300A23950EE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "700. Search in a Binary Search Tree", "700. Search in a Binary Search Tree\700. Search in a Binary Search Tree.csproj", "{DE3D5D2E-81A3-498F-8A26-24893357989A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "46. Permutations", "46. Permutations\46. Permutations.csproj", "{2AAC90C3-3492-432A-A90B-C69558B87D3A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "77. Combinations", "77. Combinations\77. Combinations.csproj", "{138291FD-7147-4D1A-82E1-11B11432B186}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "784. Letter Case Permutation", "784. Letter Case Permutation\784. Letter Case Permutation.csproj", "{F79E4D6B-0186-4AAE-81DD-7AE3141761B3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "701. Insert into a Binary Search Tree", "701. Insert into a Binary Search Tree\701. Insert into a Binary Search Tree.csproj", "{346526B1-7E14-40AF-856D-E1960B82F21C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "994. Rotting Oranges", "994. Rotting Oranges\994. Rotting Oranges.csproj", "{CF353DBF-11A5-424C-AEF2-3D3109EFB357}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "231. Power of Two", "231. Power of Two\231. Power of Two.csproj", "{460D068B-1EB2-4A5E-8553-5ADB6BDEED75}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "198. House Robber", "198. House Robber\198. House Robber.csproj", "{2485479A-78CE-4912-8ADA-F11676C9CE2C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "190. Reverse Bits", "190. Reverse Bits\190. Reverse Bits.csproj", "{12AE150A-0296-4057-9BBF-F9AEC37838E9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "136. Single Number", "136. Single Number\136. Single Number.csproj", "{D63C897B-C5EB-4C18-9091-A309E84EB471}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "191. Number of 1 Bits", "191. Number of 1 Bits\191. Number of 1 Bits.csproj", "{6950BC7E-2DB5-4DBF-9C2C-B331F9ED99DB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -333,6 +375,90 @@ Global {625340C0-FC67-4555-B49F-FB28E3177F72}.Debug|Any CPU.Build.0 = Debug|Any CPU {625340C0-FC67-4555-B49F-FB28E3177F72}.Release|Any CPU.ActiveCfg = Release|Any CPU {625340C0-FC67-4555-B49F-FB28E3177F72}.Release|Any CPU.Build.0 = Release|Any CPU + {624EA332-8762-4EE9-AD73-617AEFD359DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {624EA332-8762-4EE9-AD73-617AEFD359DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {624EA332-8762-4EE9-AD73-617AEFD359DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {624EA332-8762-4EE9-AD73-617AEFD359DF}.Release|Any CPU.Build.0 = Release|Any CPU + {00701676-F5C4-4368-B09F-2CFB65E899D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {00701676-F5C4-4368-B09F-2CFB65E899D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {00701676-F5C4-4368-B09F-2CFB65E899D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {00701676-F5C4-4368-B09F-2CFB65E899D9}.Release|Any CPU.Build.0 = Release|Any CPU + {35BDD2F1-D1A7-45BD-A335-1315E3EC45E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35BDD2F1-D1A7-45BD-A335-1315E3EC45E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35BDD2F1-D1A7-45BD-A335-1315E3EC45E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35BDD2F1-D1A7-45BD-A335-1315E3EC45E8}.Release|Any CPU.Build.0 = Release|Any CPU + {E7DE4A8E-1D9A-4BF6-8E64-FB160233FA36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7DE4A8E-1D9A-4BF6-8E64-FB160233FA36}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7DE4A8E-1D9A-4BF6-8E64-FB160233FA36}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7DE4A8E-1D9A-4BF6-8E64-FB160233FA36}.Release|Any CPU.Build.0 = Release|Any CPU + {CBCDA63E-A784-44B6-89E8-EEB650D04F14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CBCDA63E-A784-44B6-89E8-EEB650D04F14}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CBCDA63E-A784-44B6-89E8-EEB650D04F14}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CBCDA63E-A784-44B6-89E8-EEB650D04F14}.Release|Any CPU.Build.0 = Release|Any CPU + {5ACA65D5-C137-4F12-B371-38BC72C826D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5ACA65D5-C137-4F12-B371-38BC72C826D0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5ACA65D5-C137-4F12-B371-38BC72C826D0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5ACA65D5-C137-4F12-B371-38BC72C826D0}.Release|Any CPU.Build.0 = Release|Any CPU + {82B65579-1A16-4BE6-965B-40C70C6116F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82B65579-1A16-4BE6-965B-40C70C6116F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82B65579-1A16-4BE6-965B-40C70C6116F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82B65579-1A16-4BE6-965B-40C70C6116F0}.Release|Any CPU.Build.0 = Release|Any CPU + {5F3B8ABB-CADF-49D4-87E6-84D9853F18C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5F3B8ABB-CADF-49D4-87E6-84D9853F18C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5F3B8ABB-CADF-49D4-87E6-84D9853F18C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5F3B8ABB-CADF-49D4-87E6-84D9853F18C9}.Release|Any CPU.Build.0 = Release|Any CPU + {A8925C2A-E733-43BD-A2B0-5F7E6E447573}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A8925C2A-E733-43BD-A2B0-5F7E6E447573}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A8925C2A-E733-43BD-A2B0-5F7E6E447573}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A8925C2A-E733-43BD-A2B0-5F7E6E447573}.Release|Any CPU.Build.0 = Release|Any CPU + {A06F4EAA-D75E-45E3-86CD-D300A23950EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A06F4EAA-D75E-45E3-86CD-D300A23950EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A06F4EAA-D75E-45E3-86CD-D300A23950EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A06F4EAA-D75E-45E3-86CD-D300A23950EE}.Release|Any CPU.Build.0 = Release|Any CPU + {DE3D5D2E-81A3-498F-8A26-24893357989A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DE3D5D2E-81A3-498F-8A26-24893357989A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DE3D5D2E-81A3-498F-8A26-24893357989A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DE3D5D2E-81A3-498F-8A26-24893357989A}.Release|Any CPU.Build.0 = Release|Any CPU + {2AAC90C3-3492-432A-A90B-C69558B87D3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2AAC90C3-3492-432A-A90B-C69558B87D3A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2AAC90C3-3492-432A-A90B-C69558B87D3A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2AAC90C3-3492-432A-A90B-C69558B87D3A}.Release|Any CPU.Build.0 = Release|Any CPU + {138291FD-7147-4D1A-82E1-11B11432B186}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {138291FD-7147-4D1A-82E1-11B11432B186}.Debug|Any CPU.Build.0 = Debug|Any CPU + {138291FD-7147-4D1A-82E1-11B11432B186}.Release|Any CPU.ActiveCfg = Release|Any CPU + {138291FD-7147-4D1A-82E1-11B11432B186}.Release|Any CPU.Build.0 = Release|Any CPU + {F79E4D6B-0186-4AAE-81DD-7AE3141761B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F79E4D6B-0186-4AAE-81DD-7AE3141761B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F79E4D6B-0186-4AAE-81DD-7AE3141761B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F79E4D6B-0186-4AAE-81DD-7AE3141761B3}.Release|Any CPU.Build.0 = Release|Any CPU + {346526B1-7E14-40AF-856D-E1960B82F21C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {346526B1-7E14-40AF-856D-E1960B82F21C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {346526B1-7E14-40AF-856D-E1960B82F21C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {346526B1-7E14-40AF-856D-E1960B82F21C}.Release|Any CPU.Build.0 = Release|Any CPU + {CF353DBF-11A5-424C-AEF2-3D3109EFB357}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CF353DBF-11A5-424C-AEF2-3D3109EFB357}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CF353DBF-11A5-424C-AEF2-3D3109EFB357}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CF353DBF-11A5-424C-AEF2-3D3109EFB357}.Release|Any CPU.Build.0 = Release|Any CPU + {460D068B-1EB2-4A5E-8553-5ADB6BDEED75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {460D068B-1EB2-4A5E-8553-5ADB6BDEED75}.Debug|Any CPU.Build.0 = Debug|Any CPU + {460D068B-1EB2-4A5E-8553-5ADB6BDEED75}.Release|Any CPU.ActiveCfg = Release|Any CPU + {460D068B-1EB2-4A5E-8553-5ADB6BDEED75}.Release|Any CPU.Build.0 = Release|Any CPU + {2485479A-78CE-4912-8ADA-F11676C9CE2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2485479A-78CE-4912-8ADA-F11676C9CE2C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2485479A-78CE-4912-8ADA-F11676C9CE2C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2485479A-78CE-4912-8ADA-F11676C9CE2C}.Release|Any CPU.Build.0 = Release|Any CPU + {12AE150A-0296-4057-9BBF-F9AEC37838E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12AE150A-0296-4057-9BBF-F9AEC37838E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12AE150A-0296-4057-9BBF-F9AEC37838E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12AE150A-0296-4057-9BBF-F9AEC37838E9}.Release|Any CPU.Build.0 = Release|Any CPU + {D63C897B-C5EB-4C18-9091-A309E84EB471}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D63C897B-C5EB-4C18-9091-A309E84EB471}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D63C897B-C5EB-4C18-9091-A309E84EB471}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D63C897B-C5EB-4C18-9091-A309E84EB471}.Release|Any CPU.Build.0 = Release|Any CPU + {6950BC7E-2DB5-4DBF-9C2C-B331F9ED99DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6950BC7E-2DB5-4DBF-9C2C-B331F9ED99DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6950BC7E-2DB5-4DBF-9C2C-B331F9ED99DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6950BC7E-2DB5-4DBF-9C2C-B331F9ED99DB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE