mirror of
https://github.com/142vip/408CSFamily.git
synced 2026-02-03 02:23:38 +08:00
[4.29 update coding ]
This commit is contained in:
119
README.md
119
README.md
@@ -172,6 +172,125 @@
|
||||
- [【较难】顺时针打印矩阵](./算法/剑指/数组和矩阵/printMatrix.js)
|
||||
|
||||
|
||||
### 栈队列堆
|
||||
|
||||
- [【中等】最小的k个数](./栈队列堆/GetLeastNumbers_Solution.js)
|
||||
- 用两个栈实现队列
|
||||
- 包含min函数的栈
|
||||
- 栈的压入、弹出序列
|
||||
- 最小的k个数
|
||||
- 数据流中的中位数
|
||||
- 字符流中的第一个不重复的字符
|
||||
- 滑动窗口的最大值
|
||||
|
||||
|
||||
### 双指针
|
||||
|
||||
- 和为S的两个数字
|
||||
- 和为S的连续正数序列
|
||||
- 翻转单词顺序列
|
||||
- 左旋转字符串
|
||||
|
||||
|
||||
### 链表
|
||||
|
||||
- 从尾到头打印链表
|
||||
- 在O(1)时间内删除链表节点
|
||||
- 删除链表中重复的结点
|
||||
- 链表中倒数第K个结点
|
||||
- 链表中环的入口结点
|
||||
- 反转链表
|
||||
- 合并两个排序的链表
|
||||
- 复杂链表的复制
|
||||
- 两个链表的第一个公共结点
|
||||
|
||||
|
||||
### 树
|
||||
|
||||
- 重建二叉树
|
||||
- 二叉树的下一个结点
|
||||
- 树的子结构
|
||||
- 二叉树的镜像
|
||||
- 对称的二叉树
|
||||
- 从上往下打印二叉树
|
||||
- 把二叉树打印成多行
|
||||
- 二叉搜索树的后续遍历序列
|
||||
- 二叉树中和为某一值的路劲
|
||||
- 二叉搜索树和双向链表
|
||||
- 序列化二叉树
|
||||
- 二叉查找树的第K个结点
|
||||
- 二叉树的深度
|
||||
- 平衡二叉树
|
||||
- 树中两个节点的最低公共祖先
|
||||
|
||||
|
||||
### 贪心思想
|
||||
|
||||
- 剪绳子
|
||||
- 股票的最大利润
|
||||
|
||||
|
||||
### 二分查找
|
||||
|
||||
- 旋转数组的最小数字
|
||||
- 数字在排序数组中出现的次数
|
||||
|
||||
|
||||
### 分治
|
||||
|
||||
- 数值的整数次方
|
||||
|
||||
|
||||
### 搜索
|
||||
|
||||
- 矩阵中的路劲
|
||||
- 机器人的运动范围
|
||||
- 字符串的排列
|
||||
|
||||
|
||||
### 排列
|
||||
|
||||
- 调整数组顺序使奇数位于偶数前面
|
||||
- 把数组排成最小的数
|
||||
- 数组中的逆序对
|
||||
|
||||
### 动态规划
|
||||
|
||||
- 斐波拉契数列
|
||||
- 矩形覆盖
|
||||
- 跳台阶
|
||||
- 变态跳台阶
|
||||
- 连续子数组的最大和
|
||||
- 礼物的最大价值
|
||||
- 最长不含重复字符的子字符串
|
||||
- 丑数
|
||||
- n个骰子的点数
|
||||
- 构建乘积数组
|
||||
|
||||
|
||||
### 数学
|
||||
|
||||
- 数组中出现次数超过一半的数字
|
||||
- 圆圈中最后剩下的数
|
||||
- 从1到n整数中1出现的次数
|
||||
|
||||
### 位运算
|
||||
|
||||
- 二进制中1的个数
|
||||
- 数组中只出现一次的数字
|
||||
|
||||
### 其他
|
||||
|
||||
- 打印从 1 到最大的 n 位数
|
||||
- 正则表达式匹配
|
||||
- 表示数值的字符串
|
||||
- 数字序列中的某一位数字
|
||||
- 把数字翻译成字符串
|
||||
- 扑克牌顺子
|
||||
- 求 1+2+3+...+n
|
||||
- 不用加减乘除做加法
|
||||
- 把字符串转换成整数
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
BIN
算法/.DS_Store
vendored
BIN
算法/.DS_Store
vendored
Binary file not shown.
44
算法/后端/FindContinuousSequence.js
Normal file
44
算法/后端/FindContinuousSequence.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-21 22:13:04
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-22 07:43:10
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// 指针矛盾了
|
||||
function FindContinuousSequence (sum) {
|
||||
// write code here
|
||||
|
||||
// 首先,序列最少包含两位数,则 x+(x+1)<=sum ===> x<sum/2 暂且向上取整吧
|
||||
|
||||
let left=1,right=2;
|
||||
let temp_sum=3;
|
||||
let result=[]
|
||||
// < 确保序列不断,跳出玄幻时候,left ==right
|
||||
while(left<right){
|
||||
|
||||
// 求和
|
||||
if(temp_sum===sum){
|
||||
|
||||
}else if(temp_sum<sum){
|
||||
// 向右
|
||||
temp_sum-=left;
|
||||
left++;
|
||||
}else if(temp_sum>sum){
|
||||
// 向左
|
||||
}
|
||||
}
|
||||
|
||||
// 跳出循环
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
console.log(FindContinuousSequence(9))
|
||||
module.exports = {
|
||||
FindContinuousSequence: FindContinuousSequence
|
||||
};
|
||||
43
算法/后端/FindKthToTail.js
Normal file
43
算法/后端/FindKthToTail.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-18 21:59:54
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-18 22:00:33
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* function ListNode(x){
|
||||
* this.val = x;
|
||||
* this.next = null;
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
|
||||
*
|
||||
*
|
||||
* @param pHead ListNode类
|
||||
* @param k int整型
|
||||
* @return ListNode类
|
||||
*/
|
||||
function FindKthToTail (pHead, k) {
|
||||
// write code here
|
||||
// 结点不存在返回空
|
||||
if (!pHead) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let arr = [];
|
||||
// 结点遍历,将结点存放在数组中
|
||||
while (pHead) {
|
||||
arr.push(pHead)
|
||||
pHead = pHead.next;
|
||||
}
|
||||
|
||||
return arr[arr.length - k]
|
||||
}
|
||||
module.exports = {
|
||||
FindKthToTail: FindKthToTail
|
||||
};
|
||||
51
算法/后端/FindNumbersWithSum.js
Normal file
51
算法/后端/FindNumbersWithSum.js
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-21 21:48:10
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-21 22:11:21
|
||||
*/
|
||||
|
||||
|
||||
// 注意array是递增的
|
||||
function FindNumbersWithSum (array, sum) {
|
||||
// write code here
|
||||
|
||||
let left = 0;
|
||||
let right = array.length - 1;
|
||||
|
||||
let sumReuslt = [];
|
||||
// 定义无穷大Infinity 或者直接sum值的平方
|
||||
let min = Math.pow(sum, 2)
|
||||
// let min=Infinity
|
||||
while (left < right) {
|
||||
let temp_sum = array[left] + array[right];
|
||||
if (temp_sum < sum) {
|
||||
// 向右
|
||||
left++
|
||||
} else if (temp_sum > sum) {
|
||||
// 向左
|
||||
right--
|
||||
} else if (temp_sum === sum) {
|
||||
// 已找到符合条件的元素,需要对齐进行乘积比较
|
||||
if (min > array[left] * array[right]) {
|
||||
// 假定为最小值
|
||||
min= array[left] * array[right]
|
||||
sumReuslt = [array[left], array[right]]
|
||||
}
|
||||
// left向右
|
||||
left++
|
||||
// right向左
|
||||
right--
|
||||
}
|
||||
}
|
||||
// left===right 退出
|
||||
return sumReuslt
|
||||
|
||||
}
|
||||
|
||||
console.log(FindNumbersWithSum([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],21))
|
||||
module.exports = {
|
||||
FindNumbersWithSum: FindNumbersWithSum
|
||||
};
|
||||
82
算法/后端/GetLeastNumbers_Solution.js
Normal file
82
算法/后端/GetLeastNumbers_Solution.js
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* @Description: 给定一个数组,找出其中最小的K个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。如果K>数组的长度,那么返回一个空的数组
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-17 23:00:02
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-17 23:27:03
|
||||
*/
|
||||
|
||||
|
||||
// 基于冒泡排序
|
||||
function GetLeastNumbers_Solution (input, k) {
|
||||
// write code here
|
||||
|
||||
const len = input.length
|
||||
|
||||
if (k > len) {
|
||||
return []
|
||||
}
|
||||
|
||||
// 冒泡,跑k趟,每趟就有一个元素在位置上 需要输出前k,就跑k趟
|
||||
for (let i = 0; i < k; i++) {
|
||||
for (let j = len; j >i; j--) {
|
||||
if (input[i] >= input[j]) {
|
||||
// 元素换位置
|
||||
const temp = input[j]
|
||||
input[j] = input[i];
|
||||
input[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return input.slice(0, k)
|
||||
}
|
||||
|
||||
// 基于简单选择排序
|
||||
function GetLeastNumbers_Solution02 (input, k) {
|
||||
// write code here
|
||||
|
||||
const len = input.length
|
||||
|
||||
if (k > len) {
|
||||
return []
|
||||
}
|
||||
|
||||
// 冒简单选择排序,每趟就有一个元素在位置上 需要输出前k,就跑k趟
|
||||
for (let i = 0; i < k; i++) {
|
||||
for (let j = i + 1; j < len; j++) {
|
||||
if (input[i] >= input[j]) {
|
||||
// 元素换位置
|
||||
const temp = input[j]
|
||||
input[j] = input[i];
|
||||
input[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return input.slice(0, k)
|
||||
}
|
||||
|
||||
// 基于sort函数
|
||||
function GetLeastNumbers_Solution03(input, k) {
|
||||
// write code here
|
||||
// if (k > input.length) return []
|
||||
|
||||
// // 排序
|
||||
// input.sort((a,b)=>a-b)
|
||||
// return input.slice(0, k)
|
||||
|
||||
return k>input.length?[]: input.sort((a,b)=>a-b).slice(0,k)
|
||||
}
|
||||
|
||||
|
||||
console.log(GetLeastNumbers_Solution([4, 5, 1, 6, 2, 7, 3, 8], 4))
|
||||
console.log(GetLeastNumbers_Solution02([4, 5, 1, 6, 2, 7, 3, 8], 4))
|
||||
console.log(GetLeastNumbers_Solution03([4, 5, 1, 6, 2, 7, 3, 8], 4))
|
||||
|
||||
module.exports = {
|
||||
GetLeastNumbers_Solution: GetLeastNumbers_Solution,
|
||||
GetLeastNumbers_Solution02: GetLeastNumbers_Solution02,
|
||||
GetLeastNumbers_Solution03: GetLeastNumbers_Solution03
|
||||
};
|
||||
41
算法/后端/MoreThanHalfNum_Solution.js
Normal file
41
算法/后端/MoreThanHalfNum_Solution.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-18 11:58:01
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-18 12:07:04
|
||||
*/
|
||||
|
||||
|
||||
function MoreThanHalfNum_Solution (numbers) {
|
||||
|
||||
// write code here
|
||||
let map = new Map();
|
||||
|
||||
numbers.forEach(item => {
|
||||
if (map.has(item)) {
|
||||
map.set(item, map.get(item) + 1)
|
||||
} else {
|
||||
map.set(item, 1)
|
||||
}
|
||||
})
|
||||
|
||||
const arr = [...new Set(numbers)];
|
||||
|
||||
// console.log(map,arr)
|
||||
let result = 0;
|
||||
|
||||
arr.map(item => {
|
||||
if (2 * map.get(item) > numbers.length) {
|
||||
result = item;
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
}
|
||||
|
||||
console.log()
|
||||
110
算法/后端/binary_search.js
Normal file
110
算法/后端/binary_search.js
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* @Description: 二分查找
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-18 19:01:13
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-18 21:29:48
|
||||
*/
|
||||
|
||||
// 参考:https://github.com/labuladong/fucking-algorithm/blob/master/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E8%AF%A6%E8%A7%A3.md
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @param {number} target
|
||||
* @return {number}
|
||||
*/
|
||||
var search = function(nums, target) {
|
||||
|
||||
// 投机
|
||||
// return nums.indexOf(target)
|
||||
|
||||
// 二分查找
|
||||
return binary_search(nums,target)
|
||||
|
||||
};
|
||||
|
||||
// 二分查找
|
||||
function binary_search(nums,target){
|
||||
let left=0,right=nums.length;
|
||||
while(left<right){
|
||||
// 注意js取整问题;
|
||||
let mid=left+parseInt((right-left)/2);
|
||||
console.log(mid,nums)
|
||||
if(nums[mid]===target){
|
||||
return mid
|
||||
}else if(nums[mid]>target){
|
||||
// 左侧
|
||||
right=mid
|
||||
}else if(nums[mid]<target){
|
||||
// 右侧
|
||||
left=mid+1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// 左侧部分【第一个相同元素】
|
||||
function left_bound(nums,target){
|
||||
|
||||
let left=0,right=nums.length-1;
|
||||
|
||||
// [left,right]
|
||||
while(left<=right){
|
||||
// 取中间元素
|
||||
let mid=left+ Math.floor((right-left)/2);
|
||||
|
||||
if(nums[mid]===target){
|
||||
right=mid-1
|
||||
}else if(nums[mid]<target){
|
||||
// 右侧
|
||||
left=mid+1
|
||||
}else if(nums[mid]>target){
|
||||
// 左侧
|
||||
right=mid-1
|
||||
}
|
||||
|
||||
// 跳出循环,left=right+1 索引
|
||||
|
||||
return left===nums.length?left:-1
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 右侧部分【最后一个相同元素】,[left,right) 情况
|
||||
function right_bound(nums,target){
|
||||
let left=0,right=nums.length
|
||||
// [left,right) 情况
|
||||
while(left<right){
|
||||
let mid=left+Math.floor((right-left)/2);
|
||||
console.log(mid)
|
||||
if(nums[mid]===target){
|
||||
// 往右
|
||||
left=mid+1
|
||||
}else if(nums[mid]<target){
|
||||
// 右侧
|
||||
left=mid+1
|
||||
}else if(nums[mid]>target){
|
||||
// 左侧
|
||||
right=mid
|
||||
}
|
||||
}
|
||||
console.log(left,right);
|
||||
|
||||
// left===right 跳出循环
|
||||
return nums[left-1]===target?left-1:-1
|
||||
}
|
||||
|
||||
|
||||
|
||||
// console.log(search([-1,0,3,5,9,12],9))
|
||||
|
||||
// console.log(search([5,7,7,8,8,8,10],8))
|
||||
|
||||
// console.log(left_bound([5,7,7,8,8,8,10],8))
|
||||
console.log(right_bound([5,7,7,8,8,8,10],8))
|
||||
78
算法/后端/findKth.js
Normal file
78
算法/后端/findKth.js
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-19 20:27:28
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-19 21:46:21
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a int整型一维数组
|
||||
* @param n int整型
|
||||
* @param K int整型
|
||||
* @return int整型
|
||||
*/
|
||||
function findKth (a, n, K) {
|
||||
// write code here
|
||||
|
||||
|
||||
const result = quickSort(a, 0, n - 1);
|
||||
|
||||
console.log(result)
|
||||
return result[K - 1]
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
function quickSort (arr, low, high) {
|
||||
|
||||
//
|
||||
while (low < high) {
|
||||
|
||||
// 中间区分值
|
||||
let pivot = getPivot(arr, low, high);
|
||||
console.log(`pivot:${pivot}`)
|
||||
|
||||
// 左侧
|
||||
quickSort(arr, low, pivot - 1)
|
||||
// 右侧
|
||||
quickSort(arr, pivot + 1, high)
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
function getPivot (arr, low, high) {
|
||||
|
||||
let pivot = arr[low];
|
||||
|
||||
while (low < high) {
|
||||
|
||||
// pivot右侧元素都比arr[pivot]值大
|
||||
while (low < high && pivot <= arr[high]) {
|
||||
--high;
|
||||
}
|
||||
arr[low] = arr[high]
|
||||
|
||||
// pivot左侧元素都比arr[pivot]值小
|
||||
while (low < high && pivot >= arr[low]) {
|
||||
++low
|
||||
}
|
||||
arr[high] = arr[low]
|
||||
|
||||
}
|
||||
arr[low] = pivot
|
||||
|
||||
return low;
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.log(findKth([1, 3, 5, 2, 2], 5, 3))
|
||||
|
||||
module.exports = {
|
||||
findKth: findKth
|
||||
};
|
||||
23
算法/后端/removeElement.js
Normal file
23
算法/后端/removeElement.js
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-18 20:21:43
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-18 20:22:20
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @param {number} val
|
||||
* @return {number}
|
||||
*/
|
||||
var removeElement = function(nums, val) {
|
||||
|
||||
// 等于val值的个数
|
||||
return nums.filter(item=>item!==val)
|
||||
|
||||
};
|
||||
|
||||
|
||||
console.log(removeElement([3,2,2,3],3))
|
||||
51
算法/后端/threeNum.js
Normal file
51
算法/后端/threeNum.js
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @Description:
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-21 21:33:14
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-21 21:38:32
|
||||
*/
|
||||
|
||||
|
||||
|
||||
function threeNum(num){
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 暴力枚举,无法通过
|
||||
function threeSum01( num ) {
|
||||
// write code here
|
||||
// 从小排序
|
||||
num=num.sort((a,b)=>a-b);
|
||||
const len=num.length;
|
||||
let result=[];
|
||||
for(let mid=1;mid<len-1;mid++){
|
||||
|
||||
// 查找左边
|
||||
for(let left=0;left<mid;left++){
|
||||
|
||||
// 查找右边
|
||||
|
||||
for(let right=mid+1;right<len;right++){
|
||||
if(num[left]+num[mid]+num[right]===0){
|
||||
result.push([num[left],num[mid],num[right]])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 返回
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
console.log(threeSum([-2,0,1,1,2]))
|
||||
module.exports = {
|
||||
threeSum : threeSum
|
||||
};
|
||||
56
算法/后端/twoSum.js
Normal file
56
算法/后端/twoSum.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* @Description: 给出一个整数数组,请在数组中找出两个加起来等于目标值的数
|
||||
* @Version: Beta1.0
|
||||
* @Author: 【B站&公众号】Rong姐姐好可爱
|
||||
* @Date: 2021-04-17 23:31:18
|
||||
* @LastEditors: 【B站&公众号】Rong姐姐好可爱
|
||||
* @LastEditTime: 2021-04-18 10:05:27
|
||||
*/
|
||||
|
||||
// 利用左右双指针
|
||||
function twoSum (numbers, target) {
|
||||
// write code here
|
||||
|
||||
const len = numbers.length
|
||||
|
||||
// 从头开始
|
||||
for (let left = 0; left < len; left++) {
|
||||
for (let right = len - 1; right > left; right--) {
|
||||
if (numbers[left] + numbers[right] === target) {
|
||||
return [left + 1, right + 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 利用map对象来存储已经遍历的数据
|
||||
|
||||
function twoSum02 (numbers, target) {
|
||||
|
||||
// 从左到右循环,进入map
|
||||
let map = new Map();
|
||||
|
||||
for (let left = 0; left < numbers.length; left++) {
|
||||
|
||||
|
||||
if (map.has(target - numbers[left]) && map.get(target - numbers[left]) !== (left + 1)) {
|
||||
|
||||
// 则在左边找到元素
|
||||
const right = map.get(target - numbers[left]);
|
||||
|
||||
console.log(map)
|
||||
return [right, left + 1]
|
||||
}
|
||||
// 有点倒排索引的意思
|
||||
map.set(numbers[left], left + 1)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
console.log(twoSum([3,2,4],6))
|
||||
console.log(twoSum02([0, 4, 3, 0], 0))
|
||||
module.exports = {
|
||||
twoSum: twoSum
|
||||
};
|
||||
Reference in New Issue
Block a user