diff --git a/solution/0600-0699/0633.Sum of Square Numbers/README.md b/solution/0600-0699/0633.Sum of Square Numbers/README.md index 22ab2ce96fa46..798a5883a96d2 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/README.md +++ b/solution/0600-0699/0633.Sum of Square Numbers/README.md @@ -37,7 +37,11 @@ ## 解法 -### 方法一 +### 方法一:数学 + 双指针 + +我们可以使用双指针的方法来解决这个问题,定义两个指针 $a$ 和 $b$,分别指向 $0$ 和 $\sqrt{c}$。在每一步中,我们会计算 $s = a^2 + b^2$ 的值,然后比较 $s$ 与 $c$ 的大小。如果 $s = c$,我们就找到了两个整数 $a$ 和 $b$,使得 $a^2 + b^2 = c$。如果 $s < c$,我们将 $a$ 的值增加 $1$,如果 $s > c$,我们将 $b$ 的值减小 $1$。我们不断进行这个过程直到找到答案,或者 $a$ 的值大于 $b$ 的值,返回 `false`。 + +时间复杂度 $O(\sqrt{c})$,其中 $c$ 是给定的非负整数。空间复杂度 $O(1)$。 @@ -80,14 +84,17 @@ class Solution { class Solution { public: bool judgeSquareSum(int c) { - long a = 0, b = (long) sqrt(c); + long long a = 0, b = sqrt(c); while (a <= b) { - long s = a * a + b * b; - if (s == c) return true; - if (s < c) + long long s = a * a + b * b; + if (s == c) { + return true; + } + if (s < c) { ++a; - else + } else { --b; + } } return false; } @@ -114,12 +121,13 @@ func judgeSquareSum(c int) bool { ```ts function judgeSquareSum(c: number): boolean { - let a = 0, - b = Math.floor(Math.sqrt(c)); + let [a, b] = [0, Math.floor(Math.sqrt(c))]; while (a <= b) { - let sum = a ** 2 + b ** 2; - if (sum == c) return true; - if (sum < c) { + const s = a * a + b * b; + if (s === c) { + return true; + } + if (s < c) { ++a; } else { --b; @@ -131,22 +139,22 @@ function judgeSquareSum(c: number): boolean { ```rust use std::cmp::Ordering; + impl Solution { pub fn judge_square_sum(c: i32) -> bool { - let c = c as i64; - let mut left = 0; - let mut right = (c as f64).sqrt() as i64; - while left <= right { - let num = left * left + right * right; - match num.cmp(&c) { + let mut a: i64 = 0; + let mut b: i64 = (c as f64).sqrt() as i64; + while a <= b { + let s = a * a + b * b; + match s.cmp(&(c as i64)) { + Ordering::Equal => { + return true; + } Ordering::Less => { - left += 1; + a += 1; } Ordering::Greater => { - right -= 1; - } - Ordering::Equal => { - return true; + b -= 1; } } } diff --git a/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md b/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md index 20f98b421270c..1e7e2a2d19be8 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md +++ b/solution/0600-0699/0633.Sum of Square Numbers/README_EN.md @@ -33,7 +33,11 @@ ## Solutions -### Solution 1 +### Solution 1: Mathematics + Two Pointers + +We can use the two-pointer method to solve this problem. Define two pointers $a$ and $b$, pointing to $0$ and $\sqrt{c}$ respectively. In each step, we calculate the value of $s = a^2 + b^2$, and then compare the size of $s$ and $c$. If $s = c$, we have found two integers $a$ and $b$ such that $a^2 + b^2 = c$. If $s < c$, we increase the value of $a$ by $1$. If $s > c$, we decrease the value of $b$ by $1$. We continue this process until we find the answer, or the value of $a$ is greater than the value of $b$, and return `false`. + +The time complexity is $O(\sqrt{c})$, where $c$ is the given non-negative integer. The space complexity is $O(1)$. @@ -76,14 +80,17 @@ class Solution { class Solution { public: bool judgeSquareSum(int c) { - long a = 0, b = (long) sqrt(c); + long long a = 0, b = sqrt(c); while (a <= b) { - long s = a * a + b * b; - if (s == c) return true; - if (s < c) + long long s = a * a + b * b; + if (s == c) { + return true; + } + if (s < c) { ++a; - else + } else { --b; + } } return false; } @@ -110,12 +117,13 @@ func judgeSquareSum(c int) bool { ```ts function judgeSquareSum(c: number): boolean { - let a = 0, - b = Math.floor(Math.sqrt(c)); + let [a, b] = [0, Math.floor(Math.sqrt(c))]; while (a <= b) { - let sum = a ** 2 + b ** 2; - if (sum == c) return true; - if (sum < c) { + const s = a * a + b * b; + if (s === c) { + return true; + } + if (s < c) { ++a; } else { --b; @@ -127,22 +135,22 @@ function judgeSquareSum(c: number): boolean { ```rust use std::cmp::Ordering; + impl Solution { pub fn judge_square_sum(c: i32) -> bool { - let c = c as i64; - let mut left = 0; - let mut right = (c as f64).sqrt() as i64; - while left <= right { - let num = left * left + right * right; - match num.cmp(&c) { + let mut a: i64 = 0; + let mut b: i64 = (c as f64).sqrt() as i64; + while a <= b { + let s = a * a + b * b; + match s.cmp(&(c as i64)) { + Ordering::Equal => { + return true; + } Ordering::Less => { - left += 1; + a += 1; } Ordering::Greater => { - right -= 1; - } - Ordering::Equal => { - return true; + b -= 1; } } } diff --git a/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp b/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp index c703d372a5b40..d2bdd929334cc 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp +++ b/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp @@ -1,14 +1,17 @@ class Solution { public: bool judgeSquareSum(int c) { - long a = 0, b = (long) sqrt(c); + long long a = 0, b = sqrt(c); while (a <= b) { - long s = a * a + b * b; - if (s == c) return true; - if (s < c) + long long s = a * a + b * b; + if (s == c) { + return true; + } + if (s < c) { ++a; - else + } else { --b; + } } return false; } diff --git a/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs b/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs index 111c5313663f0..39072b7612aab 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs +++ b/solution/0600-0699/0633.Sum of Square Numbers/Solution.rs @@ -1,20 +1,20 @@ use std::cmp::Ordering; + impl Solution { pub fn judge_square_sum(c: i32) -> bool { - let c = c as i64; - let mut left = 0; - let mut right = (c as f64).sqrt() as i64; - while left <= right { - let num = left * left + right * right; - match num.cmp(&c) { + let mut a: i64 = 0; + let mut b: i64 = (c as f64).sqrt() as i64; + while a <= b { + let s = a * a + b * b; + match s.cmp(&(c as i64)) { + Ordering::Equal => { + return true; + } Ordering::Less => { - left += 1; + a += 1; } Ordering::Greater => { - right -= 1; - } - Ordering::Equal => { - return true; + b -= 1; } } } diff --git a/solution/0600-0699/0633.Sum of Square Numbers/Solution.ts b/solution/0600-0699/0633.Sum of Square Numbers/Solution.ts index fafaf55be07e8..676711693c222 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/Solution.ts +++ b/solution/0600-0699/0633.Sum of Square Numbers/Solution.ts @@ -1,10 +1,11 @@ function judgeSquareSum(c: number): boolean { - let a = 0, - b = Math.floor(Math.sqrt(c)); + let [a, b] = [0, Math.floor(Math.sqrt(c))]; while (a <= b) { - let sum = a ** 2 + b ** 2; - if (sum == c) return true; - if (sum < c) { + const s = a * a + b * b; + if (s === c) { + return true; + } + if (s < c) { ++a; } else { --b; diff --git a/solution/0600-0699/0634.Find the Derangement of An Array/README.md b/solution/0600-0699/0634.Find the Derangement of An Array/README.md index 1f6712fb0db77..6b4ba7e4a775c 100644 --- a/solution/0600-0699/0634.Find the Derangement of An Array/README.md +++ b/solution/0600-0699/0634.Find the Derangement of An Array/README.md @@ -56,9 +56,7 @@ $$ 最终答案即为 $f[n]$。注意答案的取模操作。 -我们发现,状态转移方程中只与 $f[i - 1]$ 和 $f[i - 2]$ 有关,因此我们可以使用两个变量 $a$ 和 $b$ 来分别表示 $f[i - 1]$ 和 $f[i - 2]$,从而将空间复杂度降低到 $O(1)$。 - -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 @@ -116,7 +114,9 @@ func findDerangement(n int) int { -### 方法二 +### 方法二:动态规划(空间优化) + +我们发现,状态转移方程中只与 $f[i - 1]$ 和 $f[i - 2]$ 有关,因此我们可以使用两个变量 $a$ 和 $b$ 来分别表示 $f[i - 1]$ 和 $f[i - 2]$,从而将空间复杂度降低到 $O(1)$。 diff --git a/solution/0600-0699/0634.Find the Derangement of An Array/README_EN.md b/solution/0600-0699/0634.Find the Derangement of An Array/README_EN.md index fb45e287f38fd..947eb31deda77 100644 --- a/solution/0600-0699/0634.Find the Derangement of An Array/README_EN.md +++ b/solution/0600-0699/0634.Find the Derangement of An Array/README_EN.md @@ -35,7 +35,24 @@ ## Solutions -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i]$ as the number of derangement of an array of length $i$. Initially, $f[0] = 1$, $f[1] = 0$. The answer is $f[n]$. + +For an array of length $i$, we consider where to place the number $1$. Suppose it is placed in the $j$-th position, where there are $i-1$ choices. Then, the number $j$ has two choices: + +- Placed in the first position, then the remaining $i - 2$ positions have $f[i - 2]$ derangements, so there are a total of $(i - 1) \times f[i - 2]$ derangements; +- Not placed in the first position, which is equivalent to the derangement of an array of length $i - 1$, so there are a total of $(i - 1) \times f[i - 1]$ derangements. + +In summary, we have the following state transition equation: + +$$ +f[i] = (i - 1) \times (f[i - 1] + f[i - 2]) +$$ + +The final answer is $f[n]$. Note the modulo operation in the answer. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -93,7 +110,9 @@ func findDerangement(n int) int { -### Solution 2 +### Solution 2: Dynamic Programming (Space Optimization) + +We notice that the state transition equation only relates to $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $a$ and $b$ to represent $f[i - 1]$ and $f[i - 2]$ respectively, thereby reducing the space complexity to $O(1)$. diff --git a/solution/0600-0699/0635.Design Log Storage System/README_EN.md b/solution/0600-0699/0635.Design Log Storage System/README_EN.md index 99c6159d1c0e2..14db3be2ec649 100644 --- a/solution/0600-0699/0635.Design Log Storage System/README_EN.md +++ b/solution/0600-0699/0635.Design Log Storage System/README_EN.md @@ -56,7 +56,11 @@ logSystem.retrieve("2016:01:01:01:01:01", "2017:01:01:23:00:00&qu ## Solutions -### Solution 1 +### Solution 1: String Comparison + +Store the `id` and `timestamp` of the logs as tuples in an array. Then in the `retrieve()` method, truncate the corresponding parts of `start` and `end` based on `granularity`, and traverse the array, adding the `id` that meets the conditions to the result array. + +In terms of time complexity, the time complexity of the `put()` method is $O(1)$, and the time complexity of the `retrieve()` method is $O(n)$, where $n$ is the length of the array.