楼主: Myron2017
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖子

🔗
 楼主| Myron2017 2021-2-5 09:45:24 | 只看该作者
全局:
594        Longest Harmonious Subsequence

一个小的提高技巧,key-1 和 key+1 其实只需要考虑一边即可,因为对称的。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-6 23:47:20 | 只看该作者
全局:
199. Binary Tree Right Side View

Level Transversal 的变体。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-7 14:12:57 | 只看该作者
全局:
71 Simplify Path

split('/') will split any mupltiple '/' not only single '/'


这个题目,很简单,但是很好的联系 Python string 惯用法的题目。值得记忆背诵。

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-9 06:46:54 | 只看该作者
全局:
284. Peeking Iterator

Notice how to use an iterator, this is a modified class of normal iterator

The Key Thing is == > Using O(1) Space and O(1) time to finish.

Notice we can reuse iterator and there is no need to get the list of the input content.


最好的解法是,复用原始数据提供的 iterator 而不是自己把数据存下来!

回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-9 07:18:33 | 只看该作者
全局:
821 Shortest Distance to a Character

Two pass:
(1) go from left to right: always using the rightside C
(2) go from right to left: always using the leftside C
And if we do not know the position of C, put it float('inf')
(3) Finally, pick the smaller one


https://leetcode.com/problems/sh ... character/solution/



不过这个视频的解法也很好,很有参考价值,从当前字符的 pos 和 c_pos list 的相对关系来分类,精彩!


回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-10 14:55:41 | 只看该作者
全局:
本帖最后由 Myron2017 于 2021-2-10 15:04 编辑

538 Convert BST to Greater Tree

还是要保持年轻状态,保持学习啊!这道题目其实就是 Tree 的遍历的变体,但是确实很新颖,答案真是巧妙!

使用右根左的顺序遍历,遍历的时候记录所有经过节点的总和,更新当前节点

https://maxming0.github.io/2021/ ... ST-to-Greater-Tree/



当然也可以不用递归,

Approach #2 Iteration with a Stack [Accepted]



官方解答给了很好的一个解法,这里记录下,https://leetcode.com/problems/co ... ater-tree/solution/



回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-11 14:14:45 | 只看该作者
全局:
138        Copy List with Random Pointer        Medium       

Recursion to do deepCopy


首先肯定是可以用 3 Pass 来做,就是三遍遍历的。

但是更加巧妙的方法其实是,用 Recursion。Code 值得背诵。

next 一定要在 random 前面,只有这样才能让 dict 不坐蜡。当然这个只需要 2 pass,但是相对的牺牲了空间。


回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-12 00:45:42 | 只看该作者
全局:
242. Valid Anagram

Easy 题,一个解法是 sorting 一个是 Hashtable。
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-13 02:05:21 | 只看该作者
全局:
Easy 题, 1342 Number of Steps to Reduce a Number to Zero
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-2-16 06:15:25 | 只看该作者
全局:
1337 The K Weakest Rows in a Matrix

很重要的优化是如何 解决这种 binary martix 里面,前面都是 1 后面都是 0 的情况。

其实只要修改下 heap 的代码就行,这里只要计算前面的1 省略后面的 0 即可。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表