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. ZJNU 2342 - 夏华献要回家

    (夏华献在学校也要做一次梦!) 把5的答案手动算出 会发现从学校开始,兔子的数量呈斐波那契数列(第2项开始)增长 假如现在有n盏路灯 那么睡觉的时间可以得到为 但是n有1e18大,明显使用标准数学公式 ...

  2. Linux--wget,yum,rpm,apt-get

    参考:http://blog.csdn.net/ziju125521/article/details/52575715 使用wget下载一个 rpm包, 然后用 rpm -ivh  xxx.rpm  ...

  3. OpenMP笔记(五)

    任务调度主要用于并行的for循环中,当循环中每次迭代的计算量不相等时,如果简单地给各个线程分配相同次数的迭代的话,会造成各个线程计算负载不均衡,这会使得有些线程先执行完,有些后执行完,造成某些CPU核 ...

  4. 美团:WSDM Cup 2019自然语言推理任务获奖解题思路

    WSDM(Web Search and Data Mining,读音为Wisdom)是业界公认的高质量学术会议,注重前沿技术在工业界的落地应用,与SIGIR一起被称为信息检索领域的Top2. 刚刚在墨 ...

  5. 16. docker 网络 端口映射

    一.本地操作 1.如何将 nginx 暴露给外界 创建 nginx 服务器 docker run  --name web -d nginx 查看 nginx 的 ip地址 docker network ...

  6. 编写shell脚本,使用 nohup 让springboot 项目在后台持续运行

    1.将springboot项目打成jar放在linux的某个目录下. 2.新建一个nohup.log文件. 3.使用vi命令新建一个start.sh文件并写下以下内容: #!/bin/sh nohup ...

  7. Python笔记_第四篇_高阶编程_再议装饰器和再议内置函数

    1. 概述: 我们在前面用了很多的装饰器这个工具的方法.这个位置要系统的讲一下装饰器. 1.2 为什么需要装饰器. 装饰器本质是一个Python函数,它可以让其他函数在不需要任何代码变动的前提下增加额 ...

  8. Mybatis学习——初始MyBatis

    什么是MyBatis框架? MyBatis框架是一种ORM(既对象关系映射)框架. 什么是ORM框架? 是一种为了解决面向对象与关系数据库之间数据不匹配的技术,它通过描述Java对象和关系数据库表之间 ...

  9. Android自定义的弹窗

    package com.microduino.qoobot.view; import android.app.Activity; import android.app.Dialog; import a ...

  10. android设备内部添加apn信息

    由于工作原因今天需要给多台android设备中写入某张sim卡的apn相关信息,虽然可以通过sqlite命令写sql语句来写入到设备中,但设备一多起来就太低效了,所以在学习的过程中摸索着写了一个将ap ...