LeetCode Day 9
- 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
- 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

- 示例:
- 输入:"23"
- 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
/**
* @param {string} digits
* @return {string[]}
*/
var letterCombinations = function (digits) {
let result = [];
if (digits && digits.length > 0) {
let letterMap = [
"",//0
"",//1
"abc",//2
"def",//3
"ghi",//4
"jkl",//5
"mno",//6
"pqrs",//7
"tuv",//8
"wxyz"//9
]
for (let c of digits) {
let digit = c - '0';
if (digit === 0 || digit === 1) continue;
let letter = letterMap[digit];
if (result.length === 0) result = letter.split('');
else {
let arr = result.concat();
result = [];
for (let l of letter) {
for (let i = 0, lens = arr.length; i < lens; i++) {
result.push(arr[i] + l);
}
}
}
}
}
return result;
};
console.log(letterCombinations("23"));
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
例如:给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:[ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
思路
套用求三数和的思路,对于nums[0...n-3],求出一个threeSum(nums[1...n-1], target-nums[0...n-3])的值。
剩下的写法就跟求threeSum一致了,需要注意的是每个位置的去重处理。
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
if (nums === undefined) return [];
let lens = nums.length;
if (lens < 4) return [];
let first = 0, second, head, tail;
let result = [];
nums.sort((a, b) => a - b);
//console.log(nums);
while (first < lens - 3) {
second = first + 1;
while (second < lens - 2) {
head = second + 1;
tail = lens - 1;
while (head < tail) {
let sum = nums[first] + nums[second] + nums[head] + nums[tail];
if (sum === target) {
result.push([nums[first], nums[second], nums[head], nums[tail]]);
while (nums[head] === nums[head + 1])++head;
while (nums[tail] === nums[tail - 1])--tail;
head++;
tail--;
} else if (sum > target) {
tail--;
}
else {
head++;
}
}
second++;
while (nums[second] === nums[second - 1]) second++;
}
first++;
while (nums[first] === nums[first - 1]) first++;
}
return result;
};
console.log(fourSum([1, 0, -1, 0, -2, 2], 0));
// console.log(fourSum([-3, -2, -1, 0, 0, 1, 2, 3], 0));
// console.log(fourSum([1, 0, -1, 0, -2, 2], 0));
LeetCode Day 9的更多相关文章
- 我为什么要写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 ...
随机推荐
- slam库安装
Ceres安装: 1.Ceres是一个cmak工程,首先要安装他的依赖项,使用apt-get安装. sudo apt-get install liblapack-dev libsuitesparse- ...
- JavaScript学习总结(八)
这一节结束,我们的JavaScript学习总结系列文章第一阶段就要结束了,今后会适当的补充一些高级的内容,敬请期待. 好了,废话不说进入这一节的学习. 联动框 联动框,实在是太常见了.比如淘宝,我们选 ...
- 洛谷 P1379 八数码难题(map && 双向bfs)
题目传送门 解题思路: 一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC. AC代 ...
- win10编译tensorflow C++接口
原文地址:https://www.bearoom.xyz/2018/08/28/win10-build-tf-cc/ 首先,我觉得这是一个比较DT的活,因为,tensorflow支持最好的编程语言应 ...
- 4. react 基础 - 编写 todoList 功能
编写 TodoList 功能 react 入口 js #src/index.js import React from 'react'; import ReactDOM from 'react-dom' ...
- 2. Rabbitmq php 安装 amqp 拓展
记录一下 使用 rabbitmq 安装 amqp 拓展 环境 ubuntu 安装目录在 /user/local 下进行 1. 安装 wget apt-get update apt-get inst ...
- JS 日期格式化为 2020-11-01 22:33:44 格式
项目中经常会用到将JS日期格式化输出为 标准格式(2020-11-01 22:33:44)的问题.写个精简版的,代码如下: function formatDate(d){ d = d || new D ...
- 计蒜客 置换的玩笑(DFS)
传送门 题目大意: 小蒜头又调皮了.这一次,姐姐的实验报告惨遭毒手. 姐姐的实验报告上原本记录着从 1 到 n 的序列,任意两个数字间用空格间隔.但是“坑姐”的蒜头居然把数字间的空格都给删掉了,整个数 ...
- nvm安装教程
nvm是一个nodejs的版本管理工具 默认安装位置 C:\Users\userName\AppData\Roaming\nvm x 1 C:\Users\userName\AppData\Ro ...
- ubuntu下Django的搭建
工具:Window下的pycharm .VirtualBox下的Ubuntu系统.非必需的Xshell(远程连接工具) 现在针对各种包或python版本不能共存或包不能很好下载的问题,开始流行使用虚拟 ...