diff --git a/solution/0200-0299/0219.Contains Duplicate II/README.md b/solution/0200-0299/0219.Contains Duplicate II/README.md index 3d54404ef3563..ea2123a30a5fd 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/README.md +++ b/solution/0200-0299/0219.Contains Duplicate II/README.md @@ -60,13 +60,13 @@ tags: ### 方法一:哈希表 -我们用哈希表 $d$ 存放最近遍历到的数以及对应的下标。 +我们用一个哈希表 $\textit{d}$ 存放最近遍历到的数以及对应的下标。 -遍历数组 `nums`,对于当前遍历到的元素 $nums[i]$,如果在哈希表中存在,并且下标与当前元素的下标之差不超过 $k$,则返回 `true`,否则将当前元素加入哈希表中。 +遍历数组 $\textit{nums}$,对于当前遍历到的元素 $\textit{nums}[i]$,如果在哈希表中存在,并且下标与当前元素的下标之差不超过 $k$,则返回 $\text{true}$,否则将当前元素加入哈希表中。 -遍历结束后,返回 `false`。 +遍历结束后,返回 $\text{false}$。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -195,13 +195,12 @@ class Solution { * @return Boolean */ function containsNearbyDuplicate($nums, $k) { - $hashtable = []; - for ($i = 0; $i < count($nums); $i++) { - $tmp = $nums[$i]; - if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) { + $d = []; + for ($i = 0; $i < count($nums); ++$i) { + if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) { return true; } - $hashtable[$tmp] = $i; + $d[$nums[$i]] = $i; } return false; } diff --git a/solution/0200-0299/0219.Contains Duplicate II/README_EN.md b/solution/0200-0299/0219.Contains Duplicate II/README_EN.md index ec2180534edb0..afe44aff34c8b 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/README_EN.md +++ b/solution/0200-0299/0219.Contains Duplicate II/README_EN.md @@ -59,13 +59,13 @@ tags: ### Solution 1: Hash Table -We use a hash table $d$ to store the nearest index of the number it has visited. +We use a hash table $\textit{d}$ to store the recently traversed numbers and their corresponding indices. -We traverse the array `nums`. For the current element $nums[i]$, if it exists in the hash table, and the difference between its index and the current index is no larger than $k$, then return `true`. Otherwise, we add the current element into the hash table. +Traverse the array $\textit{nums}$. For the current element $\textit{nums}[i]$, if it exists in the hash table and the difference between the indices is no more than $k$, return $\text{true}$. Otherwise, add the current element to the hash table. -After the traversal, return `false`. +After traversing, return $\text{false}$. -The time complexity is $O(n)$ and the space complexity is $O(n)$. Here $n$ is the length of array `nums`. +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. @@ -194,13 +194,12 @@ class Solution { * @return Boolean */ function containsNearbyDuplicate($nums, $k) { - $hashtable = []; - for ($i = 0; $i < count($nums); $i++) { - $tmp = $nums[$i]; - if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) { + $d = []; + for ($i = 0; $i < count($nums); ++$i) { + if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) { return true; } - $hashtable[$tmp] = $i; + $d[$nums[$i]] = $i; } return false; } diff --git a/solution/0200-0299/0219.Contains Duplicate II/Solution.php b/solution/0200-0299/0219.Contains Duplicate II/Solution.php index c45e959b0bf19..92f556b03ac96 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/Solution.php +++ b/solution/0200-0299/0219.Contains Duplicate II/Solution.php @@ -5,14 +5,13 @@ class Solution { * @return Boolean */ function containsNearbyDuplicate($nums, $k) { - $hashtable = []; - for ($i = 0; $i < count($nums); $i++) { - $tmp = $nums[$i]; - if (array_key_exists($tmp, $hashtable) && $k >= $i - $hashtable[$tmp]) { + $d = []; + for ($i = 0; $i < count($nums); ++$i) { + if (array_key_exists($nums[$i], $d) && $i - $d[$nums[$i]] <= $k) { return true; } - $hashtable[$tmp] = $i; + $d[$nums[$i]] = $i; } return false; } -} +} \ No newline at end of file