diff --git a/solution/0200-0299/0263.Ugly Number/README.md b/solution/0200-0299/0263.Ugly Number/README.md index a176580cfc1d2..21d5d2e55c5d8 100644 --- a/solution/0200-0299/0263.Ugly Number/README.md +++ b/solution/0200-0299/0263.Ugly Number/README.md @@ -16,7 +16,7 @@ tags: -
丑数 就是只包含质因数 2
、3
和 5
的正整数。
丑数 就是只包含质因数 2
、3
和 5
的 正 整数。
给你一个整数 n
,请你判断 n
是否为 丑数 。如果是,返回 true
;否则,返回 false
。
输入:n = 1 输出:true -解释:1 没有质因数,因此它的全部质因数是 {2, 3, 5} 的空集。习惯上将其视作第一个丑数。+解释:1 没有质因数。
示例 3:
diff --git a/solution/0600-0699/0661.Image Smoother/README.md b/solution/0600-0699/0661.Image Smoother/README.md index 68355b904f910..8ae44d7e27682 100644 --- a/solution/0600-0699/0661.Image Smoother/README.md +++ b/solution/0600-0699/0661.Image Smoother/README.md @@ -70,7 +70,15 @@ tags: -### 方法一 +### 方法一:直接遍历 + +我们创建一个大小为 $m \times n$ 的二维数组 $\textit{ans}$,其中 $\textit{ans}[i][j]$ 表示图像中第 $i$ 行第 $j$ 列的单元格的平滑值。 + +对于 $\textit{ans}[i][j]$,我们遍历 $\textit{img}$ 中第 $i$ 行第 $j$ 列的单元格及其周围的 $8$ 个单元格,计算它们的和 $s$ 以及个数 $cnt$,然后计算平均值 $s / cnt$ 并将其存入 $\textit{ans}[i][j]$ 中。 + +遍历结束后,我们返回 $\textit{ans}$ 即可。 + +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是 $\textit{img}$ 的行数和列数。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -134,7 +142,9 @@ public: int s = 0, cnt = 0; for (int x = i - 1; x <= i + 1; ++x) { for (int y = j - 1; y <= j + 1; ++y) { - if (x < 0 || x >= m || y < 0 || y >= n) continue; + if (x < 0 || x >= m || y < 0 || y >= n) { + continue; + } ++cnt; s += img[x][y]; } @@ -178,34 +188,23 @@ func imageSmoother(img [][]int) [][]int { function imageSmoother(img: number[][]): number[][] { const m = img.length; const n = img[0].length; - const locations = [ - [-1, -1], - [-1, 0], - [-1, 1], - [0, -1], - [0, 0], - [0, 1], - [1, -1], - [1, 0], - [1, 1], - ]; - - const res = []; - for (let i = 0; i < m; i++) { - res.push([]); - for (let j = 0; j < n; j++) { - let sum = 0; - let count = 0; - for (const [y, x] of locations) { - if ((img[i + y] || [])[j + x] != null) { - sum += img[i + y][j + x]; - count++; + const ans: number[][] = Array.from({ length: m }, () => Array(n).fill(0)); + for (let i = 0; i < m; ++i) { + for (let j = 0; j < n; ++j) { + let s = 0; + let cnt = 0; + for (let x = i - 1; x <= i + 1; ++x) { + for (let y = j - 1; y <= j + 1; ++y) { + if (x >= 0 && x < m && y >= 0 && y < n) { + ++cnt; + s += img[x][y]; + } } } - res[i].push(Math.floor(sum / count)); + ans[i][j] = Math.floor(s / cnt); } } - return res; + return ans; } ``` @@ -216,37 +215,21 @@ impl Solution { pub fn image_smoother(img: VecExplanation:
-We select elements at indices 2 and 8 and 2 * 8
is a perfect square.
We select elements at indices 2 and 8 and 2 * 8
is a perfect square.
Example 2:
diff --git a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md index d635064a6edbe..c6b6774946493 100644 --- a/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md +++ b/solution/3300-3399/3356.Zero Array Transformation II/README_EN.md @@ -22,7 +22,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3356.Ze[li, ri]
in nums
by at most vali
.A Zero Array is an array with all its elements equal to 0.
diff --git a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md index 2b07e8d997670..b10d18385169f 100644 --- a/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md +++ b/solution/3300-3399/3357.Minimize the Maximum Adjacent Element Difference/README_EN.md @@ -17,7 +17,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3357.MiYou are given an array of integers nums
. Some values in nums
are missing and are denoted by -1.
You can choose a pair of positive integers (x, y)
exactly once and replace each missing element with either x
or y
.
You need to minimize the maximum absolute difference between adjacent elements of nums
after replacements.
Table: books
++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| book_id | int | +| title | varchar | +| author | varchar | +| published_year | int | +| rating | decimal | ++----------------+---------+ +book_id is the unique key for this table. +Each row of this table contains information about a book including its unique ID, title, author, publication year, and rating. +rating can be NULL, indicating that the book hasn't been rated yet. ++ +
Write a solution to find all books that have not been rated yet (i.e., have a NULL rating).
+ +Return the result table ordered by book_id
in ascending order.
The result format is in the following example.
+ ++
Example:
+ +Input:
+ +books table:
+ +++---------+------------------------+------------------+----------------+--------+ +| book_id | title | author | published_year | rating | ++---------+------------------------+------------------+----------------+--------+ +| 1 | The Great Gatsby | F. Scott | 1925 | 4.5 | +| 2 | To Kill a Mockingbird | Harper Lee | 1960 | NULL | +| 3 | Pride and Prejudice | Jane Austen | 1813 | 4.8 | +| 4 | The Catcher in the Rye | J.D. Salinger | 1951 | NULL | +| 5 | Animal Farm | George Orwell | 1945 | 4.2 | +| 6 | Lord of the Flies | William Golding | 1954 | NULL | ++---------+------------------------+------------------+----------------+--------+ ++ +
Output:
+ +++---------+------------------------+------------------+----------------+ +| book_id | title | author | published_year | ++---------+------------------------+------------------+----------------+ +| 2 | To Kill a Mockingbird | Harper Lee | 1960 | +| 4 | The Catcher in the Rye | J.D. Salinger | 1951 | +| 6 | Lord of the Flies | William Golding | 1954 | ++---------+------------------------+------------------+----------------+ ++ +
Explanation:
+ +Table: books
++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| book_id | int | +| title | varchar | +| author | varchar | +| published_year | int | +| rating | decimal | ++----------------+---------+ +book_id is the unique key for this table. +Each row of this table contains information about a book including its unique ID, title, author, publication year, and rating. +rating can be NULL, indicating that the book hasn't been rated yet. ++ +
Write a solution to find all books that have not been rated yet (i.e., have a NULL rating).
+ +Return the result table ordered by book_id
in ascending order.
The result format is in the following example.
+ ++
Example:
+ +Input:
+ +books table:
+ +++---------+------------------------+------------------+----------------+--------+ +| book_id | title | author | published_year | rating | ++---------+------------------------+------------------+----------------+--------+ +| 1 | The Great Gatsby | F. Scott | 1925 | 4.5 | +| 2 | To Kill a Mockingbird | Harper Lee | 1960 | NULL | +| 3 | Pride and Prejudice | Jane Austen | 1813 | 4.8 | +| 4 | The Catcher in the Rye | J.D. Salinger | 1951 | NULL | +| 5 | Animal Farm | George Orwell | 1945 | 4.2 | +| 6 | Lord of the Flies | William Golding | 1954 | NULL | ++---------+------------------------+------------------+----------------+--------+ ++ +
Output:
+ +++---------+------------------------+------------------+----------------+ +| book_id | title | author | published_year | ++---------+------------------------+------------------+----------------+ +| 2 | To Kill a Mockingbird | Harper Lee | 1960 | +| 4 | The Catcher in the Rye | J.D. Salinger | 1951 | +| 6 | Lord of the Flies | William Golding | 1954 | ++---------+------------------------+------------------+----------------+ ++ +
Explanation:
+ +