In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to find a subarray of Java and K". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to find a subarray of K by Java".
Preorder sum
The definition sumi represents the sum added to the position I from the beginning, which we have on the array [1, 2, 3, 4].
Sum [0] = nums [0]
Sum [1] = nums [0] + nums [1] = sum [0] + nums [1]
Sum [2] = nums [0] + nums [1] + nums [2] = sum [0] + nums [1] + nums [2] = sum [1] + nums [2]
Sum [3] = nums [0] + nums [1] + nums [2] + nums [3] = sum [0] + nums [1] + nums [2] + nums [3] =.
We found that we have done a lot of repeated calculations, and these are the places where we can optimize and deform the third formula above.
Sum [1]-sum [0] = nums [1]
Sum [2]-sum [0] = nums [1] + nums [2]
Sum [2]-sum [1] = nums [2]
Sum [I]-sum [j] is the sum of consecutive arrays between sum [I]-sum [j] = k, then we can count, here we can also do a transformation sum [j] = sum [I]-k, we use a hash table to record sum [j], and we count when it occurs again.
Func subarraySum (nums [] int, k int) int {res,sum,m: = 0Pol 0, map [int] int {0:1} for iRu return res} i0 {res + = m [sum-k]} m [sum] +} return res} so far, I believe you have a deeper understanding of "how to find a subarray of Java and a subarray of K", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.