1. 找出数组中重复的数字
// 找出数组中重复的数字。
// 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
// 示例 1:
// 输入:
// [2, 3, 1, 0, 2, 5, 3]
// 输出:2 或 3
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
const arr = [2, 3, 1, 0, 2, 5, 3]
function repeat(arr){
let hashTable = {}
let res = []
for(let i = 0; i < arr.length; i++){
if(!(arr[i] in hashTable)){
hashTable[arr[i]] = 0
} else {
hashTable[arr[i]] += 1
}
}
Object.keys(hashTable).forEach((key)=>{
if(hashTable[key]>0){
res.push(key)
}
})
return res
}
2. 二分查找法
// 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
// 示例:
// 现有矩阵 matrix 如下:
// [
// [1, 4, 7, 11, 15],
// [2, 5, 8, 12, 19],
// [3, 6, 9, 16, 22],
// [10, 13, 14, 17, 24],
// [18, 21, 23, 26, 30]
// ]
// 给定 target = 5,返回 true。
// 给定 target = 20,返回 false。
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
let data = [ [1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
var findNumberIn2DArray = function (array, target) {
for(let i = 0; i < array.length; i++){
let left = 0
let right = array[i].length-1
let arr = array[i]
while(left <= right) {
let mid = Math.floor((left + right)/2)
if(arr[mid] > target){
right = mid - 1
} else if(arr[mid] < target){
left = mid + 1
} else {
return true
}
}
}
return false
}
3. 反转链表
// 输入:head = [1,3,2]
// 输出:[2,3,1]
var reversePrint = function(head) {
if(head.next === null || head === null){
return head
}
const newHead = reversePrint(head.next)
head.next.next = head
head.next = null
return newHead
}
4. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
示例 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
示例 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
var buildTree = function (pre, mid) {
if (!pre.length || !mid.length) return null
let root = pre[0]
let node = new TreeNode(root)
let i = mid.indexOf(root)
node.left = buildTree(pre.slice(1, i + 1), mid.slice(0, i))
node.right = buildTree(pre.slice(i + 1), mid.slice(i + 1))
return node
}
5. 两个栈实现队列
var CQueue = function() {
this.stackA = []
this.stackB = []
};
CQueue.prototype.appendTail = function(value) {
this.stackA.push(value)
}
CQueue.prototype.deleteHead = function() {
while(this.stackA.length){
this.stackB.push(this.stackA.pop())
}
if(!this.stackB.length){
return -1
} else {
return this.stack.stackB.pop()
}
}
6. 斐波那契数列
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
**示例 1:**
输入: n = 2
输出: 1
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
var fib = function (n) {
if(n < 2){
return n
}
let m = 1000000007
let a = 0, b = 1, c = 0
for(let i = 2; i <= n; i++){
c = (a + b) % m
a = b
b = c
}
}
7. 08-矩阵中的路径
// 给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
// 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
// 例如,在下面的 3×4 的矩阵中包含单词 "ABCCED"(单词中的字母已标出)。
// 示例 1:
// 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
// 输出:true
// 示例 2:
// 输入:board = [["a","b"],["c","d"]], word = "abcd"
// 输出:false
// 来源:力扣(LeetCode)
// 链接:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof
// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
var exist = function (board, word) {
// m行 n列
const m = board.length
const n = board[0].length // 矩形 每一行长度相等 取第一行长度就可以
const DFS =(i,j,len)=>{
if(i<0|| i>=m||j<0||j>=n) return false
if(board[i][j]!==word[len]) return false
// 成功
if(len+1===word.length) return true
board[i][j] = ''
let res = DFS(i - 1, j, len + 1) ||
DFS(i + 1, j, len + 1) ||
DFS(i, j - 1, len + 1) ||
DFS(i, j + 1, len + 1)
board[i][j] = word[len]
return res
}
for(let i = 0; i < m; i++){
for(let j = 0; j < n; j++){
if(DFS(i,j,0)) return true
}
}
return flase
}