From e38dc6e3cd432e7cadb0956fc78e1721b0acc897 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 5 Feb 2025 08:18:08 +0100 Subject: [PATCH 1/3] feat: add swift implementation to lcp problem: No.67 --- .../README.md" | 42 +++++++++++++++++++ .../Solution.swift" | 36 ++++++++++++++++ .../Solution.cs | 17 -------- .../Solution.cs | 22 ---------- 4 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" delete mode 100644 solution/1500-1599/1535.Find the Winner of an Array Game/Solution.cs delete mode 100644 solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" index e4d4babb6c862..04fd25cbe099b 100644 --- "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" @@ -197,6 +197,48 @@ func expandBinaryTree(root *TreeNode) *TreeNode { } ``` +#### Swift + +```swift +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? + +* init() { self.val = 0; self.left = nil; self.right = nil } +* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil } +* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + func expandBinaryTree(_ root: TreeNode?) -> TreeNode? { + return dfs(root) + } + + private func dfs(_ root: TreeNode?) -> TreeNode? { + guard let root = root else { return nil } + + let leftChild = dfs(root.left) + let rightChild = dfs(root.right) + + if let leftChild = leftChild { + root.left = TreeNode(-1, leftChild, nil) + } + if let rightChild = rightChild { + root.right = TreeNode(-1, nil, rightChild) + } + + return root + } +} + +``` + diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" new file mode 100644 index 0000000000000..56a1d82045ec9 --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" @@ -0,0 +1,36 @@ +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? + +* init() { self.val = 0; self.left = nil; self.right = nil } +* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil } +* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + func expandBinaryTree(_ root: TreeNode?) -> TreeNode? { + return dfs(root) + } + + private func dfs(_ root: TreeNode?) -> TreeNode? { + guard let root = root else { return nil } + + let leftChild = dfs(root.left) + let rightChild = dfs(root.right) + + if let leftChild = leftChild { + root.left = TreeNode(-1, leftChild, nil) + } + if let rightChild = rightChild { + root.right = TreeNode(-1, nil, rightChild) + } + + return root + } +} diff --git a/solution/1500-1599/1535.Find the Winner of an Array Game/Solution.cs b/solution/1500-1599/1535.Find the Winner of an Array Game/Solution.cs deleted file mode 100644 index 8f495550493d3..0000000000000 --- a/solution/1500-1599/1535.Find the Winner of an Array Game/Solution.cs +++ /dev/null @@ -1,17 +0,0 @@ -public class Solution { - public int GetWinner(int[] arr, int k) { - int maxElement = arr[0], count = 0; - for (int i = 1; i < arr.Length; i++) { - if (maxElement < arr[i]) { - maxElement = arr[i]; - count = 1; - } else { - count++; - } - if (count == k) { - break; - } - } - return maxElement; - } -} diff --git a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs b/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs deleted file mode 100644 index 17a35674faffb..0000000000000 --- a/solution/2300-2399/2389.Longest Subsequence With Limited Sum/Solution.cs +++ /dev/null @@ -1,22 +0,0 @@ -public class Solution { - public int[] AnswerQueries(int[] nums, int[] queries) { - int[] result = new int[queries.Length]; - Array.Sort(nums); - for (int i = 0; i < queries.Length; i++) { - result[i] = getSubsequent(nums, queries[i]); - } - return result; - - } - - public int getSubsequent(int[] nums,int query) { - int sum = 0; - for (int i = 0; i < nums.Length; i++) { - sum += nums[i]; - if (sum > query) { - return i; - } - } - return nums.Length; - } -} From 90e3dbda4db2e44116a0c12f35e84d05c2b7af24 Mon Sep 17 00:00:00 2001 From: klever34 <12745225+klever34@users.noreply.github.com> Date: Wed, 5 Feb 2025 07:44:29 +0000 Subject: [PATCH 2/3] style: format code and docs with prettier --- .../README.md" | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" index 04fd25cbe099b..6865ce236d4ba 100644 --- "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" @@ -204,7 +204,7 @@ func expandBinaryTree(root *TreeNode) *TreeNode { * var val: Int * var left: TreeNode? * var right: TreeNode? - + * init() { self.val = 0; self.left = nil; self.right = nil } * init(_ val: Int) { self.val = val; self.left = nil; self.right = nil } * init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { @@ -219,20 +219,20 @@ class Solution { func expandBinaryTree(_ root: TreeNode?) -> TreeNode? { return dfs(root) } - + private func dfs(_ root: TreeNode?) -> TreeNode? { guard let root = root else { return nil } - + let leftChild = dfs(root.left) let rightChild = dfs(root.right) - + if let leftChild = leftChild { root.left = TreeNode(-1, leftChild, nil) } if let rightChild = rightChild { root.right = TreeNode(-1, nil, rightChild) } - + return root } } From 1913e7a7323e098ad1a93442da698df8cb9f0a96 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Wed, 5 Feb 2025 10:08:21 +0100 Subject: [PATCH 3/3] revert file --- .../README.md" | 42 +++++++++++++++++++ .../Solution.swift" | 36 ++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" index e4d4babb6c862..04fd25cbe099b 100644 --- "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/README.md" @@ -197,6 +197,48 @@ func expandBinaryTree(root *TreeNode) *TreeNode { } ``` +#### Swift + +```swift +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? + +* init() { self.val = 0; self.left = nil; self.right = nil } +* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil } +* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + func expandBinaryTree(_ root: TreeNode?) -> TreeNode? { + return dfs(root) + } + + private func dfs(_ root: TreeNode?) -> TreeNode? { + guard let root = root else { return nil } + + let leftChild = dfs(root.left) + let rightChild = dfs(root.right) + + if let leftChild = leftChild { + root.left = TreeNode(-1, leftChild, nil) + } + if let rightChild = rightChild { + root.right = TreeNode(-1, nil, rightChild) + } + + return root + } +} + +``` + diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" new file mode 100644 index 0000000000000..56a1d82045ec9 --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.swift" @@ -0,0 +1,36 @@ +/* class TreeNode { +* var val: Int +* var left: TreeNode? +* var right: TreeNode? + +* init() { self.val = 0; self.left = nil; self.right = nil } +* init(_ val: Int) { self.val = val; self.left = nil; self.right = nil } +* init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { +* self.val = val +* self.left = left +* self.right = right +* } +* } +*/ + +class Solution { + func expandBinaryTree(_ root: TreeNode?) -> TreeNode? { + return dfs(root) + } + + private func dfs(_ root: TreeNode?) -> TreeNode? { + guard let root = root else { return nil } + + let leftChild = dfs(root.left) + let rightChild = dfs(root.right) + + if let leftChild = leftChild { + root.left = TreeNode(-1, leftChild, nil) + } + if let rightChild = rightChild { + root.right = TreeNode(-1, nil, rightChild) + } + + return root + } +}