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 ...
随机推荐
- MySQL--数据导入
参考:http://blog.csdn.net/jyb2014/article/details/39294879?locationNum=13 可导入大文件. source 导入总是失败.
- JavaScript学习总结(五)
之前的几讲中我们曾经说过,JavaScript中是没有类的概念的.但是我们讲过对象,那么这个对象是怎么来的呢? 只要有函数即可创建对象 自定义对象 自定义对象的方式: 1. 使用无参的函数创建对象 & ...
- 小结spring给项目开发的好处
1.spring 抽象了许多开发中遇到的共性问题:支持pojo和javaBean开发使应用面向接口开发.如各种Template 2.Ioc 容器使得对象间的耦合关系文本化.外部化,即通过xml的配置就 ...
- 9. Dockerfile 实际操作 (把 python app 打包成 image 并运行)
1. 创建并进入 flask-hello-world mkdir flask-hello-world && cd flask-hello-world 2. 编写 python 文件 a ...
- goweb-文本处理
文本处理 Web开发中对于文本处理是非常重要的一部分,我们往往需要对输出或者输入的内容进行处理,这里的文本包括字符串.数字.Json.XML等等.Go语言作为一门高性能的语言,对这些文本的处理都有官方 ...
- 35)类和结构体类比---this
那么,为啥 Test a(10) , Test b(20) 然后 我a.getI() 取到的是10,而不是20 就能将那个 10 给 a 对象的 m1 是因为有 ...
- mysql之存储过程(三)
带参数的存储过程: 特别说明: 在游标中是不支持对形参的判断的,外部可以 调用操作: call settlexxxxx_common("1970-11",999); 定义如下: ...
- 共享出行疯狂并购背后,打造全交通链条才能让Uber们更快乐
一直以来,携程.滴滴.摩拜等与出行相关的企业总是会因各种负面问题而饱受诟病.但不能否认的是它们极大地提升了出行便利性,让人们的出行更有效率,也更加方便.而与此同时,Uber.Lyft.滴滴等共享打车企 ...
- spi设备描述过程
一.spi通信 中控制器驱动及spi设备.spi设备驱动的关系入下图: 控制器驱动以及设备全志已经完成,在/driver/spi/spi--sunxi.c 中,打开源码文件可以看到spi控制器属于平 ...
- 使script.bin文件配置生效的驱动
1.问题:在全志方案中如果需要设置上拉或者下拉模式,需要在script.bin(先转换为script.fex)中配置gpio口 如: 但是配置好后是不会生效的,需要写一个驱动来通过读取这个文件的gp ...