source code
1 | // Runtime: 68 ms, faster than 23.67% of JavaScript online submissions for Generate Parentheses. |
test cases
1 | test("test1", () => { |
前端小天才智多星班进修中
1 | // Runtime: 68 ms, faster than 23.67% of JavaScript online submissions for Generate Parentheses. |
1 | test("test1", () => { |
思路:DFS
时间复杂度:O(n)
1 | // Runtime: 80 ms, faster than 17.32% of JavaScript online submissions for Minimum Depth of Binary Tree. |
思路:BFS
时间复杂度:O(n)
1 | // Runtime: 72 ms, faster than 21.72% of JavaScript online submissions for Minimum Depth of Binary Tree. |
1 | test("test1", () => { |
思路:DFS + recursive
时间复杂度:O(n)
1 | // Runtime: 68 ms, faster than 33.83% of JavaScript online submissions for Maximum Depth of Binary Tree. |
思路:BFS + iterative
时间复杂度:O(n)
1 | // Runtime: 88 ms, faster than 15.71% of JavaScript online submissions for Maximum Depth of Binary Tree. |
1 | test("test1", () => { |
思路:BFS
时间复杂度:O(n)
1 | // Runtime: 72 ms, faster than 19.61% of JavaScript online submissions for Binary Tree Level Order Traversal. |
思路:和方法一的思路类似,也是通过层级计数,不过通过DFS实现
时间复杂度:O(n)
1 | // Runtime: 68 ms, faster than 21.98% of JavaScript online submissions for Binary Tree Level Order Traversal. |
思路:BFS + Batch Process
时间复杂度:O(n)
1 | // Runtime: 64 ms, faster than 30.81% of JavaScript online submissions for Binary Tree Level Order Traversal. |
1 | test("test1", () => { |
1 | // Runtime: 80 ms, faster than 33.60% of JavaScript online submissions for Best Time to Buy and Sell Stock. |
1 | test("test1", () => { |
这道题的思路是:因为能买卖无数次,只要能赚钱(价格比前一天高),就尽快卖出。能同一天进行买卖。
1 | // Runtime: 84 ms, faster than 27.73% of JavaScript online submissions for Best Time to Buy and Sell Stock II. |
1 | test("test1", () => { |
1 | // Runtime: 2064 ms, faster than 5.19% of JavaScript online submissions for Best Time to Buy and Sell Stock III. |
1 | test("test1", () => { |
阿里笔试中的一道题,一眼看上去挺简单,但是仔细想想有挺多地方要注意的。。
请实现一个Person类,且拥有private的成员变量及函数,public的成员变量及函数
提示:参考ts实现
1 | class Person { |
第一眼看到这道题的时候马上想到的是最简单的实现,用约定俗成的方法,给私有变量/方法名前缀加下划线:
1 | const Person = function(name, address) { |
法一只是一种约定,外部还是能通过_name
取到私有属性。然后想到通过Object.defineProperty
或者Proxy
拦截私有属性的访问,于是写了一个版本:
1 | const Person = function (name, address) { |
这个方法能屏蔽外部对私有属性的访问,但是内部调用必须经过_safeGet
,和题目要求有微妙的区别。。而且某些浏览器不支持Proxy。
相当于方法二的简单实现,用闭包屏蔽外部访问,还是没解决内部调用的问题:
1 | const Person = function (name, address) { |
通过原型链屏蔽私有属性的访问,这个解法是符合题目要求的,但是是否是最优解不得而知:
1 | const Person = function (name, address) { |
这道题考察的是滑动窗口。
时间复杂度:O(n)
1 | // Runtime: 76 ms, faster than 14.67% of JavaScript online submissions for Remove Nth Node From End of List. |
时间复杂度:O(n)
比解法一优化了空间复杂度,只保留了窗口的头尾指针。
1 | // Runtime: 96 ms, faster than 8.34% of JavaScript online submissions for Remove Nth Node From End of List. |
1 | test("test1", () => { |
加了contenteditable的div默认是没有placeholder的,下面尝试用css实现一个placeholder的效果:
1 | <div class="richTextBox empty" contenteditable placeholder="请输入"></div> |
1 | .richTextBox { |
效果图:
使用一个 div,不借助 js 能力的前提下,纯 css 实现一个类似如下图的效果:
hover上去的时候变化。
印象中这道题在当年阿里校招的笔试上见过,但是很多年没写css了,很多知识点忘的差不多了,答得不算好。下面总结下:
1 | .switch { |
1 | <div class="switch"></div> |
关于transition这篇文章总结得不错,可以看看:
深入理解CSS过渡transition。
和checkbox结合实现点击动画
1 | .switch { |
1 | <label class="switch"> |
思路:brute force,循环嵌套依次找出每个元素出现的次数;
时间复杂度:O(n^2)
思路:用map计数,找出出现次数>长度一半的元素
时间复杂度:O(n)
1 | // Runtime: 120 ms, faster than 5.64% of JavaScript online submissions for Majority Element. |
思路:先把数组排序,众数一定在中间的位置
时间复杂度:O(n*logn)
1 | // Runtime: 84 ms, faster than 10.46% of JavaScript online submissions for Majority Element. |
思路:分治。把数组一分为二,找出前半段的众数以及众数出现的次数(lm, lc),后半段的众数以及众数出现的次数(rm, rc)。如果lm==lc,则众数为lm;否则比较lc和rc,返回较大的对应lm/rm。
时间复杂度:O(logn+n)
1 | var majorityElement = function (nums, start = 0, end) { |
1 | test("test1", () => { |
tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true