题目英文

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).

题目中文

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

示例 :

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

算法实现

利用暴力算法

public class Solution {public int ThreeSumClosest(int[] nums, int target) {double error = int.MaxValue;int sum = 0;for (int i = 0; i < nums.Length - 2; i++)for (int j = i + 1; j < nums.Length - 1; j++)for (int k = j + 1; k < nums.Length; k++){if (Math.Abs(nums[i] + nums[j] + nums[k] - target) < error){error = Math.Abs(nums[i] + nums[j] + nums[k] - target);sum = nums[i] + nums[j] + nums[k];}}return sum;        }
}

利用双指针算法

public class Solution {public int ThreeSumClosest(int[] nums, int target) {nums = nums.OrderBy(a => a).ToArray();int result = nums[0] + nums[1] + nums[2];for (int i = 0; i < nums.Length - 2; i++){int start = i + 1, end = nums.Length - 1;while (start < end){int sum = nums[start] + nums[end] + nums[i];if (Math.Abs(target - sum) < Math.Abs(target - result))result = sum;if (sum > target)end--;else if (sum < target)start++;elsereturn result;}}return result;        }
}

实验结果

利用暴力算法

  • 状态:通过
  • 125 / 125 个通过测试用例
  • 执行用时: 680 ms, 在所有 C# 提交中击败了 12.07% 的用户
  • 内存消耗: 23.7 MB, 在所有 C# 提交中击败了 7.41% 的用户

利用双指针算法

  • 状态:通过
  • 125 / 125 个通过测试用例
  • 执行用时: 132 ms, 在所有 C# 提交中击败了 100.00% 的用户
  • 内存消耗: 24 MB, 在所有 C# 提交中击败了 5.55% 的用户


相关图文

1. “数组”类算法

  • LeetCode实战:三数之和
  • LeetCode实战:求众数
  • LeetCode实战:缺失的第一个正数
  • LeetCode实战:快乐数
  • LeetCode实战:寻找两个有序数组的中位数
  • LeetCode实战:盛最多水的容器

2. “链表”类算法

  • LeetCode实战:两数相加
  • LeetCode实战:删除链表的倒数第N个节点
  • LeetCode实战:合并两个有序链表
  • LeetCode实战:合并K个排序链表
  • LeetCode实战:两两交换链表中的节点
  • LeetCode实战:旋转链表
  • LeetCode实战:环形链表

3. “栈”类算法

  • LeetCode实战:有效的括号
  • LeetCode实战:最长有效括号
  • LeetCode实战:逆波兰表达式求值

4. “队列”类算法

  • LeetCode实战:设计循环双端队列
  • LeetCode实战:滑动窗口最大值
  • LeetCode实战:整数反转
  • LeetCode实战:字符串转换整数 (atoi)

5. “递归”类算法

  • LeetCode实战:爬楼梯

6. “字符串”类算法

  • LeetCode实战:反转字符串
  • LeetCode实战:翻转字符串里的单词
  • LeetCode实战:最长公共前缀

7. “树”类算法

  • LeetCode实战:相同的树
  • LeetCode实战:对称二叉树
  • LeetCode实战:二叉树的最大深度
  • LeetCode实战:将有序数组转换为二叉搜索树

8. “哈希”类算法

  • LeetCode实战:两数之和

9. “搜索”类算法

  • LeetCode实战:搜索二维矩阵

10. “动态规划”类算法

  • LeetCode实战:最长回文子串

11. “数值分析”类算法

  • LeetCode实战:回文数
  • LeetCode实战:x 的平方根

LeetCode实战:最接近的三数之和相关推荐

  1. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  2. leetcode —— 16. 最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  3. 【leetcode】最接近的三数之和,python实现

    算法思路:跟上提的三数之和为0的题目解题思路一样,但是不同的地方在于它多包含了一个target,所以在计算的时候直接把target减掉,就是计算三数之和和0的距离了,所以是绝对值. 那在这里的迭代算法 ...

  4. leetcode 16.最接近的三数之和

    题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如, ...

  5. LeetCode 16. 最接近的三数之和(固定左端+滑动窗口)

    1. 题目 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. ...

  6. LeetCode 16. 最接近的三数之和 3Sum Closest

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. 3. Leetcode 16. 最接近的三数之和 (数组-双向双指针)

    给你一个长度为 n 的整数数组 nums 和 一个目标值 target.请你从 nums 中选出三个整数,使它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在恰好一个解.示例 1: ...

  8. LeetCode 16最接近的三数之和

    力扣 思路 排序+双指针 枚举第一个数a,对剩下的两个元素b,c,希望它们的和最接近target-a 1.如果它们在原数组中枚举的范围没有任何规律可言,只能用两重循环来枚举所有情况 ->考虑对数 ...

  9. LeetCode 16 最接近的三数之和

    https://leetcode-cn.com/problems/3sum-closest/ 解决方案 class Solution {public int threeSumClosest(int[] ...

  10. Leetcode 16. 最接近的三数之和(3Sum Closest)

    解法一: class Solution { public:int threeSumClosest(vector<int>& nums, int target) {int close ...

最新文章

  1. “chaos”的算法---之哈希表(HASH)算法详解
  2. 如何建立程序代码包的联接?
  3. C#设计模式之14-命令模式
  4. html中渐变怎么写,css3如何实现文字渐变?
  5. charles 抓包软件 安装、破解、使用
  6. WIN7安装启动盘制作并支持usb3.0
  7. 梦雨百度网盘机器人好友群组消息自动回复软件(可用于自动发货场景)
  8. 知乎-知乎网站-复制文字-破解知乎复制
  9. 千古奇才---埃舍尔
  10. Eureka(eureka)服务集群搭建搭建
  11. cdn刷新api_CDN页面刷新接口定义[高升]
  12. C语言 指针 规范,C语言学习知识指针习题集附规范标准答案.doc
  13. PHP服务器获取客户端IP地址
  14. 让你怀疑人生的“良心”软件大集锦,360可能是最“惊喜”!
  15. 这些年我们错过了什么
  16. 河北大学本部导航系统(c语言实现)
  17. 二进制推广者电子计算机,31-戏说计算机与二进制那点事儿
  18. supervisord启动子程序报错Exited too quickly (process log may have details)解决
  19. 五菱的“世界上最有名的颜色”系列海报,太令人上头了
  20. C代码实现从FTP上下载文件

热门文章

  1. 导入sql时出现Invalid default value for ‘create_time‘报错处理方法
  2. 软件测试培训适合什么人学习?
  3. java培训学习阶段步骤讲解
  4. You can't specify target table for update in FROM clause
  5. 【我的Android进阶之旅】解决SDK升级到27.0.3遇到的GLIBC_2.14 not found、no acceptable C compiler found in $PATH等问题...
  6. 光伏电价断崖式下跌 企业遭遇成长烦恼
  7. 安装配置Emacs-rails
  8. 目前流行的源程序版本管理软件和项目管理软件都有哪些?各有什么优缺点?...
  9. C#不错的扩展工具类
  10. Python-常用字符串转换实例