返回介绍

solution / 2300-2399 / 2395.Find Subarrays With Equal Sum / README_EN

发布于 2024-06-17 01:03:06 字数 4105 浏览 0 评论 0 收藏 0

2395. Find Subarrays With Equal Sum

中文文档

Description

Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices.

Return true_ if these subarrays exist, and _false_ otherwise._

A subarray is a contiguous non-empty sequence of elements within an array.

 

Example 1:

Input: nums = [4,2,4]
Output: true
Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.

Example 2:

Input: nums = [1,2,3,4,5]
Output: false
Explanation: No two subarrays of size 2 have the same sum.

Example 3:

Input: nums = [0,0,0]
Output: true
Explanation: The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0. 
Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.

 

Constraints:

  • 2 <= nums.length <= 1000
  • -109 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def findSubarrays(self, nums: List[int]) -> bool:
    vis = set()
    for a, b in pairwise(nums):
      if (x := a + b) in vis:
        return True
      vis.add(x)
    return False
class Solution {
  public boolean findSubarrays(int[] nums) {
    Set<Integer> vis = new HashSet<>();
    for (int i = 1; i < nums.length; ++i) {
      if (!vis.add(nums[i - 1] + nums[i])) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  bool findSubarrays(vector<int>& nums) {
    unordered_set<int> vis;
    for (int i = 1; i < nums.size(); ++i) {
      int x = nums[i - 1] + nums[i];
      if (vis.count(x)) {
        return true;
      }
      vis.insert(x);
    }
    return false;
  }
};
func findSubarrays(nums []int) bool {
  vis := map[int]bool{}
  for i, b := range nums[1:] {
    x := nums[i] + b
    if vis[x] {
      return true
    }
    vis[x] = true
  }
  return false
}
function findSubarrays(nums: number[]): boolean {
  const vis: Set<number> = new Set<number>();
  for (let i = 1; i < nums.length; ++i) {
    const x = nums[i - 1] + nums[i];
    if (vis.has(x)) {
      return true;
    }
    vis.add(x);
  }
  return false;
}
use std::collections::HashSet;
impl Solution {
  pub fn find_subarrays(nums: Vec<i32>) -> bool {
    let n = nums.len();
    let mut set = HashSet::new();
    for i in 1..n {
      if !set.insert(nums[i - 1] + nums[i]) {
        return true;
      }
    }
    false
  }
}
bool findSubarrays(int* nums, int numsSize) {
  for (int i = 1; i < numsSize - 1; i++) {
    for (int j = i + 1; j < numsSize; j++) {
      if (nums[i - 1] + nums[i] == nums[j - 1] + nums[j]) {
        return true;
      }
    }
  }
  return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文