Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
- For example, given array S = {-1 2 1 -4}, and target = 1.
- The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
这一题和3Sum解法差不多,而且还更简单,因为不用考虑去重的问题。首先排序nums。 然后只要for loop 一下k, 并考虑[i, j]。 每次碰到一个更接近的,就更新一下res。需要注意的是如果current sum = target, 跳出即可。如果cur sum > target, j --,反之 i++.
- class Solution(object):
- def threeSumClosest(self, nums, target):
- """
- :type nums: List[int]
- :type target: int
- :rtype: int
- """
- n = len(nums)
- nums.sort()
- res = nums[0] + nums[1] + nums[2]
- for i in range(n):
- j = i + 1
- k = n - 1
- while j < k:
- sum = nums[i] + nums[j] + nums[k]
- if abs(sum - target) < abs(res - target):
- res = sum
- if sum == target:
- return sum
- elif sum > target:
- k -= 1
- else:
- j += 1
- return res
Leetcode 16. 3Sum Closest的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- [LeetCode] 16. 3Sum Closest ☆☆☆
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...
- SAP(ABAP) 显示等待图标的FM:SAPGUI_PROGRESS_INDICATOR-SAP进度条
在执行一些数据量大的报表时候,为了防止用户认为是死机,可以再程序中添加正在处理的图标,可以CALL一个 FM来实现. CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' ...
- Sencha, the nightmare!
基础 创建一个应用程序 sencha -sdk /path/to/sdk generate app %name% /path/to/app 跑起来 cd /path/to/app sencha app ...
- ArcGIS Engine开发前基础知识(1)
ArcGIS二次开发是当前gis领域的一项重要必不可少的技能.下面介绍它的基本功能 一.ArcGIS Engine功能 在使用之前首先安装和部署arcgis sdk,(在这里不在赘述相关知识)可以实现 ...
- GO 1.5 代码编译安装 [centos7 64位]
2015年8月,Go 1.5 正式发布,这是 Go 的第六个重要版本. 此版本包括大量重大改进,编译工具链从 C 转换到 Go,从 Go 代码库中完全移除 C 代码.完完全全重新设计了垃圾收集器,减少 ...
- SQLSERVER基础语句(一)
1.插入一行数据:INSERT INTO 表名(列名)VALUES(对应的值);2.一次性插入多条数据先建表:INSERT INTO 新建表名(列表)SELECT 原始表列名 FROM 原始表:执行时 ...
- C# 在Repeater 的ItemDataBound 如何转换e.Item.DataItem 的类型
1.使用DataSet和DataTable绑定数据源时,用 DataRowView view = (DataRowView)e.Item.DataItem; 2.DataReader绑定数据源时,用 ...
- Oracle索引重建
一.前言 Oracle建议对于索引深度超过4级以及已删除的索引条目至少占有现有索引条目总数的20% 这2种情形下需要重建索引.有人持不同观点,就是强烈建议不要定期重建索引.索引重建是一个争论不休被不断 ...
- MySQL 索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的MySQL就是 ...
- linux基本知识2
date:时间管理 linux时钟: 硬件时钟:hwclock -s:硬件时钟到系统时钟 -w:系统时钟到硬件时钟 系统时钟:date 如何查看是外部命令还是内部命令: type COMMAND ...