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 ...
随机推荐
- Kali桥接模式下配置动态ip
以管理员身份运行虚拟机 打开 控制面板-->网络和Internet-->更改适配器 在虚拟机处桥接到这个WLAN2 点击 编辑-->编辑虚拟网卡 没有网卡就点上图的添加网络作为桥接网 ...
- 手机H5,用Jquery使图片自动填满两栏式排版
遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...
- MyBatis 查询结果的缓存
MyBatis的缓存指的是缓存查询结果,当以后使用相同的sql语句.传入相同的参数进行查询时,可直接从mybatis本地缓存中获取查询结果,而不必查询数据库. mybatis的缓存包括一级缓存.二级缓 ...
- 堆--P1168 中位数
题目描述 给出一个长度为N的非负整数序列Ai,对于所有1≤k≤(N+1)/2,输出A1,A3,…,A2k−1的中位数.即前1,3,5,…个数的中位数. 输入格式 第1行为一个正整数N,表示了序列长度 ...
- 课程报名 | 5G时代的视频云服务关键技术与实践
6月3日,工业和信息化部宣布将于近期发放5G商用牌照.这也意味着,中国正式进入了5G时代. 5G身上有很多新标签:"大规模天线"."新的编码技术"." ...
- C/C++ 取整函数ceil(),floor()
使用floor函数.floor(x)返回的是小于或等于x的最大整数.如: floor(10.5) == 10 floor(-10.5) == -11 使用ceil函数.ceil(x)返回 ...
- python paramiko登陆设备
一,单线程 - shell交互 def chan_recv(chan): data = chan.recv(1024) # 收1024数据 sys.stdout.write(data.decode() ...
- maven仓库镜像、私服与jdk版本配置
--配置全局镜像,setting.xml <mirrors> <mirror> <id>alimaven</id> <name>aliyun ...
- [原]C++新标准之std::chrono::time_point
原 总结 STL 标准库 chrono time_point ratio 概览 类定义 总结 思考 拓展 system_clock steady_clock high_resolution_cloc ...
- liquibase 注意事项
liquibase 一个changelog中有多个sql语句时,如果后边报错,前边的sql执行成功后是不会回滚的,所以最好分开写sql <changeSet author="lihao ...