• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

随笔分类 -  Leetcode

上一页 1 ··· 22 23 24 25 26 27 28 29 30 ··· 32 下一页
Leetcode: Largest Rectangle in Histogram

摘要:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the hist... 阅读全文
posted @ 2014-09-22 01:10 neverlandly 阅读(491) 评论(0) 推荐(0)
Leetcode: Sum Root to Leaf Numbers

摘要:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which... 阅读全文
posted @ 2014-09-21 06:47 neverlandly 阅读(477) 评论(0) 推荐(0)
Leetcode: LRU Cache

摘要:难度:90。这是一道非常综合的题目,主要应用在操作系统的资源管理中。 按照题目要求,要实现get和set功能,为了满足get功能:随机访问的需求(lookup)我们首先想到的一般是用数组,如果用链表会有O(n)的访问时间。然而他又有另一个条件就是要维护least used的队列,也就是说经常用的放在 阅读全文
posted @ 2014-09-21 05:31 neverlandly 阅读(720) 评论(0) 推荐(0)
Leetcode: Candy

摘要:贪心法 复杂度 时间 O(N) 空间 O(N) 思路 典型的贪心法,如果一个孩子比另一个孩子的分高,我们只多给1块糖。我们可以先从左往右遍历,确保每个孩子根他左边的孩子相比,如果分高,则糖要多1个,如果分比左边低,就只给一颗。然后我们再从右往左遍历,确保每个孩子跟他右边的孩子相比,如果分高则糖至少多 阅读全文
posted @ 2014-09-20 13:18 neverlandly 阅读(333) 评论(0) 推荐(0)
Leetcode: Interleaving String

摘要:难度:87. 这是一道关于字符串操作的题目,要求是判断一个字符串能不能由两个字符串按照他们自己的顺序,每次挑取两个串中的一个字符来构造出来。像这种判断能否按照某种规则来完成求是否或者某个量的题目,很容易会想到用动态规划来实现。 动态规划重点在于找到:维护量,递推式。维护量通过递推式递推,最后往往能得 阅读全文
posted @ 2014-09-20 12:26 neverlandly 阅读(379) 评论(0) 推荐(0)
Leetcode: Implement strStr()

摘要:暴力法 复杂度 时间 O(N^2) 空间 O(N)因为用了一个临时string存 思路 本题有很多高级算法可以在O(N)时间内解决问题,然而这已经超出面试的范畴。本题在面试中出现的作用就是考察基本的编程素养,以及边界条件的考虑。我们用暴力法即可。 若要用O(1)的space,就一个一个比: 推荐: 阅读全文
posted @ 2014-09-20 10:58 neverlandly 阅读(519) 评论(0) 推荐(0)
Leetcode: Gray Code

摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total numb... 阅读全文
posted @ 2014-09-20 07:15 neverlandly 阅读(425) 评论(0) 推荐(0)
Leetcode: Restore IP addresses

摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255... 阅读全文
posted @ 2014-09-20 06:48 neverlandly 阅读(399) 评论(0) 推荐(0)
Leetcode: Median of Two Sorted Arrays

摘要:2017/2/5更新:如果一定要每次扔一半,使得时间复杂度为O(log(m+n))。可以在第一个数组找前k1个,第二个数组找前k2个,使得k1+k2 == k, 分情况: 1. if A[k1] < B[k2], then A[k1]及之前的、B[k2+1]及之后的都不可能成为第k个元素,所以扔掉 阅读全文
posted @ 2014-09-20 04:55 neverlandly 阅读(535) 评论(0) 推荐(0)
Leetcode: Pow(x, n) and Summary: 负数补码总结

摘要:Analysis: Time Complexity: O(LogN) Iterative code: refer to https://discuss.leetcode.com/topic/40546/iterative-log-n-solution-with-clear-explanation N 阅读全文
posted @ 2014-09-20 00:32 neverlandly 阅读(987) 评论(0) 推荐(0)
Leetcode: Sort Colors

摘要:难度:naive做法60,计数排序 scan两次也是一种办法,第一次把1和2当做一种颜色,放到0的右边,第二次0已经全部在左边,只需把2放到1右边 我们考虑怎么用一次扫描来解决。 Best Solution(Discuss里vote最高): the basic idea is to use two 阅读全文
posted @ 2014-09-19 12:58 neverlandly 阅读(296) 评论(0) 推荐(0)
Leetcode: Sqrt(x)

摘要:Implement int sqrt(int x). 难度:76,用二分查找。要求是知道结果的范围,取定左界和右界,然后每次砍掉不满足条件的一半,知道左界和右界相遇。算法的时间复杂度是O(logx),空间复杂度是O(1)。 1 public class Solution { 2 public int 阅读全文
posted @ 2014-09-19 12:09 neverlandly 阅读(520) 评论(0) 推荐(0)
Leetcode: Palindrome Partitioning II

摘要:难度:90. 这道题跟Palindrome Partitioning非常类似,区别就是不需要返回所有满足条件的结果,而只是返回最小的切割数量就可以。做过Word Break的朋友可能马上就会想到,其实两个问题非常类似,当我们要返回所有结果(Palindrome Partitioning和Word B 阅读全文
posted @ 2014-09-19 09:08 neverlandly 阅读(577) 评论(0) 推荐(0)
Leetcode: Palindrome Partitioning

摘要:Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example,... 阅读全文
posted @ 2014-09-19 07:33 neverlandly 阅读(590) 评论(0) 推荐(0)
Leetcode: Valid Palindrome

摘要:第二次做法:用API 第一次做法:难度:79. 这道题是判断一个字符串是不是回文串。因为只是看一个字符串,算法还是比较简单,就是从两头出发,往中间走,进行两两匹配。这里面的小问题就是在这个题目要求中,只判断字母和数字类型的字符,其他字符直接跳过即可。因此我们要写一个函数判断他是不是合法字符,而且因为 阅读全文
posted @ 2014-09-19 05:36 neverlandly 阅读(461) 评论(0) 推荐(0)
Leetcode: Symmetric Tree

摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ ... 阅读全文
posted @ 2014-09-19 04:35 neverlandly 阅读(265) 评论(0) 推荐(0)
Leetcode: Validate Binary Search Tree

摘要:需要保证的是左子树所有节点都小于根,而并非仅仅左子树根。所以Recursion里面要存的是可以取值的范围。其实就是对于每个结点保存左右界,也就是保证结点满足它的左子树的每个结点比当前结点值小,右子树的每个结点比当前结点值大。对于根节点不用定位界,所以是无穷小到无穷大,接下来当我们往左边走时,上界就变 阅读全文
posted @ 2014-09-19 03:29 neverlandly 阅读(820) 评论(1) 推荐(0)
Leetcode: Recover Binary Search Tree

摘要:难度:89. 这道题是要求恢复一颗有两个元素调换错了的二叉查找树。一开始拿到可能会觉得比较复杂,其实观察出规律了就比较简单。主要还是利用二叉查找树的主要性质,就是中序遍历是有序的性质。那么如果其中有元素被调换了,意味着中序遍历中必然出现违背有序的情况。那么会出现几次呢?有两种情况,如果是中序遍历相邻 阅读全文
posted @ 2014-09-19 00:52 neverlandly 阅读(386) 评论(0) 推荐(0)
Leetcode: Search in Rotated Sorted Array II

摘要:Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function ... 阅读全文
posted @ 2014-09-18 12:18 neverlandly 阅读(348) 评论(0) 推荐(0)
Leetcode: Search in Rotated Sorted Array

摘要:根据binary search,每次我们都可以切掉一半的数据,所以算法的时间复杂度是O(logn),空间复杂度是O(1)。 阅读全文
posted @ 2014-09-18 11:38 neverlandly 阅读(320) 评论(0) 推荐(0)

上一页 1 ··· 22 23 24 25 26 27 28 29 30 ··· 32 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3