diff --git a/1. Two Sum/Program.cs b/1. Two Sum/Program.cs index 1650ac8..308b0eb 100644 --- a/1. Two Sum/Program.cs +++ b/1. Two Sum/Program.cs @@ -1,10 +1,9 @@ -namespace _1._Two_Sum +namespace _1._Two_Sum; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/102. Binary Tree Level Order Traversal/102. Binary Tree Level Order Traversal.csproj b/102. Binary Tree Level Order Traversal/102. Binary Tree Level Order Traversal.csproj new file mode 100644 index 0000000..aa387e8 --- /dev/null +++ b/102. Binary Tree Level Order Traversal/102. Binary Tree Level Order Traversal.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _102._Binary_Tree_Level_Order_Traversal + enable + enable + + + diff --git a/102. Binary Tree Level Order Traversal/Program.cs b/102. Binary Tree Level Order Traversal/Program.cs new file mode 100644 index 0000000..d6c9e58 --- /dev/null +++ b/102. Binary Tree Level Order Traversal/Program.cs @@ -0,0 +1,9 @@ +namespace _102._Binary_Tree_Level_Order_Traversal; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} \ No newline at end of file diff --git a/102. Binary Tree Level Order Traversal/Solution.cs b/102. Binary Tree Level Order Traversal/Solution.cs new file mode 100644 index 0000000..0e37b44 --- /dev/null +++ b/102. Binary Tree Level Order Traversal/Solution.cs @@ -0,0 +1,39 @@ +namespace _102._Binary_Tree_Level_Order_Traversal; + +public class Solution +{ + 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/102. Binary Tree Level Order Traversal/TreeNode.cs b/102. Binary Tree Level Order Traversal/TreeNode.cs new file mode 100644 index 0000000..ccd8d96 --- /dev/null +++ b/102. Binary Tree Level Order Traversal/TreeNode.cs @@ -0,0 +1,17 @@ +namespace _102._Binary_Tree_Level_Order_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/118. Pascal's Triangle/Program.cs b/118. Pascal's Triangle/Program.cs index f7ca310..3efd224 100644 --- a/118. Pascal's Triangle/Program.cs +++ b/118. Pascal's Triangle/Program.cs @@ -1,12 +1,11 @@ -namespace _118._Pascal_s_Triangle +namespace _118._Pascal_s_Triangle; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var res = new Solution().Generate(300); - foreach (var r in res) - Console.WriteLine(String.Join(" ", r)); - } + var res = new Solution().Generate(300); + foreach (var r in res) + Console.WriteLine(String.Join(" ", r)); } } \ No newline at end of file diff --git a/121. Best Time to Buy and Sell Stock/Program.cs b/121. Best Time to Buy and Sell Stock/Program.cs index 497a3f9..f028c65 100644 --- a/121. Best Time to Buy and Sell Stock/Program.cs +++ b/121. Best Time to Buy and Sell Stock/Program.cs @@ -1,10 +1,9 @@ -namespace _121._Best_Time_to_Buy_and_Sell_Stock +namespace _121._Best_Time_to_Buy_and_Sell_Stock; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine(new Solution().MaxProfit(new int[] { 7, 1, 5, 3, 6, 4 })); - } + Console.WriteLine(new Solution().MaxProfit(new int[] { 7, 1, 5, 3, 6, 4 })); } } \ No newline at end of file diff --git a/142. Linked List Cycle II/Program.cs b/142. Linked List Cycle II/Program.cs index 36c7c6e..ab92f0b 100644 --- a/142. Linked List Cycle II/Program.cs +++ b/142. Linked List Cycle II/Program.cs @@ -1,15 +1,14 @@ -namespace _142._Linked_List_Cycle_II -{ - internal class Program - { - static void Main(string[] args) - { - var l1 = new ListNode(3); - var l2 = new ListNode(2); +namespace _142._Linked_List_Cycle_II; - l1.next = l2; - l2.next = l1; - Console.WriteLine(new Solution().DetectCycle(l1)?.val); - } +internal class Program +{ + static void Main(string[] args) + { + var l1 = new ListNode(3); + var l2 = new ListNode(2); + + l1.next = l2; + l2.next = l1; + Console.WriteLine(new Solution().DetectCycle(l1)?.val); } } \ No newline at end of file diff --git a/1480. Running Sum of 1d Array/Program.cs b/1480. Running Sum of 1d Array/Program.cs index 7eb4b6e..84e8b95 100644 --- a/1480. Running Sum of 1d Array/Program.cs +++ b/1480. Running Sum of 1d Array/Program.cs @@ -1,10 +1,9 @@ -namespace _1480._Running_Sum_of_1d_Array +namespace _1480._Running_Sum_of_1d_Array; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/167. Two Sum II - Input Array Is Sorted/Program.cs b/167. Two Sum II - Input Array Is Sorted/Program.cs index 8024f33..e07acb1 100644 --- a/167. Two Sum II - Input Array Is Sorted/Program.cs +++ b/167. Two Sum II - Input Array Is Sorted/Program.cs @@ -1,12 +1,11 @@ -namespace _167._Two_Sum_II___Input_Array_Is_Sorted +namespace _167._Two_Sum_II___Input_Array_Is_Sorted; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var arr = new int[] { -2, -3, 4 }; - var res = new Solution().TwoSum(arr, -5); - Console.WriteLine(String.Join(" ", res)); - } + 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/189. Rotate Array/Program.cs b/189. Rotate Array/Program.cs index 23e38f3..af7611c 100644 --- a/189. Rotate Array/Program.cs +++ b/189. Rotate Array/Program.cs @@ -1,12 +1,11 @@ -namespace _189._Rotate_Array +namespace _189._Rotate_Array; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var arr = new int[] { 1, 2, 3, 4, 5, 6 }; - new Solution().Rotate(arr, 3); - Console.WriteLine(String.Join(" ", arr)); - } + var arr = new int[] { 1, 2, 3, 4, 5, 6 }; + new Solution().Rotate(arr, 3); + Console.WriteLine(String.Join(" ", arr)); } } \ No newline at end of file diff --git a/205. Isomorphic Strings/Program.cs b/205. Isomorphic Strings/Program.cs index dae734d..21e3e94 100644 --- a/205. Isomorphic Strings/Program.cs +++ b/205. Isomorphic Strings/Program.cs @@ -1,10 +1,9 @@ -namespace _205._Isomorphic_Strings +namespace _205._Isomorphic_Strings; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine(new Solution().IsIsomorphic("egg", "foo")); - } + Console.WriteLine(new Solution().IsIsomorphic("egg", "foo")); } } \ No newline at end of file diff --git a/206. Reverse Linked List/Program.cs b/206. Reverse Linked List/Program.cs index 9f6bd6d..770d4e4 100644 --- a/206. Reverse Linked List/Program.cs +++ b/206. Reverse Linked List/Program.cs @@ -1,11 +1,10 @@ -namespace _206._Reverse_Linked_List +namespace _206._Reverse_Linked_List; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var l = ListNode.Create(new int[] { 1 }); - Console.WriteLine(new Solution().ReverseList(l)); - } + var l = ListNode.Create(new int[] { 1 }); + Console.WriteLine(new Solution().ReverseList(l)); } } \ No newline at end of file diff --git a/21. Merge Two Sorted Lists/Program.cs b/21. Merge Two Sorted Lists/Program.cs index d22629d..b125962 100644 --- a/21. Merge Two Sorted Lists/Program.cs +++ b/21. Merge Two Sorted Lists/Program.cs @@ -1,12 +1,11 @@ -namespace _21._Merge_Two_Sorted_Lists +namespace _21._Merge_Two_Sorted_Lists; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var l1 = ListNode.Create(new int[] { 1, 2, 4 }); - var l2 = ListNode.Create(new int[] { 1, 3, 4 }); - Console.WriteLine(new Solution().MergeTwoLists(l1, l2)); - } + var l1 = ListNode.Create(new int[] { 1, 2, 4 }); + var l2 = ListNode.Create(new int[] { 1, 3, 4 }); + Console.WriteLine(new Solution().MergeTwoLists(l1, l2)); } } \ No newline at end of file diff --git a/217. Contains Duplicate/Program.cs b/217. Contains Duplicate/Program.cs index 01f794e..5a59f12 100644 --- a/217. Contains Duplicate/Program.cs +++ b/217. Contains Duplicate/Program.cs @@ -1,10 +1,9 @@ -namespace _217._Contains_Duplicate +namespace _217._Contains_Duplicate; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/240. Search a 2D Matrix II/Program.cs b/240. Search a 2D Matrix II/Program.cs index 2eb5b48..87e9862 100644 --- a/240. Search a 2D Matrix II/Program.cs +++ b/240. Search a 2D Matrix II/Program.cs @@ -1,11 +1,10 @@ -namespace _240._Search_a_2D_Matrix_II +namespace _240._Search_a_2D_Matrix_II; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - int[][] matrix = new int[][] { new int[] { 1, 1, 1, 1 } }; - Console.WriteLine(new Solution().SearchMatrix(matrix, 2)); - } + 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/242. Valid Anagram/242. Valid Anagram.csproj b/242. Valid Anagram/242. Valid Anagram.csproj new file mode 100644 index 0000000..95b2220 --- /dev/null +++ b/242. Valid Anagram/242. Valid Anagram.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _242._Valid_Anagram + enable + enable + + + diff --git a/242. Valid Anagram/Program.cs b/242. Valid Anagram/Program.cs new file mode 100644 index 0000000..fa05a5a --- /dev/null +++ b/242. Valid Anagram/Program.cs @@ -0,0 +1,9 @@ +namespace _242._Valid_Anagram; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} \ No newline at end of file diff --git a/242. Valid Anagram/Solution.cs b/242. Valid Anagram/Solution.cs new file mode 100644 index 0000000..1cd06bb --- /dev/null +++ b/242. Valid Anagram/Solution.cs @@ -0,0 +1,36 @@ +namespace _242._Valid_Anagram; + +public class Solution +{ + public bool IsAnagram(string s, string t) + { + Dictionary d1 = CountChars(s); + Dictionary d2 = CountChars(t); + if (d1.Count != d2.Count) + return false; + foreach (var kp in d1) + { + if (d2.ContainsKey(kp.Key)) + { + if (d2[kp.Key] != kp.Value) + return false; + } + else + return false; + } + return true; + } + + private Dictionary CountChars(string str) + { + Dictionary dict = new Dictionary(); + foreach (char ch in str) + { + if (dict.ContainsKey(ch)) + dict[ch]++; + else + dict.Add(ch, 1); + } + return dict; + } +} diff --git a/278. First Bad Version/Program.cs b/278. First Bad Version/Program.cs index d16af1e..2fd332a 100644 --- a/278. First Bad Version/Program.cs +++ b/278. First Bad Version/Program.cs @@ -1,10 +1,9 @@ -namespace _278._First_Bad_Version +namespace _278._First_Bad_Version; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine(new Solution().FirstBadVersion(2147483647, 2147483647)); - } + Console.WriteLine(new Solution().FirstBadVersion(2147483647, 2147483647)); } } \ No newline at end of file diff --git a/283. Move Zeroes/Program.cs b/283. Move Zeroes/Program.cs index ad49729..8e6a979 100644 --- a/283. Move Zeroes/Program.cs +++ b/283. Move Zeroes/Program.cs @@ -1,12 +1,11 @@ -namespace _283._Move_Zeroes +namespace _283._Move_Zeroes; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var test = new int[] { 0, 1, 0, 3, 12 }; - new Solution().MoveZeroes(test); - Console.WriteLine(String.Join(" ", test)); - } + 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/344. Reverse String/344. Reverse String.csproj b/344. Reverse String/344. Reverse String.csproj new file mode 100644 index 0000000..9c22a42 --- /dev/null +++ b/344. Reverse String/344. Reverse String.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _344._Reverse_String + enable + enable + + + diff --git a/344. Reverse String/Program.cs b/344. Reverse String/Program.cs new file mode 100644 index 0000000..a7f4137 --- /dev/null +++ b/344. Reverse String/Program.cs @@ -0,0 +1,11 @@ +namespace _344._Reverse_String; + +internal class Program +{ + static void Main(string[] args) + { + var arr = "Hello, World!".ToArray(); + new Solution().ReverseString(arr); + Console.WriteLine(String.Join("", arr)); + } +} \ No newline at end of file diff --git a/344. Reverse String/Solution.cs b/344. Reverse String/Solution.cs new file mode 100644 index 0000000..ce2aa6d --- /dev/null +++ b/344. Reverse String/Solution.cs @@ -0,0 +1,14 @@ +namespace _344._Reverse_String; + +public class Solution +{ + public void ReverseString(T[] arr) + { + for (int i = 0; i < arr.Length / 2; i++) + { + T buf = arr[i]; + arr[i] = arr[arr.Length - 1 - i]; + arr[arr.Length - 1 - i] = buf; + } + } +} diff --git a/35. Search Insert Position/Program.cs b/35. Search Insert Position/Program.cs index 2d84832..53766f4 100644 --- a/35. Search Insert Position/Program.cs +++ b/35. Search Insert Position/Program.cs @@ -1,9 +1,8 @@ -namespace _35._Search_Insert_Position +namespace _35._Search_Insert_Position; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - } } } \ No newline at end of file diff --git a/350. Intersection of Two Arrays II/Program.cs b/350. Intersection of Two Arrays II/Program.cs index 3a7d142..8b51403 100644 --- a/350. Intersection of Two Arrays II/Program.cs +++ b/350. Intersection of Two Arrays II/Program.cs @@ -1,10 +1,9 @@ -namespace _350._Intersection_of_Two_Arrays_II +namespace _350._Intersection_of_Two_Arrays_II; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/36. Valid Sudoku/Program.cs b/36. Valid Sudoku/Program.cs index d8c003c..9e41772 100644 --- a/36. Valid Sudoku/Program.cs +++ b/36. Valid Sudoku/Program.cs @@ -1,19 +1,18 @@ -namespace _36._Valid_Sudoku +namespace _36._Valid_Sudoku; + +internal class Program { - internal class Program + static void Main(string[] args) { - 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)); - } + 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/383. Ransom Note/383. Ransom Note.csproj b/383. Ransom Note/383. Ransom Note.csproj new file mode 100644 index 0000000..c106eeb --- /dev/null +++ b/383. Ransom Note/383. Ransom Note.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _383._Ransom_Note + enable + enable + + + diff --git a/383. Ransom Note/Program.cs b/383. Ransom Note/Program.cs new file mode 100644 index 0000000..b32054a --- /dev/null +++ b/383. Ransom Note/Program.cs @@ -0,0 +1,9 @@ +namespace _383._Ransom_Note; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().CanConstruct("a", "b")); + } +} \ No newline at end of file diff --git a/383. Ransom Note/Solution.cs b/383. Ransom Note/Solution.cs new file mode 100644 index 0000000..8c5a62a --- /dev/null +++ b/383. Ransom Note/Solution.cs @@ -0,0 +1,36 @@ +namespace _383._Ransom_Note; + +public class Solution +{ + public bool CanConstruct(string ransomNote, string magazine) + { + if (ransomNote.Length > magazine.Length) + return false; + Dictionary note = CountChars(ransomNote); + Dictionary mag = CountChars(magazine); + foreach (var kp in note) + { + if (mag.ContainsKey(kp.Key)) + { + if (mag[kp.Key] < kp.Value) + return false; + } + else + return false; + } + return true; + } + + private Dictionary CountChars(string str) + { + Dictionary dict = new Dictionary(); + foreach (char ch in str) + { + if (dict.ContainsKey(ch)) + dict[ch]++; + else + dict.Add(ch, 1); + } + return dict; + } +} diff --git a/387. First Unique Character in a String/387. First Unique Character in a String.csproj b/387. First Unique Character in a String/387. First Unique Character in a String.csproj new file mode 100644 index 0000000..7c2e1ff --- /dev/null +++ b/387. First Unique Character in a String/387. First Unique Character in a String.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _387._First_Unique_Character_in_a_String + enable + enable + + + diff --git a/387. First Unique Character in a String/Program.cs b/387. First Unique Character in a String/Program.cs new file mode 100644 index 0000000..fc98114 --- /dev/null +++ b/387. First Unique Character in a String/Program.cs @@ -0,0 +1,9 @@ +namespace _387._First_Unique_Character_in_a_String; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().FirstUniqChar("Leetcode")); + } +} \ No newline at end of file diff --git a/387. First Unique Character in a String/Solution.cs b/387. First Unique Character in a String/Solution.cs new file mode 100644 index 0000000..f76e8b7 --- /dev/null +++ b/387. First Unique Character in a String/Solution.cs @@ -0,0 +1,20 @@ +namespace _387._First_Unique_Character_in_a_String; + +public class Solution +{ + public int FirstUniqChar(string s) + { + Dictionary dic = new Dictionary(); + foreach (char ch in s) + { + if (dic.ContainsKey(ch)) + dic[ch]++; + else + dic.Add(ch, 1); + } + for (int i = 0; i < s.Length; i++) + if (dic[s[i]] == 1) + return i; + return -1; + } +} diff --git a/392. Is Subsequence/Program.cs b/392. Is Subsequence/Program.cs index 4af92ae..b87d2f3 100644 --- a/392. Is Subsequence/Program.cs +++ b/392. Is Subsequence/Program.cs @@ -1,10 +1,9 @@ -namespace _392._Is_Subsequence +namespace _392._Is_Subsequence; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/409. Longest Palindrome/Program.cs b/409. Longest Palindrome/Program.cs index f5dc844..1471d92 100644 --- a/409. Longest Palindrome/Program.cs +++ b/409. Longest Palindrome/Program.cs @@ -1,10 +1,9 @@ -namespace _409._Longest_Palindrome +namespace _409._Longest_Palindrome; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine(new Solution().LongestPalindrome("bb")); - } + Console.WriteLine(new Solution().LongestPalindrome("bb")); } } \ No newline at end of file diff --git a/53. Maximum Subarray/Program.cs b/53. Maximum Subarray/Program.cs index 6489b0f..3bf9cf4 100644 --- a/53. Maximum Subarray/Program.cs +++ b/53. Maximum Subarray/Program.cs @@ -1,10 +1,9 @@ -namespace _53._Maximum_Subarray +namespace _53._Maximum_Subarray; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine("Hello, World!"); - } + Console.WriteLine("Hello, World!"); } } \ No newline at end of file diff --git a/557. Reverse Words in a String III/557. Reverse Words in a String III.csproj b/557. Reverse Words in a String III/557. Reverse Words in a String III.csproj new file mode 100644 index 0000000..c8ed6ef --- /dev/null +++ b/557. Reverse Words in a String III/557. Reverse Words in a String III.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _557._Reverse_Words_in_a_String_III + enable + enable + + + diff --git a/557. Reverse Words in a String III/Program.cs b/557. Reverse Words in a String III/Program.cs new file mode 100644 index 0000000..642019a --- /dev/null +++ b/557. Reverse Words in a String III/Program.cs @@ -0,0 +1,9 @@ +namespace _557._Reverse_Words_in_a_String_III; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine(new Solution().ReverseWords("God Ding")); + } +} \ No newline at end of file diff --git a/557. Reverse Words in a String III/Solution.cs b/557. Reverse Words in a String III/Solution.cs new file mode 100644 index 0000000..f9e90dd --- /dev/null +++ b/557. Reverse Words in a String III/Solution.cs @@ -0,0 +1,25 @@ +using System.Text; + +namespace _557._Reverse_Words_in_a_String_III; + +public class Solution +{ + public string ReverseWords(string s) + { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < s.Length; i++) + { + if (s[i] == ' ') + sb.Append(' '); + else + { + int startIndex = i; + while (i < s.Length - 1 && s[i + 1] != ' ') + i++; + for (int r = i; r >= startIndex; r--) + sb.Append(s[r]); + } + } + return sb.ToString(); + } +} diff --git a/566. Reshape the Matrix/Program.cs b/566. Reshape the Matrix/Program.cs index 5a588ef..08aa321 100644 --- a/566. Reshape the Matrix/Program.cs +++ b/566. Reshape the Matrix/Program.cs @@ -1,12 +1,11 @@ -namespace _566._Reshape_the_Matrix +namespace _566._Reshape_the_Matrix; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var res = new Solution().MatrixReshape(new int[][] { new int[] { 1, 2 }, new int[] { 4, 5 } }, 2, 4); - foreach (var row in res) - Console.WriteLine(String.Join(" ", row)); - } + var res = new Solution().MatrixReshape(new int[][] { new int[] { 1, 2 }, new int[] { 4, 5 } }, 2, 4); + foreach (var row in res) + Console.WriteLine(String.Join(" ", row)); } } \ No newline at end of file diff --git a/589. N-ary Tree Preorder Traversal/589. N-ary Tree Preorder Traversal.csproj b/589. N-ary Tree Preorder Traversal/589. N-ary Tree Preorder Traversal.csproj new file mode 100644 index 0000000..8d8c482 --- /dev/null +++ b/589. N-ary Tree Preorder Traversal/589. N-ary Tree Preorder Traversal.csproj @@ -0,0 +1,11 @@ + + + + Exe + net6.0 + _589._N_ary_Tree_Preorder_Traversal + enable + enable + + + diff --git a/589. N-ary Tree Preorder Traversal/Node.cs b/589. N-ary Tree Preorder Traversal/Node.cs new file mode 100644 index 0000000..077e1bd --- /dev/null +++ b/589. N-ary Tree Preorder Traversal/Node.cs @@ -0,0 +1,21 @@ +namespace _589._N_ary_Tree_Preorder_Traversal; + +// Definition for a Node. +public class Node +{ + public int val; + public IList? children; + + public Node() { } + + public Node(int _val) + { + val = _val; + } + + public Node(int _val, IList _children) + { + val = _val; + children = _children; + } +} diff --git a/589. N-ary Tree Preorder Traversal/Program.cs b/589. N-ary Tree Preorder Traversal/Program.cs new file mode 100644 index 0000000..4754efd --- /dev/null +++ b/589. N-ary Tree Preorder Traversal/Program.cs @@ -0,0 +1,9 @@ +namespace _589._N_ary_Tree_Preorder_Traversal; + +internal class Program +{ + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } +} \ No newline at end of file diff --git a/589. N-ary Tree Preorder Traversal/Solution.cs b/589. N-ary Tree Preorder Traversal/Solution.cs new file mode 100644 index 0000000..5d7a2af --- /dev/null +++ b/589. N-ary Tree Preorder Traversal/Solution.cs @@ -0,0 +1,22 @@ +namespace _589._N_ary_Tree_Preorder_Traversal; + +public class Solution +{ + public IList Preorder(Node root) + { + List list = new List(); + RecursiveTraversal(list, root); + return list; + } + + private void RecursiveTraversal(List output, Node cur) + { + if (cur == null) + return; + output.Add(cur.val); + if (cur.children == null) + return; + foreach (var child in cur.children) + RecursiveTraversal(output, child); + } +} diff --git a/704. Binary Search/Program.cs b/704. Binary Search/Program.cs index f70ea71..51e99dd 100644 --- a/704. Binary Search/Program.cs +++ b/704. Binary Search/Program.cs @@ -1,10 +1,9 @@ -namespace _704._Binary_Search +namespace _704._Binary_Search; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Console.WriteLine(new Solution().Search(new int[] { -1, 0, 3, 5, 9, 12 }, 13)); - } + Console.WriteLine(new Solution().Search(new int[] { -1, 0, 3, 5, 9, 12 }, 13)); } } \ No newline at end of file diff --git a/724. Find Pivot Index/Program.cs b/724. Find Pivot Index/Program.cs index 360e9f2..d39e6d5 100644 --- a/724. Find Pivot Index/Program.cs +++ b/724. Find Pivot Index/Program.cs @@ -1,11 +1,10 @@ -namespace _724._Find_Pivot_Index +namespace _724._Find_Pivot_Index; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Solution sol = new Solution(); - sol.PivotIndex(new int[] { 1, 4, 2 }); - } + Solution sol = new Solution(); + sol.PivotIndex(new int[] { 1, 4, 2 }); } } \ No newline at end of file diff --git a/74. Search a 2D Matrix/Program.cs b/74. Search a 2D Matrix/Program.cs index 86b3eed..0a834e1 100644 --- a/74. Search a 2D Matrix/Program.cs +++ b/74. Search a 2D Matrix/Program.cs @@ -1,11 +1,10 @@ -namespace _74._Search_a_2D_Matrix +namespace _74._Search_a_2D_Matrix; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - int[][] matrix = new int[][] { new int[] { 1 } }; - Console.WriteLine(new Solution().SearchMatrix(matrix, 1)); - } + int[][] matrix = new int[][] { new int[] { 1 } }; + Console.WriteLine(new Solution().SearchMatrix(matrix, 1)); } } \ No newline at end of file diff --git a/876. Middle of the Linked List/Program.cs b/876. Middle of the Linked List/Program.cs index bbca776..1cc6191 100644 --- a/876. Middle of the Linked List/Program.cs +++ b/876. Middle of the Linked List/Program.cs @@ -1,13 +1,12 @@ -namespace _876._Middle_of_the_Linked_List +namespace _876._Middle_of_the_Linked_List; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var l1 = ListNode.Create(new int[] { 1, 2, 3, 4, 5 }); - var l2 = ListNode.Create(new int[] { 1, 2, 3, 4, 5, 6 }); - Console.WriteLine(new Solution().MiddleNode(l1).val); - Console.WriteLine(new Solution().MiddleNode(l2).val); - } + var l1 = ListNode.Create(new int[] { 1, 2, 3, 4, 5 }); + var l2 = ListNode.Create(new int[] { 1, 2, 3, 4, 5, 6 }); + Console.WriteLine(new Solution().MiddleNode(l1).val); + Console.WriteLine(new Solution().MiddleNode(l2).val); } } \ No newline at end of file diff --git a/88. Merge Sorted Array/Program.cs b/88. Merge Sorted Array/Program.cs index 2aab37f..beed456 100644 --- a/88. Merge Sorted Array/Program.cs +++ b/88. Merge Sorted Array/Program.cs @@ -1,14 +1,13 @@ -namespace _88._Merge_Sorted_Array +namespace _88._Merge_Sorted_Array; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - int[] n1 = new int[] { 1, 2, 3, 0, 0, 0 }; - int[] n2 = new int[] { 2, 5, 6 }; - Solution solution = new Solution(); - solution.Merge(n1, 3, n2, n2.Length); - Console.WriteLine(String.Join(" ", n1)); - } + int[] n1 = new int[] { 1, 2, 3, 0, 0, 0 }; + int[] n2 = new int[] { 2, 5, 6 }; + Solution solution = new Solution(); + solution.Merge(n1, 3, n2, n2.Length); + Console.WriteLine(String.Join(" ", n1)); } } \ No newline at end of file diff --git a/977. Squares of a Sorted Array/Program.cs b/977. Squares of a Sorted Array/Program.cs index bdea960..b926035 100644 --- a/977. Squares of a Sorted Array/Program.cs +++ b/977. Squares of a Sorted Array/Program.cs @@ -1,11 +1,10 @@ -namespace _977._Squares_of_a_Sorted_Array +namespace _977._Squares_of_a_Sorted_Array; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var res = new Solution().SortedSquares(new int[] { -7, -2, -1 }); - Console.WriteLine(String.Join(" ", res)); - } + var res = new Solution().SortedSquares(new int[] { -7, -2, -1 }); + Console.WriteLine(String.Join(" ", res)); } } \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 392e742..97e1c56 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -53,9 +53,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "74. Search a 2D Matrix", "7 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "36. Valid Sudoku", "36. Valid Sudoku\36. Valid Sudoku.csproj", "{C1759C38-D2E4-4869-B6C2-259B26586E11}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "344. Reverse String", "344. Reverse String\344. Reverse String.csproj", "{65D5229E-6D7D-41D2-BB68-2EBA40E9DCE3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "557. Reverse Words in a String III", "557. Reverse Words in a String III\557. Reverse Words in a String III.csproj", "{B3E1C7CD-0992-49BB-B44A-E129912763DB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "387. First Unique Character in a String", "387. First Unique Character in a String\387. First Unique Character in a String.csproj", "{3B479362-7079-4112-AFA1-3C344E83C8C2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "383. Ransom Note", "383. Ransom Note\383. Ransom Note.csproj", "{6F43E7FE-AD78-47D6-86FA-33B42DC55882}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "242. Valid Anagram", "242. Valid Anagram\242. Valid Anagram.csproj", "{FBD8C83E-D3F9-4CE3-B5DD-9C326CBC692C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "589. N-ary Tree Preorder Traversal", "589. N-ary Tree Preorder Traversal\589. N-ary Tree Preorder Traversal.csproj", "{4A8355E0-FF05-4DF3-B735-4B11BEAD95C5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "102. Binary Tree Level Order Traversal", "102. Binary Tree Level Order Traversal\102. Binary Tree Level Order Traversal.csproj", "{557D8195-C886-4B52-BBC4-285A1C1057FF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -171,6 +185,34 @@ Global {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 + {65D5229E-6D7D-41D2-BB68-2EBA40E9DCE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {65D5229E-6D7D-41D2-BB68-2EBA40E9DCE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {65D5229E-6D7D-41D2-BB68-2EBA40E9DCE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {65D5229E-6D7D-41D2-BB68-2EBA40E9DCE3}.Release|Any CPU.Build.0 = Release|Any CPU + {B3E1C7CD-0992-49BB-B44A-E129912763DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B3E1C7CD-0992-49BB-B44A-E129912763DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B3E1C7CD-0992-49BB-B44A-E129912763DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B3E1C7CD-0992-49BB-B44A-E129912763DB}.Release|Any CPU.Build.0 = Release|Any CPU + {3B479362-7079-4112-AFA1-3C344E83C8C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3B479362-7079-4112-AFA1-3C344E83C8C2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3B479362-7079-4112-AFA1-3C344E83C8C2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3B479362-7079-4112-AFA1-3C344E83C8C2}.Release|Any CPU.Build.0 = Release|Any CPU + {6F43E7FE-AD78-47D6-86FA-33B42DC55882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F43E7FE-AD78-47D6-86FA-33B42DC55882}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F43E7FE-AD78-47D6-86FA-33B42DC55882}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F43E7FE-AD78-47D6-86FA-33B42DC55882}.Release|Any CPU.Build.0 = Release|Any CPU + {FBD8C83E-D3F9-4CE3-B5DD-9C326CBC692C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FBD8C83E-D3F9-4CE3-B5DD-9C326CBC692C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FBD8C83E-D3F9-4CE3-B5DD-9C326CBC692C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FBD8C83E-D3F9-4CE3-B5DD-9C326CBC692C}.Release|Any CPU.Build.0 = Release|Any CPU + {4A8355E0-FF05-4DF3-B735-4B11BEAD95C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4A8355E0-FF05-4DF3-B735-4B11BEAD95C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4A8355E0-FF05-4DF3-B735-4B11BEAD95C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4A8355E0-FF05-4DF3-B735-4B11BEAD95C5}.Release|Any CPU.Build.0 = Release|Any CPU + {557D8195-C886-4B52-BBC4-285A1C1057FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {557D8195-C886-4B52-BBC4-285A1C1057FF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {557D8195-C886-4B52-BBC4-285A1C1057FF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {557D8195-C886-4B52-BBC4-285A1C1057FF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE