In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve the sum of three IOS, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1. A brief introduction to the sum of three numbers
For an array of integers, whether there is a, b, c such that a + b + c = 0, return the a b c array, the same array returns only one,:
For example:
[- 1,-2, 6, 5, 0, 1, 2,-1] returns [- 1, 0, 1], [- 2, 0, 2], [- 1,-1, 2]]
Key points:
① found three numbers with a sum of 0
② removes the same item. For example, the array above is actually [- 1,0,1]. There are three groups, but we only need to add one group.
Never use for loop to deal with this problem with another layer of for loop. Some think that two-layer for loop solution can, ah, one layer looks for A, and the second layer looks for B, to judge whether there is C =-(A + B) in the array. The idea is correct, but this kind of time complexity is very high O (n ^ 2) and when you get started, you will find that the problem of deduplication is more complicated to deal with.
The method is as follows:
Array nums first positive permutation
Then for loop, set the minimum subscript low = I + 1, the maximum subscript high = nums.count-1
Maximum value, minimum value keep shrinking search, repeatedly remove and always maintain low
< high (因为是正序排列 大值 >= small)
Make 0-nums [I] = nums [low] + nums [high] (that is, 0 = nums [low] + nums [high] + nums [I])
Create a new array and add qualified [nums [low], nums [high], nums [I]], and return at the end of the loop
Let's take a look at the code.
2. Code let num: [Int] = [0,0,0,0,-1,0,1,9,7,4] print ("return result:\ (self.caculate (nums: num)") func caculate (nums: [Int])-> [Int]] {/ / Array elements less than 2 directly return if nums.count
< 2 { return [] } //创建空数组, 用来添加 [A,B,C] var result:[[Int]] = [] //将原数组数组正序排列, 这一步很重要, 乱序数组很难处理 let sort:[Int] = nums.sorted() //循环正序数组 for i in 0..0 && sort[i] == sort[i-1] { continue } //始终保证 最大值下标 >The idea of minimum subscript / / is that the maximum value does not decrease, the minimum value continues to increase, and the minimum value will not exceed the maximum value / / until the corresponding value is found. The same value deduplicates while low < high {/ / create sum is: two digits and AbeliB let sum:Int = sort [low] + sort [high] / / if Aquib = =-C, that is, A+B+C = = 0 if sum = = target {/ / Array add new elements Result.append ([sort [low]] Sort [high], sort [I]]) / / if the current minimum value is equal to the next bit, the subscript is shifted forward, and the while low < high & & sort [low] = = sort [low + 1] {low + = 1} / / if the current maximum value is equal to the previous bit Move the subscript forward, remove the weight while low < high & & sort [high] = = sort [high-1] {high-= 1} / / the minimum value moves backward one bit, and the maximum value moves forward one bit and continues to contract. Until while low + = 1 high-= 1} else if sum < target {/ / if A+B+C B = =-C, that is, A+B+C = = 0 low + = 1} else {/ / if Atom B = =-C means A+B+C = = 0 high-= 1} return result}
Return result: [[0,1,-1], [0,0,0]]
The above is all the contents of the article "how to achieve the sum of three numbers in IOS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.