当前位置:网站首页>[record of question brushing] 16. The sum of the nearest three numbers
[record of question brushing] 16. The sum of the nearest three numbers
2022-07-24 20:42:00 【InfoQ】
One 、 Title Description
Input :nums = [-1,2,1,-4], target = 1
Output :2
explain : And target The closest and 2 (-1 + 2 + 1 = 2) .
Input :nums = [0,0,0], target = 1
Output :0
- 3 <= nums.length <= 1000
- -1000 <= nums[i] <= 1000
- -104 <= target <= 104
Two 、 Thought analysis
Sort + Double pointer sum == 0sum == targettarget- according to sum = L + nums[i] + R Result , Judge sum And target target Distance of , Update results if closer res
- Judge sum And target The size relationship of , Because the array is ordered , If sum > target be R--, If sum < target be L++, If sum == target The distance is 0 Direct return
3、 ... and 、 Code implementation
class Solution {
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
// Initialize a res
int res = nums[0] + nums[1] + nums[2];
for (int i = 0; i < nums.length; i++) {
// duplicate removal
if (i > 0 && nums[i] == nums[i - 1]) continue;
int R = i + 1, L = nums.length - 1;
while (R < L) {
int sum = nums[R] + nums[L] + nums[i];
if (Math.abs(target - sum) < Math.abs(target - res))
res = sum;
if (sum > target)
L--;
else if (sum < target)
R++;
else
return res;
}
}
return res;
}
}
Complexity analysis
nLRnRunning results

summary
边栏推荐
- Oracle primary key auto increment setting
- [training Day8] series [matrix multiplication]
- Wechat stores build order pages and automatically grab tickets
- [training Day9] maze [line segment tree]
- Eight transformation qualities that it leaders should possess
- Apache atlas version 2.2 installation
- Two methods of how to export asynchronous data
- Make Huawei router into FTP server (realize upload and download function)
- Delete remote and local branches
- How to learn automated testing? Can you teach yourself?
猜你喜欢

C WinForm actual operation XML code, including the demonstration of creating, saving, querying and deleting forms

How does starknet change the L2 landscape?

whistle ERR_ CERT_ AUTHORITY_ INVALID

PC port occupation release
![[training Day8] interesting number [digital DP]](/img/39/caad2ccff916d5ab0f8c3d93f3901d.png)
[training Day8] interesting number [digital DP]

API data interface of A-share transaction data

Upgrade appium automation framework to the latest 2.0

Let's make a nice monthly temperature map of China with ArcGIS

Connect the smart WiFi remote control in the home assistant

How to use named slots
随机推荐
Lua environment configuration
Inconsistent time
class file has wrong version 55.0, should be 52.0
How to test WebService interface
Mitmproxy tampering with returned data
Wechat stores build order pages and automatically grab tickets
Intel internship mentor layout problem 1
MySQL data recovery
ma.glasnost.orika. MappingException:No converter registered for conversion from Date to LocalDateTime
[training Day6] game [mathematics]
Two methods of how to export asynchronous data
Synthesis route of ALA PNA alanine modified PNA peptide nucleic acid | AC ala PNA
Chrome realizes automated testing: recording and playback web page actions
[feature transformation] feature transformation is to ensure small information loss but high-quality prediction results.
Fluoronisin peptide nucleic acid oligomer complex | regular active group alkyne, SH thiol alkynyl modified peptide nucleic acid
Synthesis of peptide nucleic acid PNA labeled with heptachydrin dye cy7 cy7-pna
Upgrade appium automation framework to the latest 2.0
Opencv learning Day2
95. Puzzling switch
Do you want to enroll in a training class or study by yourself?