当前位置: 首页 > 编程日记 > 正文

LeetCode实战:最接近的三数之和

题目英文

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 = [-121-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 的平方根

相关文章:

零基础学习UI设计有哪些简单有效的方法

UI设计的普及让越来越多的人对UI有了重新的认识&#xff0c;很多企业对UI设计这个岗位都是非常重视的&#xff0c;如今很多零基础学员都想要转行做UI设计&#xff0c;那么针对零基础学习UI设计有哪些简单有效的方法呢?来看看下面的详细介绍吧。 零基础学习UI设计有哪些简单有效…

(转)linux下oracle instant client安装和运行

1.首先要知道什么是ORACLE的客户端&#xff1a; Oracle Instant client 是oracle提供的简便客户端, 支持多种平台. 可从oracle网站下载, 下载地址为http://www.oracle.com/technology/tech/oci/instantclient/index.html 包括如下内容: Instant client Package - Basic 运行OCI…

LeetCode实战:删除排序数组中的重复项

题目英文 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra me…

16G 手机清理

1.16G 手机清理 清理top 5 的应用的缓存即可 2,hw wife 连接模块 低于 app wifi 的连接模块。 在同样的电脑热点面前&#xff0c;hw 连补上电脑热点&#xff0c;apple 可以连上电脑热点。 其他差异不大。 2.奇兔刷机 tencent应用宝 and 手机管理&#xff0c;备份软件 http://w…

java培训有哪些收费标准

随着学习java技术的人越来越多&#xff0c;市面上的java培训机构也越来越多&#xff0c;每家机构的收费标准都不一样&#xff0c;这让很多想要学习java技术的小伙伴都比较头疼&#xff0c;下面小编就为大家详细的介绍一下java培训有哪些收费标准?如何来评判适合自己的java培训…

使用ASP.NET操作IIS7中使用应用程序

在最新发布的启明星Portal里&#xff0c;增加了安装程序&#xff0c;下面说一下.NET对IIS7操作。IIS7的操作和IIS5/6有很大的不同&#xff0c;在IIS7里增加了 Microsoft.Web.Administration 命名空间里&#xff0c;增加了ServerManager、Site几个大类来操作IIS7。 下面是一些核…

MYSQL体系结构-来自期刊

MYSQL体系结构-来自期刊 MySQL三层体系结构|-----------------------------------------------------------------------------------| | mysqld-SQL层 | |-------------------------------------------…

LeetCode实战:搜索旋转排序数组

题目英文 Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise retur…

Python内置数据结构之双向队列

经常听说Python就是一门执行速度低的语言&#xff0c;可能是你的程序中使用了复杂的算法与数据结构&#xff0c;才会导致程序执行速率低的。在Python的标准库中提供了常见的数据结构工开发者使用&#xff0c;不仅执行速率比较快&#xff0c;还可以简化开发者的编程工作。下面我…

C# GDAL 学习一

最近一直琢磨如何用C#GDAL读取栅格数据&#xff08;.tif或.img&#xff09;&#xff0c;运气不错的在GDAL 的官网上找到一部分源码。经过本人测试&#xff0c;效果还不错。学习还将继续深入下去。 参考网址&#xff1a;http://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csh…

LeetCode实战:字符串相加

题目英文 Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100.Both num1 and num2 contains only digits 0-9.Both num1 and num2 does not contain any leadin…

js中修改this的指向方法整理

JavaScript(简称“JS”) 是一种具有函数优先的轻量级&#xff0c;解释型或即时编译型的编程语言。虽然它是作为开发Web页面的脚本语言而出名&#xff0c;但是它也被用到了很多非浏览器环境中&#xff0c;JavaScript 基于原型编程、多范式的动态脚本语言&#xff0c;并且支持面向…

VC运行时库(/MD、/MT等)

VC项目属性→配置属性→C/C→代码生成→运行时库 可以采用的方式有&#xff1a;多线程(/MT)、多线程调试(/MTd)、多线程DLL(/MD)、多线程调试DLL(/MDd)、单线程(/ML)、单线程调试(/MLd)。 Reusable LibrarySwitchLibraryMacro(s) DefinedSingle Threaded/MLLIBC(none)Static Mu…

函数式编程概述

2019独角兽企业重金招聘Python工程师标准>>> 引子 JDK8中引入了lambda函数式编程的概念&#xff0c;那么什么是函数式编程&#xff0c;函数式编程又有什么好处&#xff0c;今天我们就来说说函数式编程 我们先了解一下函数式编程的由来 一个名叫阿隆佐邱奇的数学家设…

LeetCode实战:字符串相乘

题目英文 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 "2", num2 "3" Output: "6"Example 2: Input: num1 &q…

UI培训教程之系统图标如何设计?

UI设计在近几年广受大家的关注&#xff0c;学习UI设计的人越来也多&#xff0c;今天小编要介绍的就是其中的系统图标设计方法&#xff0c;系统图标在UI设计中是非常基础的图形化语言&#xff0c;也在页面交互中起到很重要的作用。单个图标的设计并不难&#xff0c;但是系统化、…

看懂SQL Server的查询计划(绝对好文!)

在园子看到一篇SQLServer关于查询计划的好文&#xff0c;激动啊,特转载。原文出自:http://www.cnblogs.com/fish-li/archive/2011/06/06/2073626.html看懂SqlServer查询计划对于SqlServer的优化来说&#xff0c;可能优化查询是很常见的事情。关于数据库的优化&#xff0c;本身也…

LeetCode实战:全排列

题目英文 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ]题目中文 给定一个没有重复数字的序列&#xff0c;返回其所有可能的全排列。 示例: 输入: …

git pull出现There is no tracking information for the current branch

使用git pull 或者 git push 的时候报错 gitThere is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details git pull <remote> <branch> If you wish to set tracking information…

零基础快速学习Java技术的方法整理

在学习java技术这条道路上&#xff0c;有很多都是零基础学员&#xff0c;他们对于java的学习有着很多的不解&#xff0c;不知怎么学习也不知道如何下手&#xff0c;其实Java编程涉及到的知识点还是非常多的&#xff0c;我们需要制定java学习路线图这样才能少走弯路&#xff0c;…

转 深入理解Midlet类

在J2ME编程过程中&#xff0c;MIDlet是最核心的类之一&#xff0c;熟悉该类的使用是J2ME学习过程中必须首先掌握的类&#xff0c;下面就结合实际介绍一下该类的实际使用。 众所周知&#xff0c;J2ME程序都是从MIDlet类开始执行&#xff0c;系统规定了MIDlet的生命周期。规定MID…

LeetCode实战:最大子序和

题目英文 Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum 6.Follow up…

PHP如何更好的利用PHPstorm的自动提示

说明 写了一段时间的java之后&#xff0c;特别不习惯PHP本身的弱类型方式&#xff0c;在写代码的时候总觉得不怎么放心&#xff0c;特别本身PHP又是弱类型的语言&#xff0c;所以在编码的时候&#xff0c;很多时候是没有代码提示的。 一个一般例子 class Data {public $name;pu…

2021年Java面试题目最新总结【90%面试会踩的坑】

学会java技术之后大家面临的最多的问题就是面试这关&#xff0c;求职面试java岗位是否能够成功是直接影响我们的工作机会的&#xff0c;所以对于Java程序员面试你准备好了吗?今天小编汇总了一下关于Java程序员面试&#xff0c;90%会踩到的坑。 2021年Java面试题目最新总结【90…

LeetCode实战:螺旋矩阵

题目英文 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [[ 1, 2, 3 ],[ 4, 5, 6 ],[ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5]Example 2: Input: [[1, 2, 3, 4],[5, 6, 7, 8],[9,10,11,…

Json的序列化和反序列化

1、引用命名空间: usingSystem.Runtime.Serialization;2、json的序列化和反序列化的方法&#xff1a; publicclassJsonHelper {///<summary>///序列化///</summary>///<typeparam name"T"></typeparam>///<param name"t">&l…

Angular开山篇

1&#xff1a;环境搭建 今天给大家介绍4种环境搭建的方法。一&#xff1a;Angular-cli的安装 官方指导文档&#xff1a;www.angular.cn/guide/quickstart 请使用cnpm来安装&#xff0c;或者配置淘宝镜像。 使用原生npm安装可能会遇到的问题&#xff1a; 需要python的环境可能会…

零基础参加软件测试培训需要学多长时间

软件测试对于零基础学员来说是非常好入门的&#xff0c;软件测试没有很多的限制&#xff0c;那么零基础参加软件测试培训需要学多长时间呢?来看看下面的详细介绍吧。 零基础参加软件测试培训需要学多长时间?软件测试培训时间一般都在四个月左右&#xff0c;四个月时间的课程内…

Windows API函数大全

1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同一个网络资源的连接 WNetCancelConnection 结束一个网络连接 WNetCancelConnection2 结束一个网络连接 WNetCloseEnum 结束一…

LeetCode实战:螺旋矩阵 II

题目英文 Given a positive integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. Example: Input: 3 Output: [[ 1, 2, 3 ],[ 8, 9, 4 ],[ 7, 6, 5 ] ]题目中文 给定一个正整数 n&#xff0c;生成一个包含 1 到 n^2 所有元素&#x…