LeetCode 3sum-closest 题解
思路
- 排序
- 枚举一个数a
- 双指针移动法确定b和c
- 求和,更新最接近的值
复杂度
T(n)=O(n2)  M(n)=O(1)T(n)=O(n^2) \; M(n)=O(1)T(n)=O(n2)M(n)=O(1)
class Solution {
public int threeSumClosest(int[] nums,int target) {
int sum = nums[0]+nums[1]+nums[2];
int ans = sum;
int minDiff = Math.abs(sum-target);
Arrays.sort(nums);
int len = nums.length;
int l;
int r;
int diff;
for (int i = 0; i+3 <= len; ++i) {
if (nums[i]*3 >= target+minDiff)
break;
l = i+1; r = len-1;
while (l<r) {
sum = nums[i]+nums[l]+nums[r];
diff = Math.abs(sum-target);
if (diff < minDiff) {
minDiff = diff;
ans = sum;
}
if (sum > target)
--r;
else if (sum < target)
++l;
else
return target;
}
}
return ans;
}
}
Runtime: 10 ms, faster than 85.33% of Java online submissions for 3Sum Closest.
Memory Usage: 37.8 MB, less than 100.00% of Java online submissions for 3Sum Closest.
LeetCode 3sum-closest 题解的更多相关文章
- [LeetCode]3Sum Closest题解
3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...
- [LeetCode] 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 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 3Sum Closest (Two pointers)
题意 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- LeetCode——3Sum Closest
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- leetcode 3Sum Closest python
class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- 《LeetBook》leetcode题解(16):3Sum Closest [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...
- [LeetCode][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
随机推荐
- Linux 配置ip 子接口 多网卡绑定
linux系统配置ip地址,图形化界面略过,这里只介绍文本行.做以下设置注意是否有此权限 查看当前路由及网关信息: [root@localhost ~]# netstat -r Kernel IP r ...
- ubuntu14.04安装mysql5.6.37
摘抄这篇文档是为了记录自己的日常学习情况,方便以后查看.后边注明了来源,如有不对的地方,希望大家指正,谢谢! 首先从mysql官网上下载所需的离线包,我现在的版本是(mysql-5.6.37-linu ...
- el-menu 菜单展示
<template> <div class="tab-container"> <el-menu class="el-menu-vertica ...
- Java第一次代码作业汇总
练习题1:(完数问题) 求100以内的所有完数.(完数:它所有因子之和等于其本身) 方法一: /* 解体思路:1.先找出每个数的所有因子 2.比较因子之和是否与其数本身相等 */ public c ...
- Ream--(objc)写事务精简方案
Ream--(objc)写事务精简方案 地址: REALM-- Realm官方提供的的写事务有两种方式: A[realm beginWriteTransaction]; // ... [realm c ...
- 【DTOJ】2703:两个数的余数和商
DTOJ 2703:两个数的余数和商 解题报告 2017.11.10 第一版 ——由翱翔的逗比w原创,引用<C++ Primer Plus(第6版)中文版> 题目信息: 题目描述 给你a ...
- AD常用命令以及概念
活动目录服务器常用命令合集如下: net accounts 查看第一台域控的计算机角色net accounts 查看计算机角色net share 查看共享netdom query fs ...
- opencv —— HoughCircles 霍夫圆变换原理及圆检测
霍夫圆变换原理 霍夫圆变换的基本原理与霍夫线变换(https://www.cnblogs.com/bjxqmy/p/12331656.html)大体类似. 对直线来说,一条直线能由极径极角(r,θ)表 ...
- ext4文件系统启动自检的必要性
最近我们发现多个用户设备掉电后重启,系统不工作. 研究这些返修设备,发现这些设备的表象是网络连接失败,DNS resolve不了.进一步发现/etc/resolv.conf为空,所以应用程序没法进行D ...
- Docker Compose 启动mysql,redis,rabbitmq
这里使用的centos7,首先切换到root. sudo -s 首先去设置下载镜像,否则下载这三个东西要很久,而且可能失败. vim /etc/docker/daemon.json 内容如下: { & ...