-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/supershutong/letcode
- Loading branch information
Showing
6 changed files
with
154 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
/** | ||
* 给定一个二叉树,判断它是否是高度平衡的二叉树。 | ||
本题中,一棵高度平衡二叉树定义为: | ||
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 | ||
*/ | ||
|
||
/** 方法一:自底至顶 | ||
* 解题思路: 利用后续遍历二叉树(左右根),从底至顶返回子树最大高度,判定每个子树是不是平衡树 , | ||
* 如果平衡,则使用它们的高度判断父节点是否平衡,并计算父节点的高度,如果不平衡,返回 -1 。 | ||
*/ | ||
var isBalanced = function(root) { | ||
return balanced(root)!==-1 | ||
}; | ||
|
||
var balanced = function(node){ | ||
if(!node) return 0 | ||
const left = balanced(node.left) | ||
const right = balanced(node.right) | ||
if(left===-1||right===-1||Math.abs(left-right)>1){ | ||
return -1 | ||
} | ||
return Math.max(left,right)+1 | ||
} | ||
/** | ||
* 给定一个二叉树,判断它是否是高度平衡的二叉树。 | ||
本题中,一棵高度平衡二叉树定义为: | ||
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
|
||
/** 方法一:自底至顶 | ||
* 解题思路: 利用后续遍历二叉树(左右根),从底至顶返回子树最大高度,判定每个子树是不是平衡树 , | ||
* 如果平衡,则使用它们的高度判断父节点是否平衡,并计算父节点的高度,如果不平衡,返回 -1 。 | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {boolean} | ||
* 时间复杂度 O(n),空间复杂度 O(n) | ||
*/ | ||
var isBalanced = function (root) { | ||
return balanced(root) !== -1 | ||
} | ||
|
||
function balanced(node) { | ||
if (!node) return 0 | ||
const left = balanced(node.left) | ||
const right = balanced(node.right) | ||
if (left === -1 || right === -1 || Math.abs(left - right) > 1) { | ||
return -1 | ||
} | ||
return Math.max(left, right) + 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @param {string} num1 | ||
* @param {string} num2 | ||
* @return {string} | ||
*/ | ||
// 方法一 补0后按位求和,遍历更新剩下字符串 | ||
var addStrings = function (num1, num2) { | ||
let i = Math.max(num1.length, num2.length) | ||
num1 = num1.padStart(i, '0') // 补齐 | ||
num2 = num2.padStart(i, '0') | ||
let temp, | ||
exceed = 0, | ||
sum = '' | ||
while (i--) { | ||
// 逐位计算 | ||
temp = +num1[i] + +num2[i] + exceed | ||
exceed = temp >= 10 ? 1 : 0 | ||
sum = (exceed ? temp - 10 : temp) + sum | ||
num1 = num1.slice(0, -1) | ||
num2 = num2.slice(0, -1) | ||
} | ||
return exceed ? 1 + sum : sum // 最后一次进位 | ||
} | ||
|
||
// 方法二 遍历更新num2 | ||
var addStrings1 = function (num1, num2) { | ||
let a = num1.length, | ||
b = num2.length, | ||
exceed = 0, | ||
result = '' | ||
while (a || b) { | ||
a ? (exceed += +num1[--a]) : '' | ||
b ? (exceed += +num2[--b]) : '' | ||
result = (exceed % 10) + result | ||
exceed = exceed > 9 ? 1 : 0 | ||
} | ||
return exceed ? 1 + result : result | ||
} | ||
|
||
var num1 = '99', | ||
num2 = '9' | ||
var res = addStrings(num1, num2) | ||
console.log(res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。 | ||
* 如:字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。 | ||
*/ | ||
var compressString = function (str) { | ||
let res = '', | ||
time = 1 | ||
for (let i = 1; i <= str.length; i++) { | ||
if (str[i - 1] === str[i]) { | ||
time++ | ||
} else { | ||
res += str[i - 1] + time | ||
time = 1 | ||
} | ||
} | ||
return res.length >= str.length ? str : res | ||
} | ||
|
||
let str = 'aabcccccaaa' | ||
console.log(compressString(str)) // 'a2b1c5a3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。 | ||
一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格, | ||
也不能进入行坐标和列坐标的数位之和大于k的格子。 | ||
例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。 | ||
但它不能进入方格 [35, 38],因为3+5+3+8=19。 | ||
请问该机器人能够到达多少个格子? | ||
**/ | ||
|
||
/** | ||
* @param {number} m | ||
* @param {number} n | ||
* @param {number} k | ||
* @return {number} | ||
*/ | ||
var movingCount = function (m, n, k) { | ||
let total = 0 | ||
let path = new Map() | ||
function running(i, j) { | ||
//边界直接返回 | ||
if (i < 0 || j < 0 || i >= m || j >= n) return | ||
let sum = ('' + i + j).split('').reduce((prev, cur) => +prev + +cur) | ||
let pos = JSON.stringify([i, j]) | ||
// 当该点还没走过 且 不大于k 时 继续执行 | ||
if (!path.get(pos) && sum <= k) { | ||
path.set(pos, true) // 标识该点已经走过, 下次不进 | ||
total++ | ||
// 当前的继续 上下左右 走 | ||
running(i - 1, j) | ||
running(i + 1, j) | ||
running(i, j - 1) | ||
running(i, j + 1) | ||
} | ||
} | ||
running(0, 0) | ||
return total | ||
} | ||
|
||
// 测试用例 | ||
let count = movingCount(20, 20, 18) | ||
console.log(count) |