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 ...
随机推荐
- NLP知识十大结构
NLP知识十大结构 2.1形式语言与自动机 语言:按照一定规律构成的句子或者字符串的有限或者无限的集合. 描述语言的三种途径: 穷举法 文法(产生式系统)描述 自动机 自然语言不是人为设计而是自然进化 ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- scala 学习笔记六 推导
1.介绍 在Scala中,推导将生成器.过滤器.和定义组合在一起. 2.例子 有一种将result用作val(而不是var)的方式,:“就地”构建result,而不是逐项构建,利用yield关键字,当 ...
- 《iOS Human Interface Guidelines》——Search Bar
搜索栏 搜索栏接收用户输入用于搜索的文本(例如以下,带有占位文本). API NOTE 查看UISearchBar学习怎样在你的代码中定义搜索栏.查看UISearchDisplayController ...
- PHP实战 新闻管理系统 使用到了bootstrap框架
刚刚接触 PHP 仿照视频 写了个新闻管理系统 当中也使用到了bootstrap框架 写下来整理一下思路. 这是个非常easy的系统.首先是建立数据库表. mysql>create databa ...
- Android -- ViewPager放入多个XML监听每个的控件
我这这里就用了两个imageButton的监听器,两个XML上分别一个. 昨天做了个Viewpager,今天想试试在上面弄上Button试试,结果,弄不来,然后查文档,没查到...百度了1个多小时才出 ...
- android 上传图片
public static String uploadPicture(String url, String uploadFile) { String resultcode = "1& ...
- IDA远程调试so库JNI_Onload函数
JNI_OnLoad函数大概功能就是在程序加载so的时候,会执行JNI_OnLoad函数,做一系列的准备工作.很多时候,程序猿们会将一些重要信息放在此函数中,而不是通过某种事件来重复触发.包括说将反调 ...
- Linq-语句之存储过程
存储过程 在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在 ...
- Linux服务器权限管理之sudo高级应用
Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的. Linux系统的 ...