From 01c5720116c263022aefe2926108e65d436d68d3 Mon Sep 17 00:00:00 2001 From: Electrominch <64524860+Electrominch@users.noreply.github.com> Date: Wed, 12 Oct 2022 02:30:23 +0300 Subject: [PATCH] Day10 --- ...ng Next Right Pointers in Each Node.csproj | 11 ++++ .../Node.cs | 25 ++++++++ .../Program.cs | 9 +++ .../Solution.cs | 31 ++++++++++ ...144. Binary Tree Preorder Traversal.csproj | 11 ++++ .../Program.cs | 9 +++ .../Solution.cs | 22 +++++++ .../TreeNode.cs | 16 +++++ ...45. Binary Tree Postorder Traversal.csproj | 11 ++++ .../Program.cs | 9 +++ .../Solution.cs | 24 ++++++++ .../TreeNode.cs | 16 +++++ 20. Valid Parentheses/Solution.cs | 6 +- 200. Number of Islands/Solution.cs | 6 +- 232. Implement Queue using Stacks/MyQueue.cs | 4 +- .../Program.cs | 2 +- .../Solution.cs | 4 +- .../TreeNode.cs | 3 +- .../Solution.cs | 4 +- .../509. Fibonacci Number.csproj | 11 ++++ 509. Fibonacci Number/Program.cs | 8 +++ 509. Fibonacci Number/Solution.cs | 20 +++++++ 567. Permutation in String/Solution.cs | 10 ++-- .../617. Merge Two Binary Trees.csproj | 11 ++++ 617. Merge Two Binary Trees/Program.cs | 58 +++++++++++++++++++ 617. Merge Two Binary Trees/Solution.cs | 31 ++++++++++ 617. Merge Two Binary Trees/TreeNode.cs | 16 +++++ 695. Max Area of Island/Program.cs | 2 +- .../70. Climbing Stairs.csproj | 11 ++++ 70. Climbing Stairs/Program.cs | 9 +++ 70. Climbing Stairs/Solution.cs | 19 ++++++ 733. Flood Fill/Program.cs | 4 +- .../Solution.cs | 2 +- .../94. Binary Tree Inorder Traversal.csproj | 11 ++++ 94. Binary Tree Inorder Traversal/Program.cs | 9 +++ 94. Binary Tree Inorder Traversal/Solution.cs | 23 ++++++++ 94. Binary Tree Inorder Traversal/TreeNode.cs | 15 +++++ 98. Validate Binary Search Tree/Solution.cs | 10 ++-- Leetcode.sln | 44 +++++++++++++- 39 files changed, 517 insertions(+), 30 deletions(-) create mode 100644 116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.csproj create mode 100644 116. Populating Next Right Pointers in Each Node/Node.cs create mode 100644 116. Populating Next Right Pointers in Each Node/Program.cs create mode 100644 116. Populating Next Right Pointers in Each Node/Solution.cs create mode 100644 144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.csproj create mode 100644 144. Binary Tree Preorder Traversal/Program.cs create mode 100644 144. Binary Tree Preorder Traversal/Solution.cs create mode 100644 144. Binary Tree Preorder Traversal/TreeNode.cs create mode 100644 145. Binary Tree Postorder Traversal/145. Binary Tree Postorder Traversal.csproj create mode 100644 145. Binary Tree Postorder Traversal/Program.cs create mode 100644 145. Binary Tree Postorder Traversal/Solution.cs create mode 100644 145. Binary Tree Postorder Traversal/TreeNode.cs create mode 100644 509. Fibonacci Number/509. Fibonacci Number.csproj create mode 100644 509. Fibonacci Number/Program.cs create mode 100644 509. Fibonacci Number/Solution.cs create mode 100644 617. Merge Two Binary Trees/617. Merge Two Binary Trees.csproj create mode 100644 617. Merge Two Binary Trees/Program.cs create mode 100644 617. Merge Two Binary Trees/Solution.cs create mode 100644 617. Merge Two Binary Trees/TreeNode.cs create mode 100644 70. Climbing Stairs/70. Climbing Stairs.csproj create mode 100644 70. Climbing Stairs/Program.cs create mode 100644 70. Climbing Stairs/Solution.cs create mode 100644 94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.csproj create mode 100644 94. Binary Tree Inorder Traversal/Program.cs create mode 100644 94. Binary Tree Inorder Traversal/Solution.cs create mode 100644 94. Binary Tree Inorder Traversal/TreeNode.cs diff --git a/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.csproj b/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.csproj new file mode 100644 index 0000000..d8d60a0 --- /dev/null +++ b/116. Populating Next Right Pointers in Each Node/116. Populating Next Right Pointers in Each Node.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _116._Populating_Next_Right_Pointers_in_Each_Node + enable + enable + + + diff --git a/116. Populating Next Right Pointers in Each Node/Node.cs b/116. Populating Next Right Pointers in Each Node/Node.cs new file mode 100644 index 0000000..12ccf16 --- /dev/null +++ b/116. Populating Next Right Pointers in Each Node/Node.cs @@ -0,0 +1,25 @@ +namespace _116._Populating_Next_Right_Pointers_in_Each_Node; + +// Definition for a Node. +public class Node +{ + public int val; + public Node? left; + public Node? right; + public Node? next; + + public Node() { } + + public Node(int _val) + { + val = _val; + } + + public Node(int _val, Node? _left, Node? _right, Node? _next) + { + val = _val; + left = _left; + right = _right; + next = _next; + } +} diff --git a/116. Populating Next Right Pointers in Each Node/Program.cs b/116. Populating Next Right Pointers in Each Node/Program.cs new file mode 100644 index 0000000..0cf5fc5 --- /dev/null +++ b/116. Populating Next Right Pointers in Each Node/Program.cs @@ -0,0 +1,9 @@ +namespace _116._Populating_Next_Right_Pointers_in_Each_Node; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/116. Populating Next Right Pointers in Each Node/Solution.cs b/116. Populating Next Right Pointers in Each Node/Solution.cs new file mode 100644 index 0000000..562c0fa --- /dev/null +++ b/116. Populating Next Right Pointers in Each Node/Solution.cs @@ -0,0 +1,31 @@ +namespace _116._Populating_Next_Right_Pointers_in_Each_Node; + +public class Solution +{ + public Node? Connect(Node? root) + { + if (root == null) + return null; + List curLevel = new List() { root }; + List nextLevel = new List(); + while (curLevel.Count > 0) + { + for (int i = 0; i < curLevel.Count - 1; i++) + curLevel[i].next = curLevel[i + 1]; + nextLevel.Clear(); + foreach (var n in curLevel) + { + if (n == null) + continue; + if (n.left != null) + nextLevel.Add(n.left); + if (n.right != null) + nextLevel.Add(n.right); + } + List buf = curLevel; + curLevel = nextLevel; + nextLevel = buf; + } + return root; + } +} \ No newline at end of file diff --git a/144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.csproj b/144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.csproj new file mode 100644 index 0000000..b5f7eef --- /dev/null +++ b/144. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _144._Binary_Tree_Preorder_Traversal + enable + enable + + + diff --git a/144. Binary Tree Preorder Traversal/Program.cs b/144. Binary Tree Preorder Traversal/Program.cs new file mode 100644 index 0000000..437d800 --- /dev/null +++ b/144. Binary Tree Preorder Traversal/Program.cs @@ -0,0 +1,9 @@ +namespace _144._Binary_Tree_Preorder_Traversal; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/144. Binary Tree Preorder Traversal/Solution.cs b/144. Binary Tree Preorder Traversal/Solution.cs new file mode 100644 index 0000000..d6e0133 --- /dev/null +++ b/144. Binary Tree Preorder Traversal/Solution.cs @@ -0,0 +1,22 @@ +namespace _144._Binary_Tree_Preorder_Traversal; + +public class Solution +{ + public IList PreorderTraversal(TreeNode root) + { + List res = new List(); + 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); + res.Add(cur.val); + } + return res; + } +} diff --git a/144. Binary Tree Preorder Traversal/TreeNode.cs b/144. Binary Tree Preorder Traversal/TreeNode.cs new file mode 100644 index 0000000..5da3e3b --- /dev/null +++ b/144. Binary Tree Preorder Traversal/TreeNode.cs @@ -0,0 +1,16 @@ +namespace _144._Binary_Tree_Preorder_Traversal; + + +//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/145. Binary Tree Postorder Traversal/145. Binary Tree Postorder Traversal.csproj b/145. Binary Tree Postorder Traversal/145. Binary Tree Postorder Traversal.csproj new file mode 100644 index 0000000..149c7d2 --- /dev/null +++ b/145. Binary Tree Postorder Traversal/145. Binary Tree Postorder Traversal.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _145._Binary_Tree_Postorder_Traversal + enable + enable + + + diff --git a/145. Binary Tree Postorder Traversal/Program.cs b/145. Binary Tree Postorder Traversal/Program.cs new file mode 100644 index 0000000..5d2d1c5 --- /dev/null +++ b/145. Binary Tree Postorder Traversal/Program.cs @@ -0,0 +1,9 @@ +namespace _145._Binary_Tree_Postorder_Traversal; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/145. Binary Tree Postorder Traversal/Solution.cs b/145. Binary Tree Postorder Traversal/Solution.cs new file mode 100644 index 0000000..044d49e --- /dev/null +++ b/145. Binary Tree Postorder Traversal/Solution.cs @@ -0,0 +1,24 @@ +namespace _145._Binary_Tree_Postorder_Traversal; + +public class Solution +{ + public IList PostorderTraversal(TreeNode root) + { + var result = new List(); + if (root == null) return result; + var stack = new Stack(); + stack.Push(root); + while (stack.Any()) + { + var cur = stack.Pop(); + result.Add(cur.val); + if (cur.left != null) + stack.Push(cur.left); + if (cur.right != null) + stack.Push(cur.right); + } + result.Reverse(); + + return result; + } +} \ No newline at end of file diff --git a/145. Binary Tree Postorder Traversal/TreeNode.cs b/145. Binary Tree Postorder Traversal/TreeNode.cs new file mode 100644 index 0000000..24e4169 --- /dev/null +++ b/145. Binary Tree Postorder Traversal/TreeNode.cs @@ -0,0 +1,16 @@ +namespace _145._Binary_Tree_Postorder_Traversal; + + +//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/20. Valid Parentheses/Solution.cs b/20. Valid Parentheses/Solution.cs index 53a1b21..a6c45d3 100644 --- a/20. Valid Parentheses/Solution.cs +++ b/20. Valid Parentheses/Solution.cs @@ -11,7 +11,7 @@ public class Solution return true; if (sb.Length % 2 == 1) return false; - while(sb.Length > 0) + while (sb.Length > 0) { char ch = sb[0]; if ("({[".Contains(ch) == false) @@ -27,7 +27,7 @@ public class Solution int count2 = 0;// {} int count3 = 0;// [] bool succes = false; - for(int i = 1; i < sb.Length; i++) + for (int i = 1; i < sb.Length; i++) { char c = sb[i]; if (count1 == 0 && count2 == 0 && count3 == 0 && c == need) @@ -60,7 +60,7 @@ public class Solution break; } } - if(succes == false) + if (succes == false) return false; } return true; diff --git a/200. Number of Islands/Solution.cs b/200. Number of Islands/Solution.cs index 96ec932..9c25f70 100644 --- a/200. Number of Islands/Solution.cs +++ b/200. Number of Islands/Solution.cs @@ -1,6 +1,4 @@ -using System.ComponentModel; - -namespace _200._Number_of_Islands; +namespace _200._Number_of_Islands; public class Solution { @@ -28,7 +26,7 @@ public class Solution private void Recursion(char[][] grid, int[][] was, int y, int x, int id) { - if (was[y][x]!=0 || grid[y][x] != '1') + if (was[y][x] != 0 || grid[y][x] != '1') return; was[y][x] = id; for (int i = 1; i < 8; i += 2) diff --git a/232. Implement Queue using Stacks/MyQueue.cs b/232. Implement Queue using Stacks/MyQueue.cs index f8f5e8d..c5fad50 100644 --- a/232. Implement Queue using Stacks/MyQueue.cs +++ b/232. Implement Queue using Stacks/MyQueue.cs @@ -7,14 +7,14 @@ public class MyQueue public void Push(int x) { - while(_sOutput.TryPop(out int res)) + while (_sOutput.TryPop(out int res)) _sInput.Push(res); _sInput.Push(x); } public int Pop() { - while(_sInput.TryPop(out int res)) + while (_sInput.TryPop(out int res)) _sOutput.Push(res); return _sOutput.Pop(); } diff --git a/235. Lowest Common Ancestor of a Binary Search Tree/Program.cs b/235. Lowest Common Ancestor of a Binary Search Tree/Program.cs index 459ae49..b7cc03b 100644 --- a/235. Lowest Common Ancestor of a Binary Search Tree/Program.cs +++ b/235. Lowest Common Ancestor of a Binary Search Tree/Program.cs @@ -7,6 +7,6 @@ internal class Program TreeNode root = new TreeNode(6); root.left = new TreeNode(2) { left = new TreeNode(0), right = new TreeNode(4) { left = new TreeNode(3), right = new TreeNode(5) } }; root.right = new TreeNode(8) { left = new TreeNode(7), right = new TreeNode(9) }; - Console.WriteLine(new Solution().LowestCommonAncestor(root, new TreeNode(3), new TreeNode(5)).val); + Console.WriteLine(new Solution().LowestCommonAncestor(root, new TreeNode(3), new TreeNode(5))?.val); } } diff --git a/235. Lowest Common Ancestor of a Binary Search Tree/Solution.cs b/235. Lowest Common Ancestor of a Binary Search Tree/Solution.cs index 4cbd6e0..5c97672 100644 --- a/235. Lowest Common Ancestor of a Binary Search Tree/Solution.cs +++ b/235. Lowest Common Ancestor of a Binary Search Tree/Solution.cs @@ -6,11 +6,11 @@ public class Solution { int min = Math.Min(p.val, q.val); int max = Math.Max(p.val, q.val); - while(true) + while (true) { if (root == null) return null; - else if (min <= root.val && max>=root.val) + else if (min <= root.val && max >= root.val) return root; else if (root.val >= p.val) root = root.left!; diff --git a/235. Lowest Common Ancestor of a Binary Search Tree/TreeNode.cs b/235. Lowest Common Ancestor of a Binary Search Tree/TreeNode.cs index e732324..e0f3dfb 100644 --- a/235. Lowest Common Ancestor of a Binary Search Tree/TreeNode.cs +++ b/235. Lowest Common Ancestor of a Binary Search Tree/TreeNode.cs @@ -1,7 +1,8 @@ namespace _235._Lowest_Common_Ancestor_of_a_Binary_Search_Tree; //Definition for a binary tree node. -public class TreeNode { +public class TreeNode +{ public int val; public TreeNode? left; public TreeNode? right; diff --git a/3. Longest Substring Without Repeating Characters/Solution.cs b/3. Longest Substring Without Repeating Characters/Solution.cs index ae9111e..708e97e 100644 --- a/3. Longest Substring Without Repeating Characters/Solution.cs +++ b/3. Longest Substring Without Repeating Characters/Solution.cs @@ -6,10 +6,10 @@ public class Solution { int max = 0; Dictionary dict = new Dictionary(); - for(int i = 0; i < s.Length; i++) + for (int i = 0; i < s.Length; i++) { char c = s[i]; - if(dict.ContainsKey(c)) + if (dict.ContainsKey(c)) { max = Math.Max(max, dict.Count); i = dict[c]; diff --git a/509. Fibonacci Number/509. Fibonacci Number.csproj b/509. Fibonacci Number/509. Fibonacci Number.csproj new file mode 100644 index 0000000..ff6940a --- /dev/null +++ b/509. Fibonacci Number/509. Fibonacci Number.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _509._Fibonacci_Number + enable + enable + + + diff --git a/509. Fibonacci Number/Program.cs b/509. Fibonacci Number/Program.cs new file mode 100644 index 0000000..2171aa3 --- /dev/null +++ b/509. Fibonacci Number/Program.cs @@ -0,0 +1,8 @@ +namespace _509._Fibonacci_Number; + +internal class Program +{ + static void Main(string[] args) + { + } +} diff --git a/509. Fibonacci Number/Solution.cs b/509. Fibonacci Number/Solution.cs new file mode 100644 index 0000000..aa83057 --- /dev/null +++ b/509. Fibonacci Number/Solution.cs @@ -0,0 +1,20 @@ +namespace _509._Fibonacci_Number; + +public class Solution +{ + public int Fib(int n) + { + if (n == 0) + return 0; + n--; + int cur0 = 0; + int cur1 = 1; + while (--n > 0) + { + int buf = cur0 + cur1; + cur0 = cur1; + cur1 = buf; + } + return cur0 + cur1; + } +} \ No newline at end of file diff --git a/567. Permutation in String/Solution.cs b/567. Permutation in String/Solution.cs index 1a6e7fd..88811d7 100644 --- a/567. Permutation in String/Solution.cs +++ b/567. Permutation in String/Solution.cs @@ -6,19 +6,19 @@ public class Solution { int[] letters = new int[26]; foreach (char c in s1) - letters[c-97]++; + letters[c - 97]++; int[] buff = new int[26]; Array.Copy(letters, buff, letters.Length); - for (int i = 0; i <= s2.Length-s1.Length; i++) + for (int i = 0; i <= s2.Length - s1.Length; i++) { - if (letters[s2[i]-97] != 0) + if (letters[s2[i] - 97] != 0) { Array.Copy(letters, buff, letters.Length); - for(int j = i; j < i + s1.Length; j++) + for (int j = i; j < i + s1.Length; j++) { char ch = s2[j]; buff[ch - 97]--; - if (buff[ch-97] < 0) + if (buff[ch - 97] < 0) break; } bool win = true; diff --git a/617. Merge Two Binary Trees/617. Merge Two Binary Trees.csproj b/617. Merge Two Binary Trees/617. Merge Two Binary Trees.csproj new file mode 100644 index 0000000..c2574bf --- /dev/null +++ b/617. Merge Two Binary Trees/617. Merge Two Binary Trees.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _617._Merge_Two_Binary_Trees + enable + enable + + + diff --git a/617. Merge Two Binary Trees/Program.cs b/617. Merge Two Binary Trees/Program.cs new file mode 100644 index 0000000..ad3d577 --- /dev/null +++ b/617. Merge Two Binary Trees/Program.cs @@ -0,0 +1,58 @@ +namespace _617._Merge_Two_Binary_Trees; + +internal class Program +{ + static void Main(string[] args) + { + TreeNode root1 = new TreeNode(1); + root1.left = new TreeNode(3) { left = new TreeNode(5) }; + root1.right = new TreeNode(2); + + + TreeNode root2 = new TreeNode(2); + root2.left = new TreeNode(1) { right = new TreeNode(4) }; + root2.right = new TreeNode(3) { right = new TreeNode(7) }; + var res = new Write().LevelOrder(new Solution().MergeTrees(root1, root2)!); + Console.WriteLine($"\nOutput"); + foreach (var row in res) + Console.WriteLine(String.Join(" ", row)); + } + + public class Write + { + public IList> LevelOrder(TreeNode root) + { + List> output = new List>(); + RecursiveTraversal(output, new List() { root }); + return output; + } + + private void RecursiveTraversal(List> output, List curLevel) + { + if (curLevel.Count == 0) + return; + List levelValues = new List(); + foreach (var n in curLevel) + { + if (n == null) + continue; + levelValues.Add(n.val); + } + if (levelValues.Count == 0) + return; + output.Add(levelValues); + + List nextLevel = new List(); + foreach (var n in curLevel) + { + if (n == null) + continue; + if (n.left != null) + nextLevel.Add(n.left); + if (n.right != null) + nextLevel.Add(n.right); + } + RecursiveTraversal(output, nextLevel); + } + } +} diff --git a/617. Merge Two Binary Trees/Solution.cs b/617. Merge Two Binary Trees/Solution.cs new file mode 100644 index 0000000..0ac30d4 --- /dev/null +++ b/617. Merge Two Binary Trees/Solution.cs @@ -0,0 +1,31 @@ +namespace _617._Merge_Two_Binary_Trees; + +public class Solution +{ + public TreeNode? MergeTrees(TreeNode root1, TreeNode root2) + { + if (root1 == null && root2 == null) + return null; + else if (root1 == null) + return root2; + else if (root2 == null) + return root1; + var res = new TreeNode(); + RecursionMerge(res, root1, root2); + return res; + } + + private void RecursionMerge(TreeNode cur, TreeNode? t1, TreeNode? t2) + { + if (t1 != null) + cur.val += t1.val; + if (t2 != null) + cur.val += t2.val; + cur.left = (t1?.left != null || t2?.left != null) ? new TreeNode() : null; + cur.right = (t1?.right != null || t2?.right != null) ? new TreeNode() : null; + if (cur.left != null) + RecursionMerge(cur.left, t1?.left, t2?.left); + if (cur.right != null) + RecursionMerge(cur.right, t1?.right, t2?.right); + } +} \ No newline at end of file diff --git a/617. Merge Two Binary Trees/TreeNode.cs b/617. Merge Two Binary Trees/TreeNode.cs new file mode 100644 index 0000000..4d6ceb8 --- /dev/null +++ b/617. Merge Two Binary Trees/TreeNode.cs @@ -0,0 +1,16 @@ +namespace _617._Merge_Two_Binary_Trees; + + +//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/695. Max Area of Island/Program.cs b/695. Max Area of Island/Program.cs index 84ead6d..cebc157 100644 --- a/695. Max Area of Island/Program.cs +++ b/695. Max Area of Island/Program.cs @@ -4,7 +4,7 @@ internal class Program { static void Main(string[] args) { - int[][] matrix = {new int[] {0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0},new int[] {0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0},new int[] {0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0}}; + int[][] matrix = { new int[] { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, new int[] { 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0 }, new int[] { 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 }, new int[] { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 } }; Console.WriteLine(new Solution().MaxAreaOfIsland(matrix)); } } diff --git a/70. Climbing Stairs/70. Climbing Stairs.csproj b/70. Climbing Stairs/70. Climbing Stairs.csproj new file mode 100644 index 0000000..2b17eb3 --- /dev/null +++ b/70. Climbing Stairs/70. Climbing Stairs.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _70._Climbing_Stairs + enable + enable + + + diff --git a/70. Climbing Stairs/Program.cs b/70. Climbing Stairs/Program.cs new file mode 100644 index 0000000..e358650 --- /dev/null +++ b/70. Climbing Stairs/Program.cs @@ -0,0 +1,9 @@ +namespace _70._Climbing_Stairs; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/70. Climbing Stairs/Solution.cs b/70. Climbing Stairs/Solution.cs new file mode 100644 index 0000000..718dff0 --- /dev/null +++ b/70. Climbing Stairs/Solution.cs @@ -0,0 +1,19 @@ +namespace _70._Climbing_Stairs; + +public class Solution +{ + public int ClimbStairs(int n) + { + if (n == 0) + return 0; + int cur0 = 0; + int cur1 = 1; + while (--n > 0) + { + int buf = cur0 + cur1; + cur0 = cur1; + cur1 = buf; + } + return cur0 + cur1; + } +} \ No newline at end of file diff --git a/733. Flood Fill/Program.cs b/733. Flood Fill/Program.cs index 5a3d074..3b0b7e4 100644 --- a/733. Flood Fill/Program.cs +++ b/733. Flood Fill/Program.cs @@ -7,8 +7,8 @@ internal class Program int[][] matrix = new int[][] { new int[] { 1,1,1 }, new int[] { 1,1,0 }, new int[] { 1,0,1 } }; - var res = new Solution().FloodFill(matrix, 1,1, 2); - foreach(var row in res) + var res = new Solution().FloodFill(matrix, 1, 1, 2); + foreach (var row in res) Console.WriteLine(String.Join(" ", row)); } } diff --git a/83. Remove Duplicates from Sorted List/Solution.cs b/83. Remove Duplicates from Sorted List/Solution.cs index 45c7e7a..568dee0 100644 --- a/83. Remove Duplicates from Sorted List/Solution.cs +++ b/83. Remove Duplicates from Sorted List/Solution.cs @@ -19,7 +19,7 @@ public class Solution current = current.next; head = head.next; } - if(current != null) + if (current != null) current.next = null; return first; } diff --git a/94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.csproj b/94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.csproj new file mode 100644 index 0000000..3bf53c7 --- /dev/null +++ b/94. Binary Tree Inorder Traversal/94. Binary Tree Inorder Traversal.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _94._Binary_Tree_Inorder_Traversal + enable + enable + + + diff --git a/94. Binary Tree Inorder Traversal/Program.cs b/94. Binary Tree Inorder Traversal/Program.cs new file mode 100644 index 0000000..2a272cd --- /dev/null +++ b/94. Binary Tree Inorder Traversal/Program.cs @@ -0,0 +1,9 @@ +namespace _94._Binary_Tree_Inorder_Traversal; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} diff --git a/94. Binary Tree Inorder Traversal/Solution.cs b/94. Binary Tree Inorder Traversal/Solution.cs new file mode 100644 index 0000000..83c868f --- /dev/null +++ b/94. Binary Tree Inorder Traversal/Solution.cs @@ -0,0 +1,23 @@ +namespace _94._Binary_Tree_Inorder_Traversal; + +public class Solution +{ + public List inorderTraversal(TreeNode root) + { + List res = new List(); + Stack stack = new Stack(); + TreeNode? curr = root; + while (curr != null || stack.Count > 0) + { + while (curr != null) + { + stack.Push(curr); + curr = curr.left; + } + curr = stack.Pop(); + res.Add(curr.val); + curr = curr.right!; + } + return res; + } +} diff --git a/94. Binary Tree Inorder Traversal/TreeNode.cs b/94. Binary Tree Inorder Traversal/TreeNode.cs new file mode 100644 index 0000000..536a91e --- /dev/null +++ b/94. Binary Tree Inorder Traversal/TreeNode.cs @@ -0,0 +1,15 @@ +namespace _94._Binary_Tree_Inorder_Traversal; + +//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/98. Validate Binary Search Tree/Solution.cs b/98. Validate Binary Search Tree/Solution.cs index 0737c85..030c4df 100644 --- a/98. Validate Binary Search Tree/Solution.cs +++ b/98. Validate Binary Search Tree/Solution.cs @@ -9,13 +9,13 @@ public class Solution private bool Rec(TreeNode node, int? lessThan, int? moreThan) { - if((lessThan != null && node.val >= lessThan) || (moreThan != null && node.val <= moreThan)) + if ((lessThan != null && node.val >= lessThan) || (moreThan != null && node.val <= moreThan)) return false; bool res = true; - if(node.left != null) - res&=Rec(node.left, node.val, moreThan); - if(node.right != null) - res &=Rec(node.right, lessThan, node.val); + if (node.left != null) + res &= Rec(node.left, node.val, moreThan); + if (node.right != null) + res &= Rec(node.right, lessThan, node.val); return res; } } diff --git a/Leetcode.sln b/Leetcode.sln index 809017f..753fc8a 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -95,7 +95,21 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "98. Validate Binary Search EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "235. Lowest Common Ancestor of a Binary Search Tree", "235. Lowest Common Ancestor of a Binary Search Tree\235. Lowest Common Ancestor of a Binary Search Tree.csproj", "{B4466695-F56D-40E6-8D9C-D5955ACE8B4E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "200. Number of Islands", "200. Number of Islands\200. Number of Islands.csproj", "{02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}" +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}" +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}" +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}" +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}" +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}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -291,6 +305,34 @@ Global {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Debug|Any CPU.Build.0 = Debug|Any CPU {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.ActiveCfg = Release|Any CPU {02FDE4A9-07D5-46B6-95AB-B61E1F3BA97B}.Release|Any CPU.Build.0 = Release|Any CPU + {79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79FEA33C-30E7-4FE6-A7C8-18BD13EE1B9A}.Release|Any CPU.Build.0 = Release|Any CPU + {DD19137E-1A2F-4803-910B-A63826F65159}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DD19137E-1A2F-4803-910B-A63826F65159}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DD19137E-1A2F-4803-910B-A63826F65159}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DD19137E-1A2F-4803-910B-A63826F65159}.Release|Any CPU.Build.0 = Release|Any CPU + {0DA14E84-9F56-4384-9743-BA7FD7539113}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0DA14E84-9F56-4384-9743-BA7FD7539113}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0DA14E84-9F56-4384-9743-BA7FD7539113}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0DA14E84-9F56-4384-9743-BA7FD7539113}.Release|Any CPU.Build.0 = Release|Any CPU + {60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60E8258E-4BBC-42C7-96CD-C67DB629E0E1}.Release|Any CPU.Build.0 = Release|Any CPU + {6E954C2F-608C-4294-91CF-62B8E49CC415}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6E954C2F-608C-4294-91CF-62B8E49CC415}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6E954C2F-608C-4294-91CF-62B8E49CC415}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6E954C2F-608C-4294-91CF-62B8E49CC415}.Release|Any CPU.Build.0 = Release|Any CPU + {73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73450FFA-253B-42CC-B5E7-8769CFF1BE96}.Release|Any CPU.Build.0 = Release|Any CPU + {625340C0-FC67-4555-B49F-FB28E3177F72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE