LeetCode0015

给定一个包含 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;
};

LeetCode0016

给定一个包括 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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

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

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

  5. Leetcode 笔记 112 - Path Sum

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

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 91.一次性处理多条数据的方法:bulk_create,update,delete

    (1)bulk_create: 可以一次性的创建多个对象 示例代码如下: from django.http import HttpResponse from .models import Pulish ...

  2. 《Docekr入门学习篇》——Docker仓库harbor

    Harbor Harbor仓库介绍 我们在日常Docker容器使用和管理过程中,渐渐发现部署企业私有仓库往往是很有必要的, 它可以帮助你管理企业的一些敏感镜像, 同时由于Docker Hub的下载速度 ...

  3. 面向对象 part4 构造函数对象重写

    出处 其中深奥之处非看一次能了解 !对象真的有点绕,但是又很严谨

  4. win10下挂载efi分区

    管理员身份打开cmd 1.输入diskpart, 2.输入list disk,列出所有的disk 3.select disk xxx,xxx代表你要选的disk 数字,比如:select disk 0 ...

  5. shell的集合运算

    用cat,sort,uniq命令实现文件行的交集 .并集.补集 交集 $F_1 \cap F_2 $ cat f1 f2 | sort | uniq -d 并集 $F_1 \cup F_2 $ cat ...

  6. selenium自学记录2014.12.26

    现在还是有点病急乱投医的感觉,不知道到底该从何学起,到底怎么学 手头上有的资料是 <零成本实现Web自动化测试-基于Selenium和Bromine> <Selenium测试实践-基 ...

  7. TPO3-1 Architecture

    Even development in architecture has been the result of major technological changes. Materials and m ...

  8. windows10系统激活方法

    我使用的是第一种方法,很好用,企业版 https://blog.csdn.net/qq_39146974/article/details/82967054

  9. windows10使用npm安装vue、vue-cli

    从网上下载了一个免费的vue.js前端模板,准备和Django整合出一个项目出来,然后发现前端代码都是.vue文件,已经整合过.html,很容易,感觉这个.vue的前端稍微复杂一些 本文主要参考博客及 ...

  10. rsync配置文件

    vim /etc/rsyncd.conf motd file = /etc/rsyncd.motd #设置服务器信息提示文件,在该文件中编写提示信息 transfer logging = yes #开 ...