diff --git a/.DS_Store b/.DS_Store index c13e1a3..bd56ab4 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 9828082..7c1b947 100644 --- a/README.md +++ b/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 +- 不用加减乘除做加法 +- 把字符串转换成整数 + diff --git a/算法/.DS_Store b/算法/.DS_Store index 0750feb..932df2c 100644 Binary files a/算法/.DS_Store and b/算法/.DS_Store differ diff --git a/算法/后端/FindContinuousSequence.js b/算法/后端/FindContinuousSequence.js new file mode 100644 index 0000000..bd39806 --- /dev/null +++ b/算法/后端/FindContinuousSequence.js @@ -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 ===> xsum){ + // 向左 + } + } + + // 跳出循环 + return result; + +} + +console.log(FindContinuousSequence(9)) +module.exports = { + FindContinuousSequence: FindContinuousSequence +}; \ No newline at end of file diff --git a/算法/后端/FindKthToTail.js b/算法/后端/FindKthToTail.js new file mode 100644 index 0000000..815867a --- /dev/null +++ b/算法/后端/FindKthToTail.js @@ -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 +}; \ No newline at end of file diff --git a/算法/后端/FindNumbersWithSum.js b/算法/后端/FindNumbersWithSum.js new file mode 100644 index 0000000..9f1a8e2 --- /dev/null +++ b/算法/后端/FindNumbersWithSum.js @@ -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 +}; \ No newline at end of file diff --git a/算法/后端/GetLeastNumbers_Solution.js b/算法/后端/GetLeastNumbers_Solution.js new file mode 100644 index 0000000..63e56b8 --- /dev/null +++ b/算法/后端/GetLeastNumbers_Solution.js @@ -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 +}; diff --git a/算法/后端/MoreThanHalfNum_Solution.js b/算法/后端/MoreThanHalfNum_Solution.js new file mode 100644 index 0000000..2f88fcf --- /dev/null +++ b/算法/后端/MoreThanHalfNum_Solution.js @@ -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() \ No newline at end of file diff --git a/算法/后端/binary_search.js b/算法/后端/binary_search.js new file mode 100644 index 0000000..017d968 --- /dev/null +++ b/算法/后端/binary_search.js @@ -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(lefttarget){ + // 左侧 + right=mid + }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(lefttarget){ + // 左侧 + 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)) \ No newline at end of file diff --git a/算法/后端/findKth.js b/算法/后端/findKth.js new file mode 100644 index 0000000..9329b23 --- /dev/null +++ b/算法/后端/findKth.js @@ -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 +}; \ No newline at end of file diff --git a/算法/后端/removeElement.js b/算法/后端/removeElement.js new file mode 100644 index 0000000..cb0d9b4 --- /dev/null +++ b/算法/后端/removeElement.js @@ -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)) \ No newline at end of file diff --git a/算法/后端/threeNum.js b/算法/后端/threeNum.js new file mode 100644 index 0000000..81bdda1 --- /dev/null +++ b/算法/后端/threeNum.js @@ -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 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 +}; \ No newline at end of file