From 80a78b10a380ac24b595b9a7f69ff818f144591f Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Wed, 5 Feb 2025 07:55:03 +0800 Subject: [PATCH 1/3] feat: add solutions to lc problem: No.0090 No.0090.Subsets II --- solution/0000-0099/0090.Subsets II/README.md | 129 +++++++++++++++++- .../0000-0099/0090.Subsets II/README_EN.md | 129 +++++++++++++++++- .../0000-0099/0090.Subsets II/Solution.cpp | 6 +- .../0000-0099/0090.Subsets II/Solution.cs | 26 ++++ .../0000-0099/0090.Subsets II/Solution.go | 4 +- .../0000-0099/0090.Subsets II/Solution.js | 25 ++++ .../0000-0099/0090.Subsets II/Solution2.cpp | 4 +- .../0000-0099/0090.Subsets II/Solution2.cs | 24 ++++ .../0000-0099/0090.Subsets II/Solution2.js | 26 ++++ 9 files changed, 358 insertions(+), 15 deletions(-) create mode 100644 solution/0000-0099/0090.Subsets II/Solution.cs create mode 100644 solution/0000-0099/0090.Subsets II/Solution.js create mode 100644 solution/0000-0099/0090.Subsets II/Solution2.cs create mode 100644 solution/0000-0099/0090.Subsets II/Solution2.js diff --git a/solution/0000-0099/0090.Subsets II/README.md b/solution/0000-0099/0090.Subsets II/README.md index 92f9bc0881acb..a1e8455409916 100644 --- a/solution/0000-0099/0090.Subsets II/README.md +++ b/solution/0000-0099/0090.Subsets II/README.md @@ -133,11 +133,11 @@ class Solution { class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector<vector<int>> ans; vector<int> t; int n = nums.size(); - function<void(int)> dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) { if (i >= n) { ans.push_back(t); return; @@ -160,7 +160,7 @@ public: ```go func subsetsWithDup(nums []int) (ans [][]int) { - sort.Ints(nums) + slices.Sort(nums) n := len(nums) t := []int{} var dfs func(int) @@ -239,6 +239,67 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const t = []; + const ans = []; + const dfs = i => { + if (i >= n) { + ans.push([...t]); + return; + } + t.push(nums[i]); + dfs(i + 1); + t.pop(); + while (i + 1 < n && nums[i] === nums[i + 1]) { + i++; + } + dfs(i + 1); + }; + dfs(0); + return ans; +}; +``` + +#### C# + +```cs +public class Solution { + private IList<IList<int>> ans = new List<IList<int>>(); + private IList<int> t = new List<int>(); + private int[] nums; + + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + this.nums = nums; + Dfs(0); + return ans; + } + + private void Dfs(int i) { + if (i >= nums.Length) { + ans.Add(new List<int>(t)); + return; + } + t.Add(nums[i]); + Dfs(i + 1); + t.RemoveAt(t.Count - 1); + while (i + 1 < nums.Length && nums[i + 1] == nums[i]) { + ++i; + } + Dfs(i + 1); + } +} +``` + <!-- tabs:end --> <!-- solution:end --> @@ -314,7 +375,7 @@ class Solution { class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); int n = nums.size(); vector<vector<int>> ans; for (int mask = 0; mask < 1 << n; ++mask) { @@ -421,6 +482,66 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const ans = []; + for (let mask = 0; mask < 1 << n; ++mask) { + const t = []; + let ok = true; + for (let i = 0; i < n; ++i) { + if (((mask >> i) & 1) === 1) { + if (i && ((mask >> (i - 1)) & 1) === 0 && nums[i] === nums[i - 1]) { + ok = false; + break; + } + t.push(nums[i]); + } + } + if (ok) { + ans.push(t); + } + } + return ans; +}; +``` + +#### C# + +```cs +public class Solution { + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + int n = nums.Length; + IList<IList<int>> ans = new List<IList<int>>(); + for (int mask = 0; mask < 1 << n; ++mask) { + IList<int> t = new List<int>(); + bool ok = true; + for (int i = 0; i < n; ++i) { + if ((mask >> i & 1) == 1) { + if (i > 0 && (mask >> (i - 1) & 1) == 0 && nums[i] == nums[i - 1]) { + ok = false; + break; + } + t.Add(nums[i]); + } + } + if (ok) { + ans.Add(t); + } + } + return ans; + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git a/solution/0000-0099/0090.Subsets II/README_EN.md b/solution/0000-0099/0090.Subsets II/README_EN.md index 23b33b1caaa0d..cf0c5634e19a1 100644 --- a/solution/0000-0099/0090.Subsets II/README_EN.md +++ b/solution/0000-0099/0090.Subsets II/README_EN.md @@ -120,11 +120,11 @@ class Solution { class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector<vector<int>> ans; vector<int> t; int n = nums.size(); - function<void(int)> dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) { if (i >= n) { ans.push_back(t); return; @@ -147,7 +147,7 @@ public: ```go func subsetsWithDup(nums []int) (ans [][]int) { - sort.Ints(nums) + slices.Sort(nums) n := len(nums) t := []int{} var dfs func(int) @@ -226,6 +226,67 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const t = []; + const ans = []; + const dfs = i => { + if (i >= n) { + ans.push([...t]); + return; + } + t.push(nums[i]); + dfs(i + 1); + t.pop(); + while (i + 1 < n && nums[i] === nums[i + 1]) { + i++; + } + dfs(i + 1); + }; + dfs(0); + return ans; +}; +``` + +#### C# + +```cs +public class Solution { + private IList<IList<int>> ans = new List<IList<int>>(); + private IList<int> t = new List<int>(); + private int[] nums; + + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + this.nums = nums; + Dfs(0); + return ans; + } + + private void Dfs(int i) { + if (i >= nums.Length) { + ans.Add(new List<int>(t)); + return; + } + t.Add(nums[i]); + Dfs(i + 1); + t.RemoveAt(t.Count - 1); + while (i + 1 < nums.Length && nums[i + 1] == nums[i]) { + ++i; + } + Dfs(i + 1); + } +} +``` + <!-- tabs:end --> <!-- solution:end --> @@ -301,7 +362,7 @@ class Solution { class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); int n = nums.size(); vector<vector<int>> ans; for (int mask = 0; mask < 1 << n; ++mask) { @@ -408,6 +469,66 @@ impl Solution { } ``` +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const ans = []; + for (let mask = 0; mask < 1 << n; ++mask) { + const t = []; + let ok = true; + for (let i = 0; i < n; ++i) { + if (((mask >> i) & 1) === 1) { + if (i && ((mask >> (i - 1)) & 1) === 0 && nums[i] === nums[i - 1]) { + ok = false; + break; + } + t.push(nums[i]); + } + } + if (ok) { + ans.push(t); + } + } + return ans; +}; +``` + +#### C# + +```cs +public class Solution { + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + int n = nums.Length; + IList<IList<int>> ans = new List<IList<int>>(); + for (int mask = 0; mask < 1 << n; ++mask) { + IList<int> t = new List<int>(); + bool ok = true; + for (int i = 0; i < n; ++i) { + if ((mask >> i & 1) == 1) { + if (i > 0 && (mask >> (i - 1) & 1) == 0 && nums[i] == nums[i - 1]) { + ok = false; + break; + } + t.Add(nums[i]); + } + } + if (ok) { + ans.Add(t); + } + } + return ans; + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git a/solution/0000-0099/0090.Subsets II/Solution.cpp b/solution/0000-0099/0090.Subsets II/Solution.cpp index 49b08baab6a7d..488fdd4c07639 100644 --- a/solution/0000-0099/0090.Subsets II/Solution.cpp +++ b/solution/0000-0099/0090.Subsets II/Solution.cpp @@ -1,11 +1,11 @@ class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); vector<vector<int>> ans; vector<int> t; int n = nums.size(); - function<void(int)> dfs = [&](int i) { + auto dfs = [&](this auto&& dfs, int i) { if (i >= n) { ans.push_back(t); return; @@ -21,4 +21,4 @@ class Solution { dfs(0); return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0090.Subsets II/Solution.cs b/solution/0000-0099/0090.Subsets II/Solution.cs new file mode 100644 index 0000000000000..9446f1701ef36 --- /dev/null +++ b/solution/0000-0099/0090.Subsets II/Solution.cs @@ -0,0 +1,26 @@ +public class Solution { + private IList<IList<int>> ans = new List<IList<int>>(); + private IList<int> t = new List<int>(); + private int[] nums; + + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + this.nums = nums; + Dfs(0); + return ans; + } + + private void Dfs(int i) { + if (i >= nums.Length) { + ans.Add(new List<int>(t)); + return; + } + t.Add(nums[i]); + Dfs(i + 1); + t.RemoveAt(t.Count - 1); + while (i + 1 < nums.Length && nums[i + 1] == nums[i]) { + ++i; + } + Dfs(i + 1); + } +} diff --git a/solution/0000-0099/0090.Subsets II/Solution.go b/solution/0000-0099/0090.Subsets II/Solution.go index 70194ede97453..c716f94b56751 100644 --- a/solution/0000-0099/0090.Subsets II/Solution.go +++ b/solution/0000-0099/0090.Subsets II/Solution.go @@ -1,5 +1,5 @@ func subsetsWithDup(nums []int) (ans [][]int) { - sort.Ints(nums) + slices.Sort(nums) n := len(nums) t := []int{} var dfs func(int) @@ -18,4 +18,4 @@ func subsetsWithDup(nums []int) (ans [][]int) { } dfs(0) return -} \ No newline at end of file +} diff --git a/solution/0000-0099/0090.Subsets II/Solution.js b/solution/0000-0099/0090.Subsets II/Solution.js new file mode 100644 index 0000000000000..e6b6ae15f625e --- /dev/null +++ b/solution/0000-0099/0090.Subsets II/Solution.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const t = []; + const ans = []; + const dfs = i => { + if (i >= n) { + ans.push([...t]); + return; + } + t.push(nums[i]); + dfs(i + 1); + t.pop(); + while (i + 1 < n && nums[i] === nums[i + 1]) { + i++; + } + dfs(i + 1); + }; + dfs(0); + return ans; +}; diff --git a/solution/0000-0099/0090.Subsets II/Solution2.cpp b/solution/0000-0099/0090.Subsets II/Solution2.cpp index 2d5b88450b3e1..397f973163ac5 100644 --- a/solution/0000-0099/0090.Subsets II/Solution2.cpp +++ b/solution/0000-0099/0090.Subsets II/Solution2.cpp @@ -1,7 +1,7 @@ class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) { - sort(nums.begin(), nums.end()); + ranges::sort(nums); int n = nums.size(); vector<vector<int>> ans; for (int mask = 0; mask < 1 << n; ++mask) { @@ -22,4 +22,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/0000-0099/0090.Subsets II/Solution2.cs b/solution/0000-0099/0090.Subsets II/Solution2.cs new file mode 100644 index 0000000000000..52db892cdab0a --- /dev/null +++ b/solution/0000-0099/0090.Subsets II/Solution2.cs @@ -0,0 +1,24 @@ +public class Solution { + public IList<IList<int>> SubsetsWithDup(int[] nums) { + Array.Sort(nums); + int n = nums.Length; + IList<IList<int>> ans = new List<IList<int>>(); + for (int mask = 0; mask < 1 << n; ++mask) { + IList<int> t = new List<int>(); + bool ok = true; + for (int i = 0; i < n; ++i) { + if ((mask >> i & 1) == 1) { + if (i > 0 && (mask >> (i - 1) & 1) == 0 && nums[i] == nums[i - 1]) { + ok = false; + break; + } + t.Add(nums[i]); + } + } + if (ok) { + ans.Add(t); + } + } + return ans; + } +} diff --git a/solution/0000-0099/0090.Subsets II/Solution2.js b/solution/0000-0099/0090.Subsets II/Solution2.js new file mode 100644 index 0000000000000..c9ff21b919f9e --- /dev/null +++ b/solution/0000-0099/0090.Subsets II/Solution2.js @@ -0,0 +1,26 @@ +/** + * @param {number[]} nums + * @return {number[][]} + */ +var subsetsWithDup = function (nums) { + nums.sort((a, b) => a - b); + const n = nums.length; + const ans = []; + for (let mask = 0; mask < 1 << n; ++mask) { + const t = []; + let ok = true; + for (let i = 0; i < n; ++i) { + if (((mask >> i) & 1) === 1) { + if (i && ((mask >> (i - 1)) & 1) === 0 && nums[i] === nums[i - 1]) { + ok = false; + break; + } + t.push(nums[i]); + } + } + if (ok) { + ans.push(t); + } + } + return ans; +}; From e9533157a93f5d3345a5c265bd6f09ae3e809810 Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Wed, 5 Feb 2025 07:58:28 +0800 Subject: [PATCH 2/3] fix: docs --- solution/0000-0099/0090.Subsets II/README.md | 16 +++++++-------- .../0000-0099/0090.Subsets II/README_EN.md | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/solution/0000-0099/0090.Subsets II/README.md b/solution/0000-0099/0090.Subsets II/README.md index a1e8455409916..5830aabf4caea 100644 --- a/solution/0000-0099/0090.Subsets II/README.md +++ b/solution/0000-0099/0090.Subsets II/README.md @@ -59,17 +59,17 @@ tags: ### 方法一:排序 + DFS -我们可以先对数组 $nums$ 进行排序,方便去重。 +我们可以先对数组 $\textit{nums}$ 进行排序,方便去重。 -然后,我们设计一个函数 $dfs(i)$,表示当前从第 $i$ 个元素开始搜索子集。函数 $dfs(i)$ 的执行逻辑如下: +然后,我们设计一个函数 $\textit{dfs}(i)$,表示当前从第 $i$ 个元素开始搜索子集。函数 $\textit{dfs}(i)$ 的执行逻辑如下: 如果 $i \geq n$,说明已经搜索完所有元素,将当前子集加入答案数组中,递归结束。 -如果 $i < n$,将第 $i$ 个元素加入子集,执行 $dfs(i + 1)$,然后将第 $i$ 个元素从子集中移除。接下来,我们判断第 $i$ 个元素是否和下一个元素相同,如果相同,则循环跳过该元素,直到找到第一个和第 $i$ 个元素不同的元素,执行 $dfs(i + 1)$。 +如果 $i < n$,将第 $i$ 个元素加入子集,执行 $\textit{dfs}(i + 1)$,然后将第 $i$ 个元素从子集中移除。接下来,我们判断第 $i$ 个元素是否和下一个元素相同,如果相同,则循环跳过该元素,直到找到第一个和第 $i$ 个元素不同的元素,执行 $\textit{dfs}(i + 1)$。 -最后,我们只需要调用 $dfs(0)$,返回答案数组即可。 +最后,我们只需要调用 $\textit{dfs}(0)$,返回答案数组即可。 -时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 +时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 <!-- tabs:start --> @@ -308,13 +308,13 @@ public class Solution { ### 方法二:排序 + 二进制枚举 -与方法一类似,我们先对数组 $nums$ 进行排序,方便去重。 +与方法一类似,我们先对数组 $\textit{nums}$ 进行排序,方便去重。 -接下来,我们在 $[0, 2^n)$ 的范围内枚举一个二进制数 $mask$,其中 $mask$ 的二进制表示是一个 $n$ 位的位串,如果 $mask$ 的第 $i$ 位为 $1$,表示选择 $nums[i]$,为 $0$ 表示不选择 $nums[i]$。注意,如果 $mask$ 的 $i - 1$ 位为 $0$,且 $nums[i] = nums[i - 1]$,则说明在当前枚举到的方案中,第 $i$ 个元素和第 $i - 1$ 个元素相同,为了避免重复,我们跳过这种情况。否则,我们将 $mask$ 对应的子集加入答案数组中。 +接下来,我们在 $[0, 2^n)$ 的范围内枚举一个二进制数 $\textit{mask}$,其中 $\textit{mask}$ 的二进制表示是一个 $n$ 位的位串,如果 $\textit{mask}$ 的第 $i$ 位为 $1$,表示选择 $\textit{nums}[i]$,为 $0$ 表示不选择 $\textit{nums}[i]$。注意,如果 $\textit{mask}$ 的 $i - 1$ 位为 $0$,且 $\textit{nums}[i] = \textit{nums}[i - 1]$,则说明在当前枚举到的方案中,第 $i$ 个元素和第 $i - 1$ 个元素相同,为了避免重复,我们跳过这种情况。否则,我们将 $\textit{mask}$ 对应的子集加入答案数组中。 枚举结束后,我们返回答案数组即可。 -时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 +时间复杂度 $O(n \times 2^n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 <!-- tabs:start --> diff --git a/solution/0000-0099/0090.Subsets II/README_EN.md b/solution/0000-0099/0090.Subsets II/README_EN.md index cf0c5634e19a1..f23aca40907b0 100644 --- a/solution/0000-0099/0090.Subsets II/README_EN.md +++ b/solution/0000-0099/0090.Subsets II/README_EN.md @@ -46,17 +46,17 @@ tags: ### Solution 1: Sorting + DFS -We can first sort the array $nums$ to facilitate deduplication. +We can first sort the array $\textit{nums}$ to facilitate deduplication. -Then, we design a function $dfs(i)$, which represents searching for subsets starting from the $i$-th element. The execution logic of the function $dfs(i)$ is as follows: +Then, we design a function $\textit{dfs}(i)$, which represents the current search for subsets starting from the $i$-th element. The execution logic of the function $\textit{dfs}(i)$ is as follows: -If $i \geq n$, it means that all elements have been searched, and the current subset is added to the answer array, and the recursion ends. +If $i \geq n$, it means all elements have been searched, add the current subset to the answer array, and end the recursion. -If $i < n$, add the $i$-th element to the subset, execute $dfs(i + 1)$, and then remove the $i$-th element from the subset. Next, we judge whether the $i$-th element is the same as the next element. If it is the same, we loop to skip this element until we find the first element that is different from the $i$-th element, and execute $dfs(i + 1)$. +If $i < n$, add the $i$-th element to the subset, execute $\textit{dfs}(i + 1)$, then remove the $i$-th element from the subset. Next, we check if the $i$-th element is the same as the next element. If they are the same, skip the element in a loop until we find the first element different from the $i$-th element, then execute $\textit{dfs}(i + 1)$. -Finally, we only need to call $dfs(0)$ and return the answer array. +Finally, we only need to call $\textit{dfs}(0)$ and return the answer array. -The time complexity is $O(n \times 2^n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. +The time complexity is $O(n \times 2^n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. <!-- tabs:start --> @@ -295,13 +295,13 @@ public class Solution { ### Solution 2: Sorting + Binary Enumeration -Similar to Solution 1, we first sort the array $nums$ to facilitate deduplication. +Similar to Solution 1, we first sort the array $\textit{nums}$ to facilitate deduplication. -Next, we enumerate a binary number $mask$ in the range of $[0, 2^n)$, where the binary representation of $mask$ is an $n$-bit bit string. If the $i$-th bit of $mask$ is $1$, it means to select $nums[i]$, and $0$ means not to select $nums[i]$. Note that if the $i - 1$ bit of $mask$ is $0$, and $nums[i] = nums[i - 1]$, it means that in the current enumerated scheme, the $i$-th element and the $i - 1$-th element are the same. To avoid repetition, we skip this situation. Otherwise, we add the subset corresponding to $mask$ to the answer array. +Next, we enumerate a binary number $\textit{mask}$ in the range $[0, 2^n)$, where the binary representation of $\textit{mask}$ is an $n$-bit bit string. If the $i$-th bit of $\textit{mask}$ is $1$, it means selecting $\textit{nums}[i]$, and $0$ means not selecting $\textit{nums}[i]$. Note that if the $(i - 1)$-th bit of $\textit{mask}$ is $0$ and $\textit{nums}[i] = \textit{nums}[i - 1]$, it means that the $i$-th element is the same as the $(i - 1)$-th element in the current enumeration scheme. To avoid duplication, we skip this case. Otherwise, we add the subset corresponding to $\textit{mask}$ to the answer array. -After the enumeration ends, we return the answer array. +After the enumeration, we return the answer array. -The time complexity is $O(n \times 2^n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. +The time complexity is $O(n \times 2^n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. <!-- tabs:start --> From 801a7e8b1944ee188b215b35123865adee1c8c86 Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Wed, 5 Feb 2025 08:06:32 +0800 Subject: [PATCH 3/3] fix: update solutions --- .../0009.Palindrome Number/README.md | 34 +++++++++---------- .../0009.Palindrome Number/README_EN.md | 34 +++++++++---------- .../0009.Palindrome Number/Solution.cs | 12 +++---- .../0009.Palindrome Number/Solution.php | 17 ++++++---- .../0009.Palindrome Number/Solution.rs | 21 ++++-------- 5 files changed, 56 insertions(+), 62 deletions(-) diff --git a/solution/0000-0099/0009.Palindrome Number/README.md b/solution/0000-0099/0009.Palindrome Number/README.md index ee1c738dee14d..5293dccce090b 100644 --- a/solution/0000-0099/0009.Palindrome Number/README.md +++ b/solution/0000-0099/0009.Palindrome Number/README.md @@ -177,13 +177,12 @@ function isPalindrome(x: number): boolean { ```rust impl Solution { pub fn is_palindrome(mut x: i32) -> bool { - if x < 0 || (x % 10 == 0 && x != 0) { + if x < 0 || (x != 0 && x % 10 == 0) { return false; } let mut y = 0; while x > y { - y *= 10; - y += x % 10; + y = y * 10 + x % 10; x /= 10; } x == y || x == y / 10 @@ -213,17 +212,13 @@ var isPalindrome = function (x) { #### C# ```cs -public class Solution -{ - public bool IsPalindrome(int x) - { - if (x < 0 || (x > 0 && x % 10 == 0)) - { +public class Solution { + public bool IsPalindrome(int x) { + if (x < 0 || (x > 0 && x % 10 == 0)) { return false; } int y = 0; - for (; y < x; x /= 10) - { + for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; @@ -236,14 +231,19 @@ public class Solution ```php class Solution { /** - * @param int $x - * @return boolean + * @param Integer $x + * @return Boolean */ - function isPalindrome($x) { - $str = (string) $x; - $str_reverse = strrev($str); - return $str === $str_reverse; + if ($x < 0 || ($x && $x % 10 == 0)) { + return false; + } + $y = 0; + while ($x > $y) { + $y = $y * 10 + ($x % 10); + $x = (int) ($x / 10); + } + return $x == $y || $x == (int) ($y / 10); } } ``` diff --git a/solution/0000-0099/0009.Palindrome Number/README_EN.md b/solution/0000-0099/0009.Palindrome Number/README_EN.md index 5616eee00755d..8b0ac2e383ca0 100644 --- a/solution/0000-0099/0009.Palindrome Number/README_EN.md +++ b/solution/0000-0099/0009.Palindrome Number/README_EN.md @@ -169,13 +169,12 @@ function isPalindrome(x: number): boolean { ```rust impl Solution { pub fn is_palindrome(mut x: i32) -> bool { - if x < 0 || (x % 10 == 0 && x != 0) { + if x < 0 || (x != 0 && x % 10 == 0) { return false; } let mut y = 0; while x > y { - y *= 10; - y += x % 10; + y = y * 10 + x % 10; x /= 10; } x == y || x == y / 10 @@ -205,17 +204,13 @@ var isPalindrome = function (x) { #### C# ```cs -public class Solution -{ - public bool IsPalindrome(int x) - { - if (x < 0 || (x > 0 && x % 10 == 0)) - { +public class Solution { + public bool IsPalindrome(int x) { + if (x < 0 || (x > 0 && x % 10 == 0)) { return false; } int y = 0; - for (; y < x; x /= 10) - { + for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; @@ -228,14 +223,19 @@ public class Solution ```php class Solution { /** - * @param int $x - * @return boolean + * @param Integer $x + * @return Boolean */ - function isPalindrome($x) { - $str = (string) $x; - $str_reverse = strrev($str); - return $str === $str_reverse; + if ($x < 0 || ($x && $x % 10 == 0)) { + return false; + } + $y = 0; + while ($x > $y) { + $y = $y * 10 + ($x % 10); + $x = (int) ($x / 10); + } + return $x == $y || $x == (int) ($y / 10); } } ``` diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.cs b/solution/0000-0099/0009.Palindrome Number/Solution.cs index b58946514c161..f530d9156371b 100644 --- a/solution/0000-0099/0009.Palindrome Number/Solution.cs +++ b/solution/0000-0099/0009.Palindrome Number/Solution.cs @@ -1,14 +1,10 @@ -public class Solution -{ - public bool IsPalindrome(int x) - { - if (x < 0 || (x > 0 && x % 10 == 0)) - { +public class Solution { + public bool IsPalindrome(int x) { + if (x < 0 || (x > 0 && x % 10 == 0)) { return false; } int y = 0; - for (; y < x; x /= 10) - { + for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.php b/solution/0000-0099/0009.Palindrome Number/Solution.php index 9fd8c6654ec95..c4042d56fbed1 100644 --- a/solution/0000-0099/0009.Palindrome Number/Solution.php +++ b/solution/0000-0099/0009.Palindrome Number/Solution.php @@ -1,12 +1,17 @@ class Solution { /** - * @param int $x - * @return boolean + * @param Integer $x + * @return Boolean */ - function isPalindrome($x) { - $str = (string) $x; - $str_reverse = strrev($str); - return $str === $str_reverse; + if ($x < 0 || ($x && $x % 10 == 0)) { + return false; + } + $y = 0; + while ($x > $y) { + $y = $y * 10 + ($x % 10); + $x = (int) ($x / 10); + } + return $x == $y || $x == (int) ($y / 10); } } diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.rs b/solution/0000-0099/0009.Palindrome Number/Solution.rs index 7163690151d08..9275d20a011d7 100644 --- a/solution/0000-0099/0009.Palindrome Number/Solution.rs +++ b/solution/0000-0099/0009.Palindrome Number/Solution.rs @@ -1,20 +1,13 @@ impl Solution { - pub fn is_palindrome(x: i32) -> bool { - if x < 0 { + pub fn is_palindrome(mut x: i32) -> bool { + if x < 0 || (x != 0 && x % 10 == 0) { return false; } - let s = x.to_string(); - let bs = s.as_bytes(); - let n = bs.len(); - let mut l = 0; - let mut r = n - 1; - while l < r { - if bs[l] != bs[r] { - return false; - } - l += 1; - r -= 1; + let mut y = 0; + while x > y { + y = y * 10 + x % 10; + x /= 10; } - true + x == y || x == y / 10 } }