LintCode
real_lisa
这个作者很懒,什么都没留下…
展开
-
7 · Serialize and Deserialize Binary Tree
class Solution: """ @param root: An object of TreeNode, denote the root of the binary tree. This method will be invoked first, you should design your own algorithm to serialize a binary tree which denote by a root node to a string which .原创 2021-06-18 07:42:21 · 145 阅读 · 0 评论 -
LintCode Encode and Decode TinyURL
class Codec: codeDB, urlDB = defaultdict(), defaultdict() chars = string.ascii_letters + string.digits def getCode(self) -> str: code = ''.join(random.choice(self.chars) for i in range(6)) return "http://tinyurl.com/" + cod.原创 2021-06-18 07:02:58 · 152 阅读 · 0 评论 -
LintCode - Find Peak Element II
解法一 binary search O(n logm)找每一行的peak(binary search),然后check是否为matrix peakclass Solution {public: vector<int> findPeakII(vector<vector<int>> &A) { int n = A.size...原创 2018-11-21 10:36:11 · 303 阅读 · 0 评论 -
LintCode - Search for a Range
解法一 O(nlogn) + O(n)class Solution {public: vector<int> searchRange(vector<int> &A, int target) { // lower index if(A.empty()) return {-1,-1}; int n = A....原创 2018-11-19 12:43:24 · 131 阅读 · 0 评论 -
LeetCode / LintCode - Search a 2D Matrix
解法一 two binary search O(logm)+O(logn)class Solution {public: bool searchMatrix(vector<vector<int>> &matrix, int target) { if(matrix.empty() || matrix[0].empty()) return...原创 2018-11-11 13:55:11 · 123 阅读 · 1 评论 -
LintCode - Find Minimum in Rotated Sort Array II
class Solution {public: int findMin(vector<int> &nums) { int l=0, r=nums.size()-1; while(l<r){ int mid = l+(r-l)/2; if(nums[mid]>nums[r]) ...原创 2018-11-18 15:19:09 · 122 阅读 · 0 评论 -
LeetCode / LintCode - Search a 2d Matrix II
class Solution {public: bool searchMatrix(vector<vector<int> > &matrix, int target) { if (matrix.empty() || matrix[0].empty()) return false; if (target < matri...原创 2018-11-11 14:21:11 · 106 阅读 · 0 评论 -
LintCode - Search In a Big Sorted Array
class Solution {public: int searchBigSortedArray(ArrayReader * reader, int target) { int l=0, r=0; while(reader->get(r)<=target){ r=2*r+1; } whi...原创 2018-11-18 13:46:53 · 218 阅读 · 2 评论 -
LintCode - Find Minimum in Rotated Sorted Array
class Solution {public: int findMin(vector<int> &nums) { int l=0, r=nums.size()-1; while(l<r){ int mid = l+(r-l)/2; if(nums[mid]>nums[r]) ...原创 2018-11-18 14:23:57 · 150 阅读 · 0 评论 -
LintCode - Smallest Rectangle Enclosing Black Pixels
解法一 4 binary searchclass Solution {public: int minArea(vector<vector<char>> &image, int x, int y) { int n = image.size(), m =image[0].size(); int left=0, right=...原创 2018-11-20 10:47:55 · 155 阅读 · 0 评论 -
LintCode - Maximum Number in Mountain Sequence
class Solution {public: int mountainSequence(vector<int> &nums) { int l=0, r=nums.size()-1; while(l<r){ int mid = l+(r-l)/2; if(nums[mid]<...原创 2018-11-20 11:16:40 · 151 阅读 · 0 评论 -
LeetCode / LintCode - First Bad Version
LeetCode// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: int firstBadVersion(int n) { int l=0,r=n; while(l<r){ ...原创 2018-11-10 15:58:34 · 129 阅读 · 0 评论 -
LintCode - Rehashing
class Solution {public: vector<ListNode*> rehashing(vector<ListNode*> hashTable) { int n = hashTable.size(), m=n*2; vector<ListNode*> res(m, NULL); for...原创 2018-11-13 17:29:52 · 144 阅读 · 0 评论 -
LintCode - Window Sum
class Solution {public: /** * @param nums: a list of integers. * @param k: length of window. * @return: the sum of the element inside the window at each moving. */ vector...原创 2018-11-12 10:45:10 · 250 阅读 · 0 评论 -
LeetCode - Remove Duplicate Numbers In Array
解法一 O(n) hashtableclass Solution {public: int deduplication(vector<int> &nums) { unordered_set<int> temp; int n = nums.size(); int idx = n-1, i=0; ...原创 2018-11-12 10:44:27 · 190 阅读 · 0 评论 -
LintCode - Binary Tree Preorder Traversal
解法一 recursion三种方法,不同在于如何处理vectorclass Solution {public: vector<int> preorderTraversal(TreeNode * root) { if(!root) return {}; vector<int> res{root->val}; ...原创 2018-11-12 11:45:35 · 105 阅读 · 0 评论 -
LintCode - Binary Tree Inorder Traversal
解法一 recursionclass Solution {public: vector<int> res; vector<int> inorderTraversal(TreeNode * root) { res.clear(); helper(root); return res; } v...原创 2018-11-12 13:23:06 · 124 阅读 · 0 评论 -
LintCode - Binary Tree Postorder Traversal
解法一 recursionclass Solution {public: vector<int> res; vector<int> postorderTraversal(TreeNode * root) { res.clear(); helper(root); return res; } ...原创 2018-11-12 16:41:08 · 165 阅读 · 0 评论 -
LintCode - Maximum Depth of Binary Tree C++
解法一 recursiondivide and conquerclass Solution {public: int maxDepth(TreeNode * root) { if(!root) return 0; int left = maxDepth(root->left); int right = maxDepth(ro...原创 2018-11-12 17:06:41 · 113 阅读 · 0 评论 -
LintCode / LeetCode 102. Binary Tree Level Order Traversal | Queue or Stack DFS | C++ Python
解法一 queueclass Solution {public: vector<vector<int>> levelOrder(TreeNode * root) { queue<TreeNode*> q; vector<vector<int>> res; if(!root) ...原创 2018-11-12 17:44:33 · 87 阅读 · 0 评论 -
LintCode - Subsets
解法一 recursion DFSclass Solution {public: vector<vector<int>> subsets(vector<int> &nums) { vector<vector<int>> res; vector<int> temp; ...原创 2018-11-13 09:34:09 · 109 阅读 · 0 评论 -
LintCode - Reverse Nodes in k-Groups
解法一每次翻转k个element1. 利用%k来完成每次k个element 的翻转2. 多次将cur移到最前端完成翻转class Solution {public: ListNode * reverseKGroup(ListNode * head, int k) { if(!head || k==1) return head; ListNo...原创 2018-11-13 09:32:58 · 133 阅读 · 0 评论