Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Runtime: 72 ms, faster than 56.55% of JavaScript online submissions for Continuous Subarray Sum.
- Memory Usage: 36.5 MB, less than 100.00% of JavaScript online submissions for Continuous Subarray Sum.
- */
- const checkSubarraySum = (nums, k) => {
- if (nums.length < 2) {
- return false
- }
- if (k === 1) {
- return true
- }
- for (let i = 0; i < nums.length; i++) {
- let sum = 0
- for (let j = i; j < nums.length; j++) {
- sum += nums[j]
- if (j - i < 1) {
- continue
- }
- if (i === 0) {
- if (sum === 0 && k === 0 || hasRepeatedZero(nums, i, j)) {
- return true
- }
- }
- if (sum % k === 0) {
- return true
- }
- }
- if (i === 0 && sum < k) {
- return false
- }
- }
- return false
- };
- function hasRepeatedZero(nums, i, j) {
- return nums[j] === 0 && nums[j - 1] === 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement