From 935965bb3bc09c1c8b25dfb5308fc8e8fe49e40c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 30 Jul 2025 07:03:25 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2419 No.2419.Longest Subarray With Maximum Bitwise AND --- .../README.md | 45 +++++++++++++++++++ .../README_EN.md | 45 +++++++++++++++++++ .../Solution.cs | 14 ++++++ .../Solution.php | 21 +++++++++ 4 files changed, 125 insertions(+) create mode 100644 solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cs create mode 100644 solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.php diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md index 1de9e92ed8677..f5b167c632c59 100644 --- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README.md @@ -217,6 +217,51 @@ var longestSubarray = function (nums) { }; ``` +#### C# + +```cs +public class Solution { + public int LongestSubarray(int[] nums) { + int mx = nums.Max(); + int ans = 0, cnt = 0; + foreach (int x in nums) { + if (x == mx) { + ans = Math.Max(ans, ++cnt); + } else { + cnt = 0; + } + } + return ans; + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer + */ + function longestSubarray($nums) { + $mx = max($nums); + $ans = 0; + $cnt = 0; + + foreach ($nums as $x) { + if ($x == $mx) { + $ans = max($ans, ++$cnt); + } else { + $cnt = 0; + } + } + + return $ans; + } +} +``` + diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md index c95729b44a910..e6d663d5fd01b 100644 --- a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/README_EN.md @@ -215,6 +215,51 @@ var longestSubarray = function (nums) { }; ``` +#### C# + +```cs +public class Solution { + public int LongestSubarray(int[] nums) { + int mx = nums.Max(); + int ans = 0, cnt = 0; + foreach (int x in nums) { + if (x == mx) { + ans = Math.Max(ans, ++cnt); + } else { + cnt = 0; + } + } + return ans; + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param Integer[] $nums + * @return Integer + */ + function longestSubarray($nums) { + $mx = max($nums); + $ans = 0; + $cnt = 0; + + foreach ($nums as $x) { + if ($x == $mx) { + $ans = max($ans, ++$cnt); + } else { + $cnt = 0; + } + } + + return $ans; + } +} +``` + diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cs b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cs new file mode 100644 index 0000000000000..43ec08525037c --- /dev/null +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.cs @@ -0,0 +1,14 @@ +public class Solution { + public int LongestSubarray(int[] nums) { + int mx = nums.Max(); + int ans = 0, cnt = 0; + foreach (int x in nums) { + if (x == mx) { + ans = Math.Max(ans, ++cnt); + } else { + cnt = 0; + } + } + return ans; + } +} diff --git a/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.php b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.php new file mode 100644 index 0000000000000..390d95d2acbd8 --- /dev/null +++ b/solution/2400-2499/2419.Longest Subarray With Maximum Bitwise AND/Solution.php @@ -0,0 +1,21 @@ +class Solution { + /** + * @param Integer[] $nums + * @return Integer + */ + function longestSubarray($nums) { + $mx = max($nums); + $ans = 0; + $cnt = 0; + + foreach ($nums as $x) { + if ($x == $mx) { + $ans = max($ans, ++$cnt); + } else { + $cnt = 0; + } + } + + return $ans; + } +} \ No newline at end of file