Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ achieve the sum of the last three?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article introduces in detail "how C++ achieves the sum of the recent three numbers". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to achieve the sum of the recent three numbers by C++" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, together to learn new knowledge.

The sum of the last three numbers of 3Sum Closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [- 1,2,1,-4], and target = 1.

The sum that is closest to the target is 2. (- 1 + 2 + 1 = 2)

This question allows us to find the sum of the three numbers closest to the given value, which adds a little more difficulty on the basis of the previous 3Sum, so this problem allows us to return the value closest to the given value, that is, to ensure that the absolute value of the difference between the current three numbers and the given value is minimum, so we need to define a variable diff to record the absolute value of the difference, and then we still have to sort the array first, and then start traversing the array. The idea is very similar to the sum of the three numbers, which is to determine a number first, and then use two pointers left and right to slide to find the other two numbers. Every time two numbers are determined, the sum of the three numbers is obtained, and then the absolute value of the difference between the calculation and the given value is stored in newDiff. Then compare with diff and update the diff and the result closest. The code is as follows:

Solution 1:

Class Solution {public: int threeSumClosest (vector& nums, int target) {int closest = nums [0] + nums [1] + nums [2]; int diff = abs (closest-target); sort (nums.begin (), nums.end ()); for (int I = 0; I

< nums.size() - 2; ++i) { int left = i + 1, right = nums.size() - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; int newDiff = abs(sum - target); if (diff >

NewDiff) {diff = newDiff; closest = sum;} if (sum

< target) ++left; else --right; } } return closest; }}; 我们还可以稍稍进行一下优化,每次判断一下,当 nums[i]*3 >

When target, you can directly compare the values of closest and nums [I] + nums [iTune1] + nums [iTune2], and return the smaller one, because the array is already sorted, and the following numbers will only get larger and larger, so you don't have to compare them later. See the code below:

Solution 2:

Class Solution {public: int threeSumClosest (vector& nums, int target) {int closest = nums [0] + nums [1] + nums [2]; int diff = abs (closest-target); sort (nums.begin (), nums.end ()); for (int I = 0; I

< nums.size() - 2; ++i) { if (nums[i] * 3 >

Target) return min (closest, nums [I] + nums [I + 1] + nums [I + 2]); int left = I + 1, right = nums.size ()-1; while (left

< right) { int sum = nums[i] + nums[left] + nums[right]; int newDiff = abs(sum - target); if (diff >

NewDiff) {diff = newDiff; closest = sum;} if (sum < target) + + left; else-- right;}} return closest;}} After reading this, the article "how C++ can achieve the sum of the last three numbers" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about the article, welcome to 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report