随笔分类 - Leetcode
Leetcode: Binary Tree Right Side View
摘要:这道题就是BT的Level Order Traversal,每次要换一层的时候,记录当前节点 注意,每次都是先添加右边child,所以是i==0的时候add
阅读全文
Leetcode: Number of Islands
摘要:DFS的Flood Fill方法, 不使用额外visited数组,但是用‘1’变成‘2’表示visited的方法 复杂度 时间 O(NM) 空间 O(max(N,M)) 递归栈空间 Union Find: (Quick Union) follow up是找最大岛的面积,想法是设置两个全局变量 max
阅读全文
Leetcode: Reverse Bits
摘要:Reverse Integer那道题会考虑溢出,因为那是reverse each digit,这里不会溢出,因为reverse的是each bit Q:如果该方法被大量调用,或者用于处理超大数据(Bulk data)时有什么优化方法?A:这其实才是这道题的精髓,考察的大规模数据时算法最基本的优化方法
阅读全文
Leetcode: Number of 1 Bits
摘要:Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit i...
阅读全文
Leetcode: Best Time to Buy and Sell Stock IV
摘要:这道题在Best Time to Buy and Sell Stock III做过,那道题只是把k取了2而已 递推式依然是 local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff), global[i][j]=max(local
阅读全文
Leetcode: Reverse Words in a String II
摘要:这道题要求in-place做法,不能使用extra space, 那么,做法跟Rotate Array那道题非常相似 (1)reverse the whole array (2)reverse each subarray seperated by ' ' 注意不要忘了reverse最后一个word
阅读全文
Leetcode: Repeated DNA Sequence
摘要:Naive 方法就是两层循环,外层for(int i=0; i<=s.length()-10; i++), 内层for(int j=i+1; j<=s.length()-10; j++), 比较两个字符串s.substring(i, i+10)和s.substring(j, j+10)是否equal
阅读全文
Leetcode: Rotate Array
摘要:Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:...
阅读全文
Leetcode: Largest Number
摘要:聪明方法:其实干嘛要挨个比呢,按最直接的方法,接起来,谁大谁在前: 可以换一下思路,要想比较两个数在最终结果中的先后位置,何不直接比较一下不同组合的结果大小? 举个例子:要比较3和34的先后位置,可以比较334和343的大小,而343比334大,所以34应当在前。 这样,有了比较两个数的方法,就可以
阅读全文
Leetcode: Dungeon Game
摘要:The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a...
阅读全文
Leetcode: Binary Search Tree Iterator
摘要:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the ne...
阅读全文
Leetcode: Two Sum III - Data structure design
摘要:The trade off should be considered: In fact, there has to be one operation's time complexity is O(n) and the other is O(1), no matter add or find. So
阅读全文
Leetcode: Two Sum II
摘要:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function ...
阅读全文
Leetcode: Fraction to Recurring Decimal
摘要:Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating,...
阅读全文
Leetcode: Compare Version Numbers
摘要:这道题的意思用一个例子说明就是1.2 < 1.10, 因为1.10跟1.1是不一样的,1.10代表tenth revision. 而1.1代表first revision。所以这道题要按“.”分段,然后分段比较,方法是用split函数把string 分段,每一段挨个比过去。这里参考了网上的一个小技巧
阅读全文
Leetcode: Maximum Gap
摘要:Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 ...
阅读全文
Leetcode: Missing Ranges
摘要:第二遍做法:参考下面一个vimukthi的解答 https://discuss.leetcode.com/topic/18612/accepted-java-solution-with-explanation/3 low 表示next possible missing integer streak的
阅读全文
Leetcode: One Edit Distance
摘要:注意这道题:Must be exactly one distance apart. Not the same. 另外FB面经有一道比较狠的这个题的变形: 这题就是one edit distance的变形题,难点在于给的Iterator,事先不知道两个file的长度,也不允许用extra space(
阅读全文
Leetcode: Longest Substring with At Most Two Distinct Characters
摘要:方法一:用HashMap, map里面存元素及其出现次数。维护一个最大长度。用两个指针,右指针一直走到出现3个dinstinct character为止。然后调整左指针删元素,直接从左往右逐个字符的删除,一直删到某个字符不会再出现。判断字符被删光就看次数是否减为了0. 采用方法: 这里每个元素都会进
阅读全文
Leetcode: Read N Characters Given Read4 II - Call multiple times
摘要:这道题跟I不一样在于,read函数可能多次调用,比如read(buf,23)之后又read(buf, 25), 第一次调用时的buffer还没用完,还剩一个char在buffer里,第二次拿出来接着用,这样才能保证接着上次读的地方继续往下读。 1. 所以应该设置这4个char的buffer为inst
阅读全文
浙公网安备 33010602011771号