From 105b45314d73c4b65a803fef7d021c4f886be5e5 Mon Sep 17 00:00:00 2001 From: Qiu <99040799+Qiu-IT@users.noreply.github.com> Date: Fri, 24 Mar 2023 12:46:24 +0100 Subject: [PATCH 1/3] feat: add php solution to lc problem: No.0125 (#951) --- .../0100-0199/0125.Valid Palindrome/README.md | 24 +++++++++++++++++++ .../0125.Valid Palindrome/README_EN.md | 24 +++++++++++++++++++ .../0125.Valid Palindrome/Solution.php | 19 +++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 solution/0100-0199/0125.Valid Palindrome/Solution.php diff --git a/solution/0100-0199/0125.Valid Palindrome/README.md b/solution/0100-0199/0125.Valid Palindrome/README.md index 6ad4d22b6a195..f301362baa837 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README.md +++ b/solution/0100-0199/0125.Valid Palindrome/README.md @@ -308,6 +308,30 @@ func verify(ch byte) bool { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Boolean + */ + function isPalindrome($s) { + $regex = "/[a-z0-9]/"; + $s = strtolower($s); + preg_match_all($regex, $s, $matches); + if ($matches[0] == Null) return true; + $len = floor(count($matches[0]) / 2); + for ($i = 0; $i < $len; $i++) { + if ($matches[0][$i] != $matches[0][count($matches[0]) - 1 - $i]) { + return false; + } + } + return true; + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0125.Valid Palindrome/README_EN.md b/solution/0100-0199/0125.Valid Palindrome/README_EN.md index 8c2957cdbf523..8f6a681034ac0 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README_EN.md +++ b/solution/0100-0199/0125.Valid Palindrome/README_EN.md @@ -296,6 +296,30 @@ func verify(ch byte) bool { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $s + * @return Boolean + */ + function isPalindrome($s) { + $regex = "/[a-z0-9]/"; + $s = strtolower($s); + preg_match_all($regex, $s, $matches); + if ($matches[0] == Null) return true; + $len = floor(count($matches[0]) / 2); + for ($i = 0; $i < $len; $i++) { + if ($matches[0][$i] != $matches[0][count($matches[0]) - 1 - $i]) { + return false; + } + } + return true; + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0125.Valid Palindrome/Solution.php b/solution/0100-0199/0125.Valid Palindrome/Solution.php new file mode 100644 index 0000000000000..454272a80e6a9 --- /dev/null +++ b/solution/0100-0199/0125.Valid Palindrome/Solution.php @@ -0,0 +1,19 @@ +class Solution { + /** + * @param String $s + * @return Boolean + */ + function isPalindrome($s) { + $regex = "/[a-z0-9]/"; + $s = strtolower($s); + preg_match_all($regex, $s, $matches); + if ($matches[0] == Null) return true; + $len = floor(count($matches[0]) / 2); + for ($i = 0; $i < $len; $i++) { + if ($matches[0][$i] != $matches[0][count($matches[0]) - 1 - $i]) { + return false; + } + } + return true; + } +} \ No newline at end of file From 4c5a06e2f54046e8076e04616d1e0ea3e6bce7e0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 24 Mar 2023 21:51:14 +0800 Subject: [PATCH 2/3] feat: add solutions to lcp problem: No.67 --- .../README.md" | 135 +++++++++++++++++- .../Solution.cpp" | 31 ++++ .../Solution.go" | 25 ++++ .../Solution.java" | 35 +++++ .../Solution.py" | 19 +++ 5 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.cpp" create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.go" create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.java" create mode 100644 "lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.py" 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 10b27ab20cf7b..d08de8f039162 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" @@ -40,6 +40,21 @@ +**方法一:递归** + +我们设计一个函数 $dfs(root)$,表示将灯饰插入以 $root$ 为根节点的树中,返回插入灯饰后的树的根节点。那么答案就是 $dfs(root)$。 + +函数 $dfs(root)$ 的逻辑如下: + +- 若 $root$ 为空,则返回空; +- 否则,递归地对 $root$ 的左右子树分别调用 $dfs$ 函数,得到插入灯饰后的左右子树的根节点 $l$ 和 $r$; +- 若 $l$ 不为空,则我们创建一个新节点 $TreeNode(-1, l, null)$,并将其作为 $root$ 的左子节点; +- 若 $r$ 不为空,则我们创建一个新节点 $TreeNode(-1, null, r)$,并将其作为 $root$ 的右子节点; + +最后,返回 $root$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树的节点数。 + ### **Python3** @@ -47,7 +62,25 @@ ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + def dfs(root): + if root is None: + return None + l, r = dfs(root.left), dfs(root.right) + if l: + root.left = TreeNode(-1, l) + if r: + root.right = TreeNode(-1, None, r) + return root + + return dfs(root) ``` ### **Java** @@ -55,7 +88,107 @@ ```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode expandBinaryTree(TreeNode root) { + return dfs(root); + } + + private TreeNode dfs(TreeNode root) { + if (root == null) { + return null; + } + TreeNode l = dfs(root.left); + TreeNode r = dfs(root.right); + if (l != null) { + root.left = new TreeNode(-1, l, null); + } + if (r != null) { + root.right = new TreeNode(-1, null, r); + } + return root; + } +} +``` + +### **C++** + +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* expandBinaryTree(TreeNode* root) { + function dfs = [&](TreeNode* root) -> TreeNode* { + if (!root) { + return nullptr; + } + TreeNode* l = dfs(root->left); + TreeNode* r = dfs(root->right); + if (l) { + root->left = new TreeNode(-1, l, nullptr); + } + if (r) { + root->right = new TreeNode(-1, nullptr, r); + } + return root; + }; + return dfs(root); + } +}; +``` +### **Go** + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func expandBinaryTree(root *TreeNode) *TreeNode { + var dfs func(*TreeNode) *TreeNode + dfs = func(root *TreeNode) *TreeNode { + if root == nil { + return root + } + l, r := dfs(root.Left), dfs(root.Right) + if l != nil { + root.Left = &TreeNode{-1, l, nil} + } + if r != nil { + root.Right = &TreeNode{-1, nil, r} + } + return root + } + return dfs(root) +} ``` ### **...** diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.cpp" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.cpp" new file mode 100644 index 0000000000000..2baf71927162a --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.cpp" @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* expandBinaryTree(TreeNode* root) { + function dfs = [&](TreeNode* root) -> TreeNode* { + if (!root) { + return nullptr; + } + TreeNode* l = dfs(root->left); + TreeNode* r = dfs(root->right); + if (l) { + root->left = new TreeNode(-1, l, nullptr); + } + if (r) { + root->right = new TreeNode(-1, nullptr, r); + } + return root; + }; + return dfs(root); + } +}; \ No newline at end of file diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.go" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.go" new file mode 100644 index 0000000000000..7ad5582de900f --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.go" @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func expandBinaryTree(root *TreeNode) *TreeNode { + var dfs func(*TreeNode) *TreeNode + dfs = func(root *TreeNode) *TreeNode { + if root == nil { + return root + } + l, r := dfs(root.Left), dfs(root.Right) + if l != nil { + root.Left = &TreeNode{-1, l, nil} + } + if r != nil { + root.Right = &TreeNode{-1, nil, r} + } + return root + } + return dfs(root) +} \ No newline at end of file diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.java" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.java" new file mode 100644 index 0000000000000..50a7abe9eb0aa --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.java" @@ -0,0 +1,35 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode expandBinaryTree(TreeNode root) { + return dfs(root); + } + + private TreeNode dfs(TreeNode root) { + if (root == null) { + return null; + } + TreeNode l = dfs(root.left); + TreeNode r = dfs(root.right); + if (l != null) { + root.left = new TreeNode(-1, l, null); + } + if (r != null) { + root.right = new TreeNode(-1, null, r); + } + return root; + } +} \ No newline at end of file diff --git "a/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.py" "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.py" new file mode 100644 index 0000000000000..ab7cfb9d66e64 --- /dev/null +++ "b/lcp/LCP 67. \350\243\205\351\245\260\346\240\221/Solution.py" @@ -0,0 +1,19 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + def dfs(root): + if root is None: + return None + l, r = dfs(root.left), dfs(root.right) + if l: + root.left = TreeNode(-1, l) + if r: + root.right = TreeNode(-1, None, r) + return root + + return dfs(root) From d8ec3b69e48c2eb4e0dc50f62df41c82a17a54c4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 24 Mar 2023 22:09:04 +0800 Subject: [PATCH 3/3] feat: add solutions to lcp problem: No.68 --- .../README.md" | 94 ++++++++++++++++++- .../Solution.cpp" | 18 ++++ .../Solution.go" | 25 +++++ .../Solution.java" | 19 ++++ .../Solution.py" | 12 +++ 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 "lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.cpp" create mode 100644 "lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.go" create mode 100644 "lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.java" create mode 100644 "lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.py" diff --git "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/README.md" "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/README.md" index 960cc83033226..461b298a9012b 100644 --- "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/README.md" +++ "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/README.md" @@ -44,6 +44,16 @@ +**方法一:双指针** + +我们用双指针 $j$ 和 $i$ 分别指向当前窗口的左右端点,用数组或哈希表 $d$ 记录当前窗口内的元素以及出现的次数。 + +遍历数组 $flowers$,每一次我们将 $flowers[i]$ 加入到窗口中,即 $d[flowers[i]]++$,然后判断 $d[flowers[i]]$ 是否大于 $cnt$,如果大于 $cnt$,则我们需要将 $flowers[j]$ 从窗口中移除,即 $d[flowers[j]]--$,并将 $j$ 右移,直到 $d[flowers[i]] \leq cnt$。此时窗口内的元素都不超过 $cnt$ 个,因此我们可以将 $i - j + 1$ 加到答案中。 + +最后返回答案即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为数组 $flowers$ 的长度以及数组 $flowers$ 中的最大值。 + ### **Python3** @@ -51,7 +61,18 @@ ```python - +class Solution: + def beautifulBouquet(self, flowers: List[int], cnt: int) -> int: + mod = 10**9 + 7 + d = Counter() + ans = j = 0 + for i, x in enumerate(flowers): + d[x] += 1 + while d[x] > cnt: + d[flowers[j]] -= 1 + j += 1 + ans = (ans + i - j + 1) % mod + return ans ``` ### **Java** @@ -59,7 +80,78 @@ ```java +class Solution { + public int beautifulBouquet(int[] flowers, int cnt) { + int mx = 0; + for (int x : flowers) { + mx = Math.max(mx, x); + } + int[] d = new int[mx + 1]; + long ans = 0; + final int mod = (int) 1e9 + 7; + for (int i = 0, j = 0; i < flowers.length; ++i) { + ++d[flowers[i]]; + while (d[flowers[i]] > cnt) { + --d[flowers[j++]]; + } + ans = (ans + i - j + 1) % mod; + } + return (int) ans; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int beautifulBouquet(vector& flowers, int cnt) { + int mx = *max_element(flowers.begin(), flowers.end()); + int d[mx + 1]; + memset(d, 0, sizeof(d)); + long long ans = 0; + const int mod = 1e9 + 7; + for (int i = 0, j = 0; i < flowers.size(); ++i) { + ++d[flowers[i]]; + while (d[flowers[i]] > cnt) { + --d[flowers[j++]]; + } + ans = (ans + i - j + 1) % mod; + } + return ans; + } +}; +``` +### **Go** + +```go +func beautifulBouquet(flowers []int, cnt int) (ans int) { + mx := 0 + for _, x := range flowers { + mx = max(mx, x) + } + d := make([]int, mx+1) + j := 0 + const mod = 1e9 + 7 + for i, x := range flowers { + d[x]++ + for d[x] > cnt { + d[flowers[j]]-- + j++ + } + ans = (ans + i - j + 1) % mod + } + return +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} ``` ### **...** diff --git "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.cpp" "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.cpp" new file mode 100644 index 0000000000000..15370d152a27b --- /dev/null +++ "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + int beautifulBouquet(vector& flowers, int cnt) { + int mx = *max_element(flowers.begin(), flowers.end()); + int d[mx + 1]; + memset(d, 0, sizeof(d)); + long long ans = 0; + const int mod = 1e9 + 7; + for (int i = 0, j = 0; i < flowers.size(); ++i) { + ++d[flowers[i]]; + while (d[flowers[i]] > cnt) { + --d[flowers[j++]]; + } + ans = (ans + i - j + 1) % mod; + } + return ans; + } +}; \ No newline at end of file diff --git "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.go" "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.go" new file mode 100644 index 0000000000000..9559c0b828d67 --- /dev/null +++ "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.go" @@ -0,0 +1,25 @@ +func beautifulBouquet(flowers []int, cnt int) (ans int) { + mx := 0 + for _, x := range flowers { + mx = max(mx, x) + } + d := make([]int, mx+1) + j := 0 + const mod = 1e9 + 7 + for i, x := range flowers { + d[x]++ + for d[x] > cnt { + d[flowers[j]]-- + j++ + } + ans = (ans + i - j + 1) % mod + } + return +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} \ No newline at end of file diff --git "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.java" "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.java" new file mode 100644 index 0000000000000..cccd592ad7830 --- /dev/null +++ "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.java" @@ -0,0 +1,19 @@ +class Solution { + public int beautifulBouquet(int[] flowers, int cnt) { + int mx = 0; + for (int x : flowers) { + mx = Math.max(mx, x); + } + int[] d = new int[mx + 1]; + long ans = 0; + final int mod = (int) 1e9 + 7; + for (int i = 0, j = 0; i < flowers.length; ++i) { + ++d[flowers[i]]; + while (d[flowers[i]] > cnt) { + --d[flowers[j++]]; + } + ans = (ans + i - j + 1) % mod; + } + return (int) ans; + } +} \ No newline at end of file diff --git "a/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.py" "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.py" new file mode 100644 index 0000000000000..bd2fd1aa525e7 --- /dev/null +++ "b/lcp/LCP 68. \347\276\216\350\247\202\347\232\204\350\212\261\346\235\237/Solution.py" @@ -0,0 +1,12 @@ +class Solution: + def beautifulBouquet(self, flowers: List[int], cnt: int) -> int: + mod = 10**9 + 7 + d = Counter() + ans = j = 0 + for i, x in enumerate(flowers): + d[x] += 1 + while d[x] > cnt: + d[flowers[j]] -= 1 + j += 1 + ans = (ans + i - j + 1) % mod + return ans