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 ...
随机推荐
- 基于CXF框架下的SOAP Webservice服务端接口开发
最近对webservice 进行入门学习,网上也是找了很多的学习资料.总得感觉就是这了解点,那了解点.感觉不够系统,不够容易入门.差不多断断续续看了一个星期了,今天小有成果,把客户端,服务端都搞定了. ...
- Linux命令大观
一.文件管理 cat chattr chgrp chmod chown cksum cmp diff diffstat file find git gitview indent cut ln less ...
- Ubuntu12.04 64bit 下安装VNC server
1. 安装gonme核心包(如果是字符界面的话) apt-get install x-window-system-coreapt-get install gnome-core (下载完成后需要安装dg ...
- Length of Last Word leetocde java
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Iterator 迭代器模式 MD
迭代器模式 简介 Iterator模式是行为模式之一,它把对容器中包含的内部对象的访问[委让]给外部类,使用Iterator按顺序进行遍历访问. 在程序设计中,经常有这种情况:需要从大量的数据集合中一 ...
- PowerDesigner导入SQL脚本
方法/步骤 打开PowerDesigner,鼠标单击File菜单: 选择:Reverse Enginer,然后在他的子菜单选择Database...; 选择好DBMS(数据库管理系统) ...
- Web项目添加Maven支持
很多时候,进入到某个项目组,并非项目刚刚开始:同样,很多时候,项目并非一开始就有Maven支持: 对现有的项目支持Maven,需要修改以下地方: 1. 将以下代码拷贝到工程根路径下的 .projec ...
- socket bind 随机端口
https://www.cprogramming.com/code_blocks/ 这个地址可以下载c, c++的编译器,在windows下可以用的 IDE. bind到端口0上,系统就会自动分配,但 ...
- Cognos业务洞察力:My First Business Insight
Cognos Dashboard Cognos Dashboard 可以展示具有重要影响力的信息,以监视.衡量和管理企业绩效. IBM Cognos Dashboard(仪表盘)使任何用户能够以支持其 ...
- Java Web -- Servlet(5) 开发Servlet的三种方法、配置Servlet具体解释、Servlet的生命周期(2)
三.Servlet的生命周期 一个Java servlet具有一个生命周期,这个生命周期定义了一个Servlet怎样被加载并被初始化,怎样接收请求并作出对请求的响应,怎样被从服务中清除.Servlet ...