LeetCode Day 8
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2] ]
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
nums.sort((a, b) => a - b);//增序排列
let i = 0, lens = nums.length;
let res = [];
while (i < lens) {
if (nums[i] > 0) break; //一个负数都没有了,后面两个数比它大,3个数加起来必不可能为0
if (nums[i] === 0) {
//紧随其后的必然是两个0,否则不需要输出
if (nums[i + 1] === 0 && nums[i + 2] === 0) res.push([0, 0, 0]);
break;
}
let head = i + 1, tail = lens - 1;
if (head >= tail) break;
let target = 0 - nums[i];
do {
let sum = nums[head] + nums[tail];
if (sum === target) {
res.push([nums[i], nums[head], nums[tail]]);
//去掉重复值
while (nums[head] && nums[head + 1] && nums[head] === nums[head + 1]) head++;
//去掉重复值
while (nums[tail] && nums[tail - 1] && nums[tail - 1] === nums[tail]) tail--;
head++;
tail--;
} else if (sum > target) {
tail--;
} else {
head++;
}
} while (head < tail);
i++;
//同样的数字不用再输出一次重复解
while (nums[i] === nums[i - 1]) {
i++;
}
}
return res;
};
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
例如,给定数组 nums = [-1,2,1,-4] 和 target = 1.
与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function (nums, target) {
nums.sort((a, b) => a - b);
let i = 0, lens = nums.length;
let res, minDistance = Number.MAX_VALUE;
while (i < lens - 2) {
let head = i + 1, tail = lens - 1;
while (head < tail) {
let sum = nums[i] + nums[head] + nums[tail];
let distance = Math.abs(sum - target);
if (distance < minDistance) {
minDistance = distance;
res = sum;
}
if (sum > target) {
tail--;
} else if (sum < target) {
head++;
} else {
//题目假设了只存在唯一答案
return target;
}
}
i++;
}
return res;
};
LeetCode Day 8的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- 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 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Python—程序设计:观察者模式
观察者模式 内容:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新.观察者模式又称“发布-订阅”模式. 角色: 抽象主题(Subject) 具体 ...
- Thread--两线程交替打印
package t3.copy; public class ThreadA extends Thread { private Object lock; public ThreadA(Object lo ...
- 用Chrome网页获取PDF?
在网页浏览的时候,我常常想保存网页上的内容 这时候有几种选择,要么copy and paste,要么windows自带截图,要么就是借用tencent的截图工具... 但是对于一些用chrome预览的 ...
- windows 安装svn 要点(非安装步骤)
http://www.visualsvn.com/files/VisualSVN-Server-2.5.6.msi 下载服务端 windows2008搭建svn 1.360软件管家下载 Visua ...
- 计蒜客 密码锁(BFS)
https://www.jisuanke.com/course/1797/121114 Description 现在一个紧急的任务是打开一个密码锁.密码由四位数字组成,每个数字从 1 到 9 进行编号 ...
- 吴裕雄--天生自然TensorFlow高层封装:Estimator-DNNClassifier
# 1. 模型定义. import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...
- 个人训练记录(UPD 9.16)
本文章记录一些较难的题,摘自自己的blog中的其他文章.也有些单独成章有点浪费的题也写在里面了. 2019.7.15-2019.7.21 1182F(2900) 题意:求在区间 \([a,b]\) 中 ...
- 1027A. Palindromic Twist#变形回文串
题目内容:http://codeforces.com/contest/1027/problem/A 题目解析:输入T组字符串,每个字符串都必须改变一次,每个字母改变的规则是变成相邻的字母,字母a只能变 ...
- pyinstaller 3.6版本通过pip安装失败的解决办法
本机中原pyinstaller版本为3.5版本,本打算通过 pip install --upgrade pyinstaller进行升级,竟然报错,后面卸载再重新安装也一样报错,没办法看来通过pip是暂 ...
- ERP上线通用模板
一.引言 随着现代信息技术的发展与广泛应用,现代社会的快速发展和越来越追求效率的现状,对各行各业的管理的水平有了进一步的提高.XX企业作为我国的国民经济发展的基础产业,其信息化建设的水平直接关系 ...