![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
数据结构与算法
写出高效的代码
刘景照
学习、实践、改变。
展开
-
【数据结构入门】基于对象实现队列
// 基于对象实现class Queue { constructor() { this.queue = {}; this.head = 0; this.foot = 0; } enqueue(item) { this.queue[this.foot] = item; this.foot++; } dequeue() { if (this.isEmpty()) { return; } const val.原创 2022-05-13 09:22:36 · 145 阅读 · 0 评论 -
【LeetCode】739.每日温度,今天温度升高了吗
暴力破解var dailyTemperatures = function(T) { let res = [] for(let i = 0; i < T.length; i++) { const curr = T[i]; for (let j = i + 1; j < T.length; j++) { if (curr < T[j]) { res.push(j - i); break;原创 2022-05-11 18:00:03 · 154 阅读 · 0 评论 -
【leetcode】155. 最小栈
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。原创 2022-05-10 15:11:18 · 295 阅读 · 0 评论 -
【leetcode】面试题 03.02. 栈的最小值
//leetcode https://leetcode.cn/problems/min-stack-lcci/// 请设计一个栈,除了常规栈支持的pop与push函数以外,还支持min函数,该函数返回栈元素中的最小值。// 执行push、pop和min操作的时间复杂度必须为O(1)。// 示例:// MinStack minStack = new MinStack();// minStack.push(-2);// minStack.push(0);// minStack.push(-3);原创 2022-05-10 09:19:47 · 91 阅读 · 0 评论 -
【数据结构入门】如果你学不会数据结构,至少看看这个栈
js 使用数组乞丐版实现栈原创 2022-05-09 23:01:48 · 362 阅读 · 0 评论 -
【简单算法练习】爱因斯坦的阶梯
爱因斯坦曾出过这样一道有趣的数学题:有一个长阶梯,若每步上2阶,最后剩1阶;若每步上3阶,最后剩2阶;若每步上5阶,最后剩4阶;若每步上6阶,最后剩5阶;只有每步上7阶,最后刚好一阶也不剩【分析】我们假设最后的阶梯数是n。每步上2阶,最后剩1阶,可知 n 除以 2 余数是 1。每步上3阶,最后剩2阶,可知 n 除以 3 余数是2 。其余可知: n 除以 5 余数是 4 , n除以 6 余数是 5, n 除以 7余数是 0;你可能立马就能想到,循环查找符合以上条件的数字#include <原创 2021-07-02 09:50:41 · 2329 阅读 · 0 评论 -
排序算法-冒泡排序
var Arr = [3, 5, 74, 64, 64, 3, 1, 8, 3, 49, 16, 161, 9, 4]console.log(Arr, "before");bubbleSort(Arr)console.log(Arr, "after");function bubbleSort(){ let len = Arr.length; for (let i = 0; i < len; i++) { for (let j = 0; j < le原创 2021-05-27 17:29:00 · 52 阅读 · 0 评论 -
排序算法-快速排序
function partition(arr,left,right){ let pivot = arr[right]; let index = left; for (let i = left; i < right; i++) { if(arr[i] < pivot){ swap(arr,index++,i); } } swap(arr, index, right); return index;}function swap(arr,i,原创 2021-05-27 17:01:49 · 66 阅读 · 0 评论 -
排序算法-简易桶排序
function sorted(arr){ let result = new Array(11).fill(0); let res = []; for(let i=0; i< arr.length; i++){ result[arr[i]] += 1; } for(let i=0; i< result.length;i++){ for (let j = 0; j < result[i]; j++) { if(result[i] != 0)原创 2021-05-26 17:46:48 · 50 阅读 · 0 评论 -
字符串匹配
function searchStr(str,c){ let len = str.length; let j = 0; for(let i = 0; i < len;i++ ){ if(str[i] != c[j]){ i = i - j; j = 0; continue; }else{ j++; if(j>=c.length){ ret原创 2021-05-26 10:55:20 · 66 阅读 · 0 评论 -
[LeetCode] 453. 最小操作次数使数组元素相等
给定一个长度为 n 的 非空 整数数组,每次操作将会使 n - 1 个元素增加 1。找出让数组所有元素相等的最小操作次数。想了3分钟都要看题解了,先把这个写出来试试第一种:将 1 添加到除最大元素之外的所有元素,并增加移动数的计数var minMoves = function (nums) { let result = 0; while(Math.min(...nums) !== Math.max(...nums)){ let max = Math.max(...nums);原创 2021-04-29 09:36:05 · 101 阅读 · 0 评论 -
记录Leecode刷题过程
一. 数组数组的遍历统计数组中的元素数组的改变、移动453665283二维数组及滚动数组 118、119、661、598、419数组的旋转 189、396特定顺序遍历二维数组 54、59、498二维数组变换 566、48、73、289前缀和数组 303、304、238原文:有没有人一起从零开始刷力扣...原创 2021-04-28 15:54:26 · 93 阅读 · 0 评论 -
【freeCodeCamp Intermediate】Convert HTML Entities
Convert the characters &, <, >, " (double quote), and ’ (apostrophe), in a string to their corresponding HTML entities.function convertHTML(str) { let result = '', map = new Map([ ['&', '&'], ['<', '<'], [原创 2021-04-26 18:33:17 · 102 阅读 · 0 评论 -
【freeCodeCamp Intermediate】Missing letters 丢失的字母
Find the missing letter in the passed letter range and return it.If all letters are present in the range, return undefined.第一种:function fearNotLetter(str) { let result = undefined; let aaa = 'abcdefghijklmnopqrstuvwxyz' let start = aaa.indexOf(str原创 2021-04-26 16:36:24 · 147 阅读 · 0 评论 -
【freeCodeCamp Intermediate】DNA Pairing DNA配对
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.Base pairs are a pair of AT and CG. Match the missing element to the provided character.Return the provided character as the first elem原创 2021-04-26 16:09:07 · 111 阅读 · 0 评论 -
【freeCodeCamp Intermediate】Search and Replace 查找和替换
Perform a search and replace on the sentence using the arguments provided and return the new sentence.First argument is the sentence to perform the search and replace on.Second argument is the word that you will be replacing (before).Third argument is w原创 2021-04-26 15:48:37 · 149 阅读 · 0 评论 -
【freeCodeCamp Intermediate】Seek and Destroy 查找和消灭
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.Note: You have to use the arguments obje原创 2021-04-25 14:58:26 · 79 阅读 · 0 评论 -
【freeCodeCamp base】Chunky Monkey 拆分数组
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.分割数组:将一个数组(第一个参数)分割成一组长度为 size(第二个参数)的数组,然后在一个二维数组中返回这些结果。function chunkArrayInGroups(arr, size) { if (s原创 2021-04-25 10:29:26 · 127 阅读 · 0 评论 -
怎么刻意练习数据结构和算法
第一遍第一步:尽可能的想出所有解法(用时5-10分钟),想不出来看题解吧,不要挣扎了第二步:分析情况,选择最优第三步:写出解答第四步:自己多举例子验证(例子很重要,临界情况,各种情况,例子举的好能检测出来程序中不对的地方)第五步:提交第二遍第一步:提交后可以查看自己前面效率高的代码,抄笔记第二步:中文版的解题思路看看,官方的,个人的都看看第三步:现在有各种版本了,自己不能理解的,能不能理解。自己没想到的,能不能记住第四步:内化成自己的(不管是背,还是使劲背,或者玩命背,反正都变成自己的)原创 2021-04-21 22:17:23 · 1384 阅读 · 0 评论 -
【JavaScript实现LeetCode】 283. 移动零
第一步:尽可能的想出所有解法(用时5-10分钟),想不出来看题解吧,不要挣扎了第二步:分析情况,选择最优第三步:写出解答第四步:自己多举例子验证(例子很重要,临界情况,各种情况,例子举的好能检测出来程序中不对的地方)第一种:思路:循环所有元素,遇到0把当前0删掉,数组最后添加上0细节:1.我们用n统计最后添加了几个02.循环的时候,i < array.length-n, 不循环最后添加的03.因为我们获取当前元素用的array[i],所以删除掉当前为0的数时,后一位会前移,所以有.原创 2021-04-21 16:38:57 · 84 阅读 · 1 评论