diff --git a/.prettierignore b/.prettierignore index 13715bc116db8..47b55e9bdf606 100644 --- a/.prettierignore +++ b/.prettierignore @@ -26,3 +26,4 @@ node_modules/ /solution/3100-3199/3150.Invalid Tweets II/Solution.sql /solution/3100-3199/3198.Find Cities in Each State/Solution.sql /solution/3300-3399/3328.Find Cities in Each State II/Solution.sql +/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql diff --git a/solution/0000-0099/0027.Remove Element/README.md b/solution/0000-0099/0027.Remove Element/README.md index 66512c052c070..e0a104c1eb0d9 100644 --- a/solution/0000-0099/0027.Remove Element/README.md +++ b/solution/0000-0099/0027.Remove Element/README.md @@ -205,6 +205,22 @@ var removeElement = function (nums, val) { }; ``` +#### C# + +```cs +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0027.Remove Element/README_EN.md b/solution/0000-0099/0027.Remove Element/README_EN.md index e033fd507fbbd..6bbc22e167933 100644 --- a/solution/0000-0099/0027.Remove Element/README_EN.md +++ b/solution/0000-0099/0027.Remove Element/README_EN.md @@ -205,6 +205,22 @@ var removeElement = function (nums, val) { }; ``` +#### C# + +```cs +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0027.Remove Element/Solution.cs b/solution/0000-0099/0027.Remove Element/Solution.cs new file mode 100644 index 0000000000000..1ded258b8a3eb --- /dev/null +++ b/solution/0000-0099/0027.Remove Element/Solution.cs @@ -0,0 +1,11 @@ +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} \ No newline at end of file diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md index 201650a5221bf..fdbf53dd7d59c 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md @@ -76,11 +76,11 @@ tags: ### 方法一:数组 + 模拟 -观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $cnt$ 来统计每个编号的各个位数之和的数量。 +观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $\textit{cnt}$ 来统计每个编号的各个位数之和的数量。 -答案就是数组 $cnt$ 中的最大值。 +答案就是数组 $\textit{cnt}$ 中的最大值。 -时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。 +时间复杂度 $O(n \times \log_{10}m)$。其中 $n = \textit{highLimit} - \textit{lowLimit} + 1$,而 $m = \textit{highLimit}$。 @@ -172,6 +172,67 @@ function countBalls(lowLimit: number, highLimit: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; +``` + +#### C# + +```cs +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} +``` + diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md index f76f6e7f9877a..519ee846cc15e 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md @@ -74,11 +74,11 @@ Box 10 has the most number of balls with 2 balls. ### Solution 1: Array + Simulation -Observing the data range of the problem, the maximum number of the ball does not exceed $10^5$, so the maximum value of the sum of each digit of the number is less than $50$. Therefore, we can directly create an array $cnt$ with a length of $50$ to count the number of each digit sum of each number. +Observing the problem's data range, the maximum number of balls does not exceed $10^5$, so the maximum sum of the digits of each number is less than $50$. Therefore, we can directly create an array $\textit{cnt}$ of length $50$ to count the number of occurrences of each digit sum. -The answer is the maximum value in the array $cnt$. +The answer is the maximum value in the array $\textit{cnt}$. -The time complexity is $O(n \times \log_{10}m)$. Here, $n = highLimit - lowLimit + 1$, and $m = highLimit$. +The time complexity is $O(n \times \log_{10}m)$. Here, $n = \textit{highLimit} - \textit{lowLimit} + 1$, and $m = \textit{highLimit}$. @@ -170,6 +170,67 @@ function countBalls(lowLimit: number, highLimit: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; +``` + +#### C# + +```cs +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} +``` + diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs new file mode 100644 index 0000000000000..70de08a91ab94 --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs @@ -0,0 +1,15 @@ +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js new file mode 100644 index 0000000000000..53c0f0670b57e --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js @@ -0,0 +1,16 @@ +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs new file mode 100644 index 0000000000000..8d0cd508c82ac --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +} diff --git a/solution/3400-3499/3451.Find Invalid IP Addresses/README.md b/solution/3400-3499/3451.Find Invalid IP Addresses/README.md new file mode 100644 index 0000000000000..c09c41e732002 --- /dev/null +++ b/solution/3400-3499/3451.Find Invalid IP Addresses/README.md @@ -0,0 +1,162 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md +tags: + - 数据库 +--- + + + +# [3451. Find Invalid IP Addresses](https://leetcode.cn/problems/find-invalid-ip-addresses) + +[English Version](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md) + +## 题目描述 + + + +
Table: logs
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| log_id | int | +| ip | varchar | +| status_code | int | ++-------------+---------+ +log_id is the unique key for this table. +Each row contains server access log information including IP address and HTTP status code. ++ +
Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:
+ +255
in any octet01.02.03.04
)4
octetsReturn the result table ordered by invalid_count
, ip
in descending order respectively.
The result format is in the following example.
+ ++
Example:
+ +Input:
+ +logs table:
+ +++--------+---------------+-------------+ +| log_id | ip | status_code | ++--------+---------------+-------------+ +| 1 | 192.168.1.1 | 200 | +| 2 | 256.1.2.3 | 404 | +| 3 | 192.168.001.1 | 200 | +| 4 | 192.168.1.1 | 200 | +| 5 | 192.168.1 | 500 | +| 6 | 256.1.2.3 | 404 | +| 7 | 192.168.001.1 | 200 | ++--------+---------------+-------------+ ++ +
Output:
+ +++---------------+--------------+ +| ip | invalid_count| ++---------------+--------------+ +| 256.1.2.3 | 2 | +| 192.168.001.1 | 2 | +| 192.168.1 | 1 | ++---------------+--------------+ ++ +
Explanation:
+ +The output table is ordered by invalid_count, ip in descending order respectively.
+Table: logs
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| log_id | int | +| ip | varchar | +| status_code | int | ++-------------+---------+ +log_id is the unique key for this table. +Each row contains server access log information including IP address and HTTP status code. ++ +
Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:
+ +255
in any octet01.02.03.04
)4
octetsReturn the result table ordered by invalid_count
, ip
in descending order respectively.
The result format is in the following example.
+ ++
Example:
+ +Input:
+ +logs table:
+ +++--------+---------------+-------------+ +| log_id | ip | status_code | ++--------+---------------+-------------+ +| 1 | 192.168.1.1 | 200 | +| 2 | 256.1.2.3 | 404 | +| 3 | 192.168.001.1 | 200 | +| 4 | 192.168.1.1 | 200 | +| 5 | 192.168.1 | 500 | +| 6 | 256.1.2.3 | 404 | +| 7 | 192.168.001.1 | 200 | ++--------+---------------+-------------+ ++ +
Output:
+ +++---------------+--------------+ +| ip | invalid_count| ++---------------+--------------+ +| 256.1.2.3 | 2 | +| 192.168.001.1 | 2 | +| 192.168.1 | 1 | ++---------------+--------------+ ++ +
Explanation:
+ +The output table is ordered by invalid_count, ip in descending order respectively.
+