From 2568bee1c25b9704aa8a85470a81547e2193b25e Mon Sep 17 00:00:00 2001 From: emafazillah Date: Wed, 9 Oct 2019 03:40:55 +0800 Subject: [PATCH 1/2] Add solution for 0523.Continuous Subarray Sum --- .../0523.Continuous Subarray Sum/README_EN.md | 24 +++++++++++++++++++ .../Solution.java | 23 ++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 solution/0523.Continuous Subarray Sum/README_EN.md create mode 100644 solution/0523.Continuous Subarray Sum/Solution.java diff --git a/solution/0523.Continuous Subarray Sum/README_EN.md b/solution/0523.Continuous Subarray Sum/README_EN.md new file mode 100644 index 0000000000000..a477317996123 --- /dev/null +++ b/solution/0523.Continuous Subarray Sum/README_EN.md @@ -0,0 +1,24 @@ +# Continuous Subarray Sum + +Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to a multiple of k, that is, sums up to n*k where n is also an integer. + +## Example 1: +``` + Input: [23, 2, 4, 6, 7], k=6 + Output: True + Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6. +``` + +## Example 2: +``` + Input: [23, 2, 6, 4, 7], k=6 + Output: True + Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42. +``` + + + +## Note: + + 1. The length of the array won't exceed 10,000. + 2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer. diff --git a/solution/0523.Continuous Subarray Sum/Solution.java b/solution/0523.Continuous Subarray Sum/Solution.java new file mode 100644 index 0000000000000..838df8a4d3898 --- /dev/null +++ b/solution/0523.Continuous Subarray Sum/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public boolean checkSubarraySum(int[] nums, int k) { + for (int start = 0; start < nums.length; start++) { + int check = 0; + for (int i = start; i < nums.length; i++) { + check += nums[i]; + if (i > start) { + if (k != 0) { + if (check % k == 0) { + return true; + } + } else { + if (check == k) { + return true; + } + } + } + } + } + + return false; + } +} \ No newline at end of file From 27c377514bf81b948ef385b6db400ff61dae3c58 Mon Sep 17 00:00:00 2001 From: emafazillah Date: Wed, 9 Oct 2019 03:41:54 +0800 Subject: [PATCH 2/2] Add README_EN for 0523.Continuous Subarray Sum --- solution/0523.Continuous Subarray Sum/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/0523.Continuous Subarray Sum/README_EN.md b/solution/0523.Continuous Subarray Sum/README_EN.md index a477317996123..45e14fb6c19f4 100644 --- a/solution/0523.Continuous Subarray Sum/README_EN.md +++ b/solution/0523.Continuous Subarray Sum/README_EN.md @@ -18,7 +18,7 @@ Given a list of non-negative numbers and a target integer k, write a function to -## Note: +Note: 1. The length of the array won't exceed 10,000. 2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.