2395. Find Subarrays With Equal Sum

2023. 1. 24. 13:40Algorithm/Leetcode, Lintcode, HackerRank, etc.

    목차
반응형

Python

class Solution:
    def findSubarrays(self, nums: List[int]) -> bool:
        n = len(nums)

        if n < 2:
            return False


        for i in range(n - 1):
            tot = nums[i] + nums[i + 1]

            for j in range(i + 1, n - 1):
                if tot == nums[j] + nums[j + 1]:
                    return True

        return False

 

Kotlin

class Solution {
    fun findSubarrays(nums: IntArray): Boolean {
        val n: Int = nums.size
        if (n < 2) {
            return false
        }

        for (i in 0 until n - 1) {
            val tot: Int = nums[i] + nums[i + 1]

            for (j in i + 1 until n - 1) {
                if (tot == nums[j] + nums[j + 1]) {
                    return true
                }
            }
        }

        return false
    }
}

 

C++

class Solution {
public:
    bool findSubarrays(vector<int>& nums) {
        int n = nums.size();
        if (n < 2) {
            return false;
        }

        for (auto i = 0; i < n - 1; ++i) {
            int tot = nums[i] + nums[i + 1];

            for (auto j = i + 1; j < n - 1; ++j) {
                if (tot == nums[j] + nums[j + 1]) {
                    return true;
                }
            }
        }

        return false;
    }
};
반응형