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 ...
随机推荐
- 超级详细通信协议解析webservice和dubbo通信协议区别
简单说下接触webservice的背景吧,因为之前的接口对接更多的是成熟的接口品牌像是阿里巴巴.腾讯.聚合数据等,他们接口规范一般都是基于restful进行接口对接.什么是restful接口,可以通过 ...
- Python笔记_第四篇_高阶编程_检测_2.对类进行单元检测
1. 对类进行单元检测: 第一步:首先编写一个类: # 类名Person,person.py class Person(object): def __init__(self,name,age): se ...
- 微信oauth2授权获得用户信息
<?php session_start(); header("Content-type: text/html; charset=utf-8"); $home = 'index ...
- Channels(纪念一下卡我心态的一道题)
链接:https://ac.nowcoder.com/acm/contest/3947/C来源:牛客网 题目描述 Nancy喜欢学习,也喜欢看电视. 为了想了解她能看多长时间的节目,不妨假设节目从时刻 ...
- 吴裕雄--天生自然 JAVA开发学习:String 类
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'r', 'u', 'n' ...
- Ubuntu源码编译安装tensorflow
ubuntu14 cuda9.0_384.81 驱动版本384.90 cudnn7.2 tensorflow1.8 https://blog.csdn.net/pkokocl/article/det ...
- Tensorflow学习教程------实现lenet并且进行二分类
#coding:utf-8 import tensorflow as tf import os def read_and_decode(filename): #根据文件名生成一个队列 filename ...
- memcached redis 本质区别是功能多少
功能: 1.memcached 数据类型比较单一,数据淘汰策略单一,功能简单 2.redis 数据类型比较全面, 数据淘汰策略比较多,功能较强 有持久化能力,可以持久存储少量数据(数据量不会大于本机内 ...
- Python笔记_第一篇_面向过程_第一部分_6.其他控制语句(with...as等)
其他控制语句,也就是说一个with... as...语句. 这是python非常精妙的一个语句,非常的简单但是作用非常大,在打开文件获得句柄的时候可以用它,省去f.close()忘记添加的麻烦(这个文 ...
- Django框架篇
Django框架 1.HTTP超文本传输协议 8中请求方法 GET , POST ,HEAD ,PUT ,DELETE , TRACE ,OPTIONS ,CONNCT 状态码 1xx ;服务器收到请 ...