diff --git a/solution/0200-0299/0219.Contains Duplicate II/README.md b/solution/0200-0299/0219.Contains Duplicate II/README.md index 1a063ad6936ff..f830bcd9d14e3 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/README.md +++ b/solution/0200-0299/0219.Contains Duplicate II/README.md @@ -155,6 +155,32 @@ function containsNearbyDuplicate(nums: number[], k: number): boolean { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @param Integer $k + * @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] + ) { + return true; + } + $hashtable[$tmp] = $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 4c9e2df08e889..8fd4e03e2bc33 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/README_EN.md +++ b/solution/0200-0299/0219.Contains Duplicate II/README_EN.md @@ -146,6 +146,32 @@ function containsNearbyDuplicate(nums: number[], k: number): boolean { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $nums + * @param Integer $k + * @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] + ) { + return true; + } + $hashtable[$tmp] = $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 new file mode 100644 index 0000000000000..4f85410647f5b --- /dev/null +++ b/solution/0200-0299/0219.Contains Duplicate II/Solution.php @@ -0,0 +1,21 @@ +class Solution { + /** + * @param Integer[] $nums + * @param Integer $k + * @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] + ) { + return true; + } + $hashtable[$tmp] = $i; + } + return false; + } +}

AltStyle によって変換されたページ (->オリジナル) /