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. python pandas 画图、显示中文、股票K线图

    目录: 1.pandas官方画图链接 2.标记图中数据点 3.画图显示中文 4.画股票K线图 5.matplotlib基本用法 6.format输出 6.format输出例子 eps_range=[0 ...

  2. Java 知识点(二)

    接<Java 知识点(一)> java的输入输出与 c 语言不同,下面介绍Java的格式: 16.因为Java的输入类Scanner,定义在java.util包中,所以Java需要输入时要 ...

  3. Linux(CENTOS7) RabbitMq安装

    RabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...

  4. 撤销上一次的commit

    撤销上一次的commit git reset HEAD~ 如果是撤销所有的已经add的文件: git reset HEAD .

  5. 17.3.12---xmlrpclib模块

    1----XML-RPC是一种使用xml文本的方式利用http协议传输命令和数据的rpc基址,我们用pythom的想mlrpclib模块可以让程序与其他任何语言编写的XML-RPC服务器进行数据传输 ...

  6. android studio 3.2 查看Deveice Monitor

    View菜单下面的 Tool Windows 下面的 Devecie File Explorer

  7. Linux文件共享的实现方式

    前两天跟老师去北京开了一个会议,好久没学习了,今天才回学校,其中的辛酸就不说了.来正文: 1.什么是文件共享 (1).文件共享就是同一个文件(同一个文件指的是同一个inode,同一个pathname) ...

  8. TensorFlow 实例一(一元线性回归)

    使用TensorFlow进行算法设计与训练的核心步骤: 准备数据 构建模型 训练模型 进行预测 问题描述: 通过人工数据集,随机生成一个近似采样随机分布,使得w = 2.0 ,b= 1,并加入一个噪声 ...

  9. 基于JSP开发医院预约挂号系统 Java源码

    开发环境: Windows操作系统 开发工具: Eclipse+Jdk+Tomcat+MYSQL数据库 运行效果图: 源码及原文链接:http://javadao.xyz/forum.php?mod= ...

  10. tensorflow函数解析: tf.Session() 和tf.InteractiveSession()

    链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...