- 博客(35)
- 收藏
- 关注
原创 [LeetCode] Meeting Rooms 会议室-min_heap, sort, greedy
Meeting Rooms解法:按start排序,只要有overlap就是false,比较简单。Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), determine if a person could attend all...
2019-02-07 15:04:48
219
原创 Leetcode: Valid number 和 atoi
Valid Number这个题主要是corner case的考虑。另一个例子是atoi,我ex老板面试最喜欢考的题。几个tips:1,remove space front and end2,’+’ ‘-’3,’.’ and ‘0-9’这个check无效还挺有意思的,如果’.'多于1,或者#小于1。4,'e’和’e’后面的处理。class Solution {public: ...
2018-12-02 07:18:51
113
原创 Leetcode: 688. Knight Probability in Chessboard
Knight Probability in Chessboard这是最近的fb高频题,在一亩三分地上看到有人post原题典型的dp算法dp[i][j]表示在(i,j)位置上,棋子走完一步还在棋盘上的可能总和。初始为0,表示没走的时候棋子都在棋盘上。当走一步,(K=0)时,dp[i][j]就会update当再走一步,(K=1)时,dp[i][j]又会update。这个code,是每...
2018-12-01 13:05:30
236
原创 Leetcode: find peak
Peak Index in a Mountain Arraypeak的题都是用binary search,但是又不那么boringrefer to:https://blog.csdn.net/weixin_43476349/article/details/83238145这个题写法跟普通的sorted array binary search有一些不同。class Solution {...
2018-12-01 07:14:56
134
原创 Leetcode:remove node or elements from linkedlist
Remove Linked List Elements这两个都是easy题,还是写错:(当首节点也要处理的时候,最简单就是创建一个新的dummy节点。两种方法:一种是alloc (new)一个node,function ends时需要free。一种是创建一个local node,function ends时memory自动回收。这里涉及到最基本的stack vs heap:Diff...
2018-11-29 15:07:18
116
原创 Leetcode: 146. LRU Cache
凌晨1:24,转行中的程序媛,一边敷着面膜一边刷题,画面太美。既然是在程序人生专栏,应该可以写一点与程序无关的东西。感恩节长假,放松了四天,今天上班回家累的只想葛优躺,但是还得撑着伺候公主洗漱睡觉,眼睛都睁不开了,好想直接倒头就睡,可是看着临近的面试,不得不撑着。实在受不了,在沙发上睡了一会儿,起来接着刷LRU Cache这是个设计题。Cache一般有几种,分类主要是针对当Cache ...
2018-11-27 18:03:18
146
1
原创 Leetcode: 关于string的几道题,Encode and Decode Strings
纯刷题,几个关于string的题一般string的题就是顾名思义,把human的想法code出来,算法要求低一些常用API:cur += to_string(count) + res[i];int len = stoi(s.substr(start, curpos - start));int curpos = s.find_first_of(’/’, start);Count and...
2018-11-27 15:42:21
128
原创 Leetcode: Lowest Common Ancestor
平常工作里面基本不用tree,一提到tree总是心虚,多写吧Binary Search Tree vs Binary TreeIn a binary search tree there is a relative ordering in how the nodes are organized, while there is nothing of that sort in a binary tr...
2018-11-19 10:10:23
154
原创 Leetcode: Best Time to Buy and Sell Stock
这一篇总结leetcode里关于股票的题目,都是用dynamic programmingBest Time to Buy and Sell Stockhttps://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/只能操作一次class Solution {public: int maxPro...
2018-11-19 07:06:39
144
原创 Leetcode 268. Missing Number
https://leetcode.com/problems/missing-number/description/如果sorted,这题用binary search更快这里的解法就是用了bit manipulation里最常考的一个:XOR: A ^ A = 0class Solution {public: int missingNumber(vector<int>&...
2018-11-18 13:12:03
111
原创 Leetcode: Reverse Linked List
linkedlist,iterative seems much easier than recursiveReverse Linked Listhttps://leetcode.com/problems/reverse-linked-list/description//** * Definition for singly-linked list. * struct ListNode ...
2018-11-18 12:39:01
99
原创 backtracking vs DFS
leetcode 200. Number of IslandsInput:11110110101100000000Output: 1vsleetcode 79. Word Searchboard =[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E']]Given word = "ABCCED",...
2018-11-18 09:26:59
245
1
原创 how to define boundary in binary search 二分法的边界设定
refer to:https://blog.csdn.net/u011523762/article/details/50878613用伪代码来表示, 二分查找算法大致是这个样子的:left = 0, right = n -1while (left &amp;amp;amp;amp;lt;= right) mid = (left + right) / 2 case x[mid] &amp;amp;amp;amp;lt; t...
2018-11-18 09:24:57
216
原创 Binary tree Traversals
1, Depth first traversals:inorder (left, root, right)preorder (root, left, right)postorder (left, right, root)The root postion decides the order.For example:Inorder: void helper(TreeNode* ro...
2018-11-18 09:24:44
113
原创 Leetcode: Integer to Roman and Roman to Integer
没什么可说的,纯刷题,Integer to Roman,vector的建立可以单独写个API。Roman to Integer,主要是规律找出来,如果后面的char比见面的大,就是相减得到res。Integer to Romanhttps://leetcode.com/problems/integer-to-roman/description/class Solution {pub...
2018-11-18 09:00:39
103
原创 Leetcode: remove duplicate
有点啰嗦。这篇都是easy题,纪念刚开始刷题的日子,一直做嵌入式,根本不了解算法。最开始刷的一题是remove duplicates,跟这个题一样的,都是用:manage a new index to build array。感叹一下,以前的同学早已经在AI领域大展拳脚,我还在这里从最简单的算法开始,安慰自己开始了就好吧。Move Zeroeshttps://leetcode.com...
2018-11-16 14:44:34
169
原创 Leetcode 461. Hamming Distance
https://leetcode.com/problems/hamming-distance/description/bit manipulationone tip: (n & n - 1) is to remove the 1 in the LSB.用这个方法比comment的方法快一丢丢,毕竟少了一个ifclass Solution {public: int hamm...
2018-11-16 14:07:18
92
原创 Leetcode: two pointers
two pointer的简单实例time complexisty O(n), space complexisty O(1)two pointer跟binary search不要搞混了,binary search的每次计算量减半,所以time complexisty O(logn)Valid Palindromehttps://leetcode.com/problems/valid-pal...
2018-11-16 13:24:22
114
原创 Leetcode: intervals
https://leetcode.com/problems/merge-intervals/description/56. Merge Intervals这个题比较有意思的应该就是sort的写法。/** * Definition for an interval. * struct Interval { * int start; * int end; * In...
2018-11-15 16:37:12
108
原创 Leetcode 138. Copy List with Random Pointer
https://leetcode.com/problems/copy-list-with-random-pointer/description/A linked list is given such that each node contains an additional random pointer which could point to any node in the list or n...
2018-11-12 16:01:38
72
原创 Backtracking(回溯) 系列三subsets
这类题的共同点都是用backtracking,建立一个helper API然后recursioninput nums needs sort,为了方便remove duplicates如果input有duplicates(subsets II)sort(nums.begin(), nums.end())helper API有几个共同点:(跟combinations不一样的是)exit...
2018-11-12 13:27:59
118
原创 Backtracking(回溯) 系列二permutation
Refer to上一篇https://blog.csdn.net/weixin_43476349/article/details/83989562这类题的共同点都是用backtracking,建立一个helper API然后recursioninput nums needs sort,为了方便remove duplicatessort(nums.begin(), nums.end())...
2018-11-12 13:03:56
175
原创 Backtracking(回溯) 系列一combination
Tips:这类题的共同点都是用backtracking,建立一个helper API然后recursionhelper API有几个共同点:input:res,row,startexit condition:1,如果return有array size要求,比如combinations,要求k=2,exit condition就是:row.size()==k2,如果return是tar...
2018-11-12 11:48:40
287
原创 Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
Binary Tree Inorder Traversalhttps://leetcode.com/problems/binary-tree-inorder-traversal/description//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
2018-11-08 16:04:25
142
原创 leetcode:two sum,Subarray Sum类的问题 (hash table)
Subarray Sum Equals Khttps://leetcode.com/problems/subarray-sum-equals-k/description/Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum...
2018-11-08 14:18:57
124
原创 string add and string multiply
string的加和乘1, char to int / int to char2, 运算的时候,尽量int with int, char with char3,当char + int,结果还是char:‘1’ + 2 = ‘3’https://leetcode.com/problems/add-strings/description/415. Add Stringsclass Solut...
2018-11-07 08:34:11
122
原创 Leetcode 273. Integer to English Words
https://leetcode.com/problems/integer-to-english-words/description/Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.这题主要是corner c...
2018-11-06 15:57:37
100
原创 Leetcode 301. Remove Invalid Parentheses
https://leetcode.com/problems/remove-invalid-parentheses/description/Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.又是一道经典题。算法...
2018-11-06 15:00:44
85
原创 Sparse Matrix Multiplication
FB高频题sparse matrix指的是很多value都是0,1,save the non zero index in pair2,计算只对非0 index算新的datastructure-pair<int,int>class Solution {public: /** * @param A: a sparse matrix * @param...
2018-11-06 14:13:01
434
原创 Leetcode 157/158 Read N Characters Given Read4
FB高频题157:The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left...
2018-11-06 07:28:07
138
原创 Leetcode 128. Longest Consecutive Sequence
https://leetcode.com/problems/longest-consecutive-sequence/description/学到的新的data structure:unordered_sethttp://www.cplusplus.com/reference/unordered_set/unordered_set/unordered_set和unordered_map内部...
2018-11-05 14:14:40
86
原创 Leetcode: sliding window+two pointers
这种题有个规律,1,移动end pointer2,找到目标(if count == 0)3,移动begin pointer前面两题目标string是固定长度,后一题目标string是找到最短的,不同的处理在第三步。每次移动begin pointer要保证把map和count复原,这样才有机会找到下一个candidate。438 and 439 are exactely the same....
2018-11-05 13:34:50
97
原创 Leetcode 218. The Skyline Problem
https://leetcode.com/problems/the-skyline-problem/description/class Solution {public: vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) { vect...
2018-11-04 13:17:27
92
原创 Leetcode 23. Merge k Sorted Lists
https://leetcode.com/problems/merge-k-sorted-lists/description//** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n...
2018-11-03 13:32:05
97
原创 Leetcode 85. Maximal Rectangle and 84. Largest Rectangle in Histogram
https://leetcode.com/problems/largest-rectangle-in-histogram/description/class Solution {public: int maximalRectangle(vector<vector<char>>& matrix) { int row = matrix.siz...
2018-11-03 07:49:04
65
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人