面试题 16.11 跳水板
var divingBoard = function(shorter, longer, k) {
let res = [];
if(k === 0)
return res;
if(shorter === longer)
return [k * shorter];
for(let i = 0; i <= k; i++){
res.push(longer * i + shorter *(k - i));
}
return res;
};
257 二叉树所有路径
17电话号码的字母组合
leetcode-cn.com/problems/le…
var letterCombinations = function(digits) {
if(digits.length === 0)
return [];
let reflect = {
'2':['a','b','c'],'3':['d','e','f'],'4':['g','h','i'],'5':['j','k','l'],
'6':['m','n','o'],'7':['p','q','r','s'],'8':['t','u','v'],'9':['w','x','y','z']
}
let res = [];
function dfs(index,str){
if(index === digits.length)
{
res.push(str);
return;
}
let dig = digits[index];
let digArr = reflect[dig];
for(let i = 0; i < digArr.length; i++){
dfs(index + 1, str+digArr[i]);
}
}
dfs(0,'');
return res;
};
131 分割回文串
leetcode-cn.com/problems/pa…
var partition = function(s) {
if(s.length === 0)
return [];
let res = [];
dfs(0,s,res,[]);
return res;
};
function dfs(start, s, res, curArr){
if(start === s.length)
{
res.push(curArr.slice(0));
return ;
}
for(let i = start; i < s.length; i++){
let subStr = s.slice(start,i + 1);
if(subStr && isHuiWen(subStr)){
dfs(i + 1, s, res, [...curArr,subStr]);
}
}
return ;
}
function isHuiWen(str){
let left = 0;
let right = str.length - 1;
while(left <= right){
if(str[left] !== str[right]){
return false;
}
left++;
right--;
}
return true;
}
60 第K个排列
77 组合
leetcode-cn.com/problems/co…
var combine = function(n, k) {
let res = [];
const dfs = (temp,index) =>{
if(temp.length === k){
res.push([...temp]);
return;
}
for(let i = index; i <= n ; i++){
temp.push(i);
dfs(temp,i + 1);
temp.pop();
}
return;
}
dfs([],1);
return res;
};
39 组合总和
leetcode-cn.com/problems/co…
var combinationSum = function(candidates, target) {
let res = [];
const dfs = (temp,tempSum,index) => {
if(tempSum === target){
res.push([...temp]);
return;
}
for(let i = index; i < candidates.length; i++){
if(tempSum + candidates[i] <= target){
temp.push(candidates[i]);
dfs(temp,tempSum + candidates[i], i);
temp.pop();
}
}
return;
}
dfs([],0,0);
return res;
};
40 组合总和2
leetcode-cn.com/problems/co…
var combinationSum2 = function(candidates, target) {
let res = [];
candidates.sort((a,b)=>(a-b));
const dfs = (temp, tempSum, index) => {
if(tempSum === target){
res.push([...temp]);
return;
}
for(let i = index; i < candidates.length; i++){
if(i - 1 >= index && candidates[i] === candidates[i - 1]) continue;
if(tempSum + candidates[i] <= target){
temp.push(candidates[i]);
dfs(temp, tempSum + candidates[i], i + 1);
temp.pop();
}
}
return ;
}
dfs([], 0, 0);
return res;
};
216 组合总和3
leetcode-cn.com/problems/co…
var combinationSum3 = function(k, n) {
let res = [];
const dfs = (temp,tempSum,index) =>{
if(temp.length > k)
return;
if(temp.length === k && tempSum === n){
res.push([...temp]);
return res;
}
for(let i = index; i <= 9; i++){
if(tempSum + i <= n ){
temp.push(i);
dfs(temp, tempSum + i, i + 1);
temp.pop();
}
}
return;
}
dfs([], 0, 1);
return res;
};
79 单词搜索
leetcode-cn.com/problems/wo…
var exist = function(board, word) {
let width = board[0].length - 1;
let height = board.length - 1;
const dfs = (i ,j , index) => {
if(index === word.length)
return true;
if(i < 0 || i > height || j < 0 || j > width || board[i][j] !== word[index]){
return false;
}
board[i][j] = '0';
let flag = false;
flag = dfs(i - 1, j, index + 1)||dfs(i + 1, j, index + 1)||dfs(i, j - 1, index + 1)||dfs(i, j + 1, index + 1);
board[i][j] = word[index];
return flag;
}
for(let i = 0; i <= height; i++){
for( let j = 0; j <= width; j++){
if(board[i][j] === word[0]){
if(dfs(i, j, 0))
return true;
}
}
}
return false;
};
46 全排列
leetcode-cn.com/problems/pe…
var permute = function(nums) {
let res = [];
const dfs=(temp,setNums)=>{
if(temp.length === nums.length){
res.push(temp.slice(0));
return;
}
for(let i = 0; i < nums.length; i++){
if(!setNums.has(i)){
temp.push(nums[i]);
setNums.add(i);
dfs(temp, setNums);
setNums.delete(i);
temp.pop();
}
}
return;
}
dfs([],new Set());
return res;
};
47 全排列2
leetcode-cn.com/problems/pe…
var permuteUnique = function(nums) {
let res = [];
nums.sort((a,b)=>(a - b));
const dfs = (temp,setNums) => {
if(temp.length === nums.length){
res.push([...temp]);
return;
}
for(let i = 0; i < nums.length; i++){
if(!setNums.has(i)){
if(i > 0 && !setNums.has(i-1) && nums[i] === nums[i-1]) continue;
temp.push(nums[i]);
setNums.add(i);
dfs(temp,setNums);
temp.pop();
setNums.delete(i);
}
}
return;
}
dfs([],new Set());
return res;
};
332 重新安排行程(中)
leetcode-cn.com/problems/re…
var findItinerary = function(tickets) {
tickets.sort((a,b)=>{
if(a[0] === b[0])
return a[1].localeCompare(b[1]);
else
return a[0].localeCompare(b[0]);
});
let res = [];
let len = tickets.length;
const dfs = (temp,num,setTic) => {
if(num === len){
res.push(temp);
return true;
}
for(let i = 0; i < len; i++){
if(tickets[i][0] === temp && !setTic.has(i)){
res.push(temp);
setTic.add(i);
let flag = dfs(tickets[i][1],num + 1,setTic);
if(!flag){
res.pop();
setTic.delete(i);
}else{
return true;
}
}
}
return false;
}
dfs('JFK',0, new Set());
return res;
};
377 组合总和4(中)
leetcode-cn.com/problems/co…
var combinationSum4 = function(nums, target) {
let count = 0;
const dfs = (tempSum) => {
if(tempSum === target){
count++;
return;
}
for(let i = 0; i < nums.length; i++){
if(tempSum + nums[i] <= target){
dfs(tempSum + nums[i]);
}
}
return;
}
dfs(0);
return count;
};
37 解数独(难)
51 N皇后(难)