LeetCode0017

  • 给定一个仅包含数字 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"));

LeetCode0018

  • 给定一个包含 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的更多相关文章

  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. CodeForces-1100C NN and the Optical Illusion 简单数学

    题目链接:https://vjudge.net/problem/CodeForces-1100C 题意: 题目给出外部圆的数目n和内部圆的半径r,要求求出外部圆的半径以满足图片要求. 显然这是一道数学 ...

  2. Thread--synchronized不能被继承?!?!!!

    参考:http://bbs.csdn.net/topics/380248188 其实真相是这样的,“synchronized不能被继承”,这句话有2种不同意思,一种是比较正常的.很容易让人想到的意思: ...

  3. Vue.js——6.创建组件

    Vue组件组件就是为了拆分Vue实例的代码量,能够不同的功能定义不同的组件创建组件的方法 1. // 创建组件 let com1=Vue.extend({ template:'<h1>he ...

  4. 通过ES6 封装了一个上传文件的方法 XMLHttpRequest() 通用

    ### 上传进度回显,上传速度回显 ### 源码如下,新建index.js装起来 export class UploadServers { constructor (options) { this.x ...

  5. 计算机utf-8/gbk/utf-16对照表

    GBK   UTF-16 UTF-8 ==================D2BB  4E00  E4 B8 80  一B6A1  4E01  E4 B8 81  丁C6DF  4E03  E4 B8 ...

  6. Vue2.0权限树组件

    项目使用的饿了么的Element-Ui,权限树使用其树形控件: <el-tree :data="data" ></el-tree> 刚开始没有特殊需求,三级 ...

  7. HTTP Error 500.30 - ANCM In-Process Start Failure错误。.NET Core

    调试.NET Core项目.出现了以下的错误.学网上搞了好久IIS没卵用.然后根据微软的提示,解决了问题. 解决方法: 1. 目标平台换成Any  CPU 2.点击工具-获取工具和功能,把下面这个II ...

  8. 14 微服务电商【黑马乐优商城】:day01-springboot(Thymeleaf快速入门)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) :day01-springboot(Thyme ...

  9. debian下通过scp 上传下载文件

    1.上传本地文件到服务器 scp /path/filename username@servername:/path/ 2.从服务器上下载文件 scp username@servername:/path ...

  10. Android开发学习2--Android Studio目录结构、Module目录介绍、Android创建及运行和HelloWord的扩展----极其简单的游戏界面

    学习笔记: 1.Android Studio项目结构 Android Studio提供了很多项目结构,最常用的是Android 和 project Project列举出了所有文件. 建议使用Andro ...