随笔分类 - Leetcode
Leetcode: Read N Characters Given Read4
摘要:说明:1. read4(char * buf)的这个buf起初是空的,然后read4读出来的东西存在这个buf里; 2. read函数的buf是destination, 是我们需要往里面依次写通过read4读出来的东西 首先read4是一个读文件的函数,只能读4个char。 char [] buff
阅读全文
Leetcode: Binary Tree Upside Down
摘要:这题第一眼看上去觉得没头绪,不知道怎么上下翻转和左右翻转。但在纸上画几个例子就清楚了。所有的右子树要么为空、要么就是叶子节点且有左子树存在。 那么原来的数一直沿左子树走下去最左的那个节点就是新树的根节点。 这道题最关键在于想到要用递归去做!这种树的结构、父子两层节点关系的问题多半都要用递归去做。这是
阅读全文
Leetcode: Excel Sheet Column Number
摘要:Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A
阅读全文
Leetcode: Factorial Trailing Zeroes
摘要:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Naive方法:A simple method is...
阅读全文
Leetcode: Excel Sheet Column Title
摘要:这道题是我微软onsite时遇到的一道题,没做过遇到这道题确实有点难一下子理得很清楚(我当时这道题应该做的不好,从most significant digit做,而且忘了n要-1)。这道题说白了其实就是十进制转换26进制,而且是从1开始的1十进制的转换 短除法: Short Division 本质是
阅读全文
Leetcode: Find Peak Element
摘要:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index...
阅读全文
Leetcode: Intersection of Two Linked Lists
摘要:第一想法是用HashSet<ListNode>, A list先遍历,存HashSet,然后B list遍历,发现ListNode存在就返回。但是这个方法不满足O(1)memory的要求。 再想了一会儿,略微受了点提醒,发现可以利用这个O(n) time做文章。这个条件方便我们scan list几次
阅读全文
Leetcode: Majority Element
摘要:Leetcode的官方答案给的解答很好,我的方法是HashMap. 除了brute force和sorting常见方法以外,还有几个方法,思路都还不错,1是我的方法,我觉得2、4、5都是不错的思路。 位操作法 复杂度 时间 O(N) 空间 O(1) 思路 假设一个数是最多只有32位的二进制数,那么我
阅读全文
Leetcode: Find Minimum in Rotated Sorted Array II
摘要:Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?前面那道题:Find ...
阅读全文
Leetcode: Find Minimum in Rotated Sorted Array
摘要:这道题跟Search in Rotated Sorted Array这道题很像,区别在于这道题不是找一个目标值,而是找最小值。所以与那道题的思路类似又有区别。类似在于同样是用binary search找目标区域,通过左边界和中间的大小关系来得到左边或者右边有序。区别在于这一次迭代不会存在找到targ
阅读全文
Leetcode: Min Stack
摘要:这是一道关于栈的题目,整体来说比较简单。我最开始想的时候想的太简单,以为使用一个栈st, 再维护一个最小值变量就好了。突然发现pop()操作之后需要更新这个最小值,那就需要知道第二小的值,这个第二小的值怎么找呢?于是乎我想到了使用另外一个栈minst专门来存储最小值。push()操作的时候每当x小于
阅读全文
Leetcode: Maximum Product Subarray
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],th...
阅读全文
Leetcode: Evaluate Reverse Polish Notation
摘要:难度:70. RPN中文名字叫做逆波兰表示法,它的好处维基百科说了,就是不需要括号来表示运算的先后,直接根据式子本身就可以求解。解题思路就是维护一个栈,遇到数字就入栈,遇到操作符就两次出栈得到栈顶的两个操作数,运用操作符进行运算以后,再把结果入栈。直到式子结束,此时栈中唯一一个元素便是结果。 以上代
阅读全文
Leetcode: Clone Graph
摘要:Leetcode里关于图的题其实并不多,这道题就是其中之一。DFS深度优先搜索和BFS广度优先搜索都可以做,遍历完原图的所有节点。这道题的难点在于neighbour关系的拷贝:原图中某一个点跟一些点具有neighbour关系,那么该点的拷贝也要与上述那些点的拷贝具有neighbour关系。那么,就需
阅读全文
Leetcode: Text Justification
摘要:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You shoul...
阅读全文
Leetcode: Valid Number
摘要:Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the p...
阅读全文
Leetcode: Spiral Matrix II
摘要:Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order.For example,Given n = 3,You should return the followin...
阅读全文
Leetcode: Regular Expression Matching
摘要:难度:100,情况分得太多太细,跟Wildcard Matching很像又比那个难多了,refer to: https://discuss.leetcode.com/topic/40371/easy-dp-java-solution-with-detailed-explanation Here ar
阅读全文
Leetcode: Decode Ways
摘要:难度:90. 非常不错的一道一维DP题目,参考了网上的做法。 看到这种求数量的,我们很容易想到动态规划来存储前面信息,然后迭代得到最后结果。我们维护的量res[i]是表示前i个数字有多少种解析的方式,接下来来想想递归式,有两种方式:第一种新加进来的数字不然就是自己比较表示一个字符,那么解析的方式有r
阅读全文
Leetcode: Permutation Sequence
摘要:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence ...
阅读全文
浙公网安备 33010602011771号