leetcode16 3-Sum
给定数组a[](长度不小于3)和一个数字target,要求从a中选取3个数字,让它们的和尽量接近target。
解法:首先对数组a进行排序,其次枚举最外面两层指针,对于第三个指针肯定是从右往左(由大变小)慢慢单向滑动的。
class Solution(object):
def __init__(self):
self.ans=0xffffffffff
def threeSumClosest(self, nums, target):
nums=sorted(nums)
def update(i,j,k):#更新答案
now_ans=nums[i]+nums[j]+nums[k]
if abs(self.ans-target)>abs(now_ans-target):
self.ans=now_ans
for i in range(len(nums)-2):
k=len(nums)-1
for j in range(i+1,len(nums)-1):
while k>j and nums[i]+nums[j]+nums[k]>target:
k-=1
if k<j:
update(i,j,j+1)
break
if k>j and k<len(nums):
update(i,j,k)
if k>=j and k+1<len(nums):
update(i,j,k+1)
return self.ans
leetcode16 3-Sum的更多相关文章
- leetcode16—3 Sum Closet
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- Xcode界面切换动画效果
CATransition *animation = [CATransition animation]; [animation setDuration:0.2f]; [animation setTimi ...
- oracle 日期相减 转载
转自 http://hi.baidu.com/juanjuan_66/blog/item/cf48554c9331fbe6d62afc6a.html oracle日期相减2012-02-10 12 ...
- leetcode笔记:Same Tree
一. 题目描写叙述 Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- C 语言:返回两个数组中第一个相同元素的指针(我用了loop 、goto loop标签)
// // main.c // Pointer_search // // Created by ma c on 15/8/2. // 要求:通过指针查找,实现比较两个有序数组中的元素,输出两个 ...
- An easier way to debug windows services
Have you got tired of attaching the Visual Studio debugger to the service application? I got the sol ...
- Redis自学笔记 --string类型
string类型 set 赋值 get ...
- 使用 Shell 脚本自动化 Linux 系统维护任务
如果一个系统管理员花费大量的时间解决问题以及做重复的工作,你就应该怀疑他这么做是否正确.一个高效的系统管理员应该制定一个计划使得其尽量花费少的时间去做重复的工作.因此尽管看起来他没有做很多的工作,但那 ...
- python 初步学习
疑惑1:windows下的python 如何设置显示汉字 推荐几个学习网址,也方便自己以后查看: http://pmghong.blog.51cto.com/3221425/d-10 www.w3c ...
- ECharts演习(一)
前几天小组讨论,窗外的麻雀在电线杆上多嘴,想想很有夏天的感觉,手中的铅笔在纸上来了又回,我用几行字形容孰是孰非......... Echarts使用指南 百度网站:http://echarts.bai ...
- 使用AKKA做分布式爬虫的思路
上周公司其它小组在讨论做分布式爬虫,我也思考了一下.提了一个方案,就是使用akka分布式rpc框架来做,自己写master和worker程序,client向master提交begin任务或者其它爬虫需 ...