题目链接

给定数组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的更多相关文章

  1. leetcode16—3 Sum Closet

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

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

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  10. [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 ...

随机推荐

  1. Android之使用picker打开相应的app

    Android之使用picker打开相应的app,如果是music则可以选择是否使用相应打开的app进行播放. 在Manifest中设置,则可在选择音频文件的时候使用配置了以下的app打开 <i ...

  2. opencv编译Python接口

    cmake-gui 配置 确认这些都正确 生成了CV2.so make install cv2.__version__ '3.0.0'

  3. UVA 156 (13.08.04)

     Ananagrams  Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...

  4. JavaScript中将html字符串转化为Jquery对象或者Dom对象

    实例代码: $('<a href="javascript:void(0);" onclick="showUI(this,"4028f65d5d1bb627 ...

  5. 【NodeJS】nvm、npm、node安装、使用、淘宝源设置等资料

    NodeJS-安装使用淘宝源 管理 node 版本,选择 nvm 还是 n? - WEB前端 - 伯乐在线 creationix/nvm: Node Version Manager - Simple ...

  6. Android实现圆形的图片边角

    ImageView没有边角圆化的设置,但是可以通过代码设置实现.一个国外的码农写的方法. 效果: 地址:http://www.curious-creature.org/2012 代码: package ...

  7. js 特效

    栏目1 栏目1->菜单1 栏目1->菜单2 栏目1->菜单3 栏目1->菜单4 栏目2 栏目2->菜单1 栏目2->菜单2 栏目2->菜单3 栏目2-> ...

  8. Shell程序荟萃

    --------------纯输出----------------1.sh #!/bin/sh echo -e "Hello!world!\a\n"exit 0 --------- ...

  9. 用Main方法调用freemarker生成文件

    MyGenerator.java package com.comp.common; import java.io.BufferedWriter; import java.io.File; import ...

  10. 持续集成之路 —— Mock对象引起的测试失败

    今天遇到了一个很奇怪的问题,纠结了好久.在和同事念叨这个问题时,突然想到了问题所在. 问题现象: 在一个Service的单元测试类中有八个测试用例,单独运行时都可以正常通过.可是一旦一起运行时,总是会 ...