LintCode medium
文章平均质量分 72
qq_34153219
这个作者很懒,什么都没留下…
展开
-
LintCode 427:Generate Parentheses
Problem:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.Solution:因为之前写过Valid Parentheses,所以一开始想的是把所有排列可能先找出来再验证。但是时间复杂度过于恐怖,达到了O(n!),枪毙此方法。原创 2017-02-10 10:57:16 · 216 阅读 · 0 评论 -
Lintcode 61:Search for a Range
Description:Given a sorted array of n integers, find the starting and ending position of a given target value.If the target is not found in the array, return [-1, -1].Note:1.用二分法找两次index,时原创 2017-10-03 10:44:34 · 214 阅读 · 0 评论 -
Lintcode 3:Digit Counts
Description:Count the number of k's between 0 and n. k can be 0 - 9.Note:直接暴力做。当k为零的时候特殊处理下。Code:class Solution {public: /* * @param : An integer * @param : An integer * @return原创 2017-10-02 17:06:55 · 305 阅读 · 0 评论 -
LintCode 374: Spiral Matrix
Description:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.思路:设置四条边的界限,每遍历完一条边,边的界限缩小1(左边的边左移一格,上边的边下移一格,以此类推。)注意每次移动边后数组下标有可能越界,要注原创 2017-08-28 21:48:29 · 212 阅读 · 0 评论 -
LintCode 379:Reorder array to construct the minimum number
Description:Construct minimum number by reordering a given non-negative integer array. Arrange them such that they form the minimum number.Note:思想参照剑指offer,重新定义下两个数的比较符号,并对开头为零的情况进行处理。原创 2017-09-06 16:05:18 · 192 阅读 · 0 评论 -
LintCode 5: Kth Largest Element
Description:Find K-th largest element in an array.Note:用了heapSort的思想。需要注意的是调用vector的size()函数返回的是unsigned int类型,当为负时会越界,所以要转换为int类型。class Solution {public: /* * param k : desc原创 2017-09-05 15:04:53 · 383 阅读 · 0 评论 -
LintCode 4:Ugly Number II
Description:Ugly number is a number that only have factors 2, 3 and 5. Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12Note:1.根原创 2017-09-05 15:02:40 · 189 阅读 · 0 评论 -
LintCode 532:Reverse Pairs
Description:For an array A, if i A [j], called (A [i], A [j]) is a reverse pair.return total of reverse pairs in A.Note:1.采用分治思想(类似于mergesort),先计算一个subarray中的reverse pairs,然后再计算两个subarray原创 2017-09-05 11:03:49 · 238 阅读 · 0 评论 -
LintCode 150 : Fast Power
Description:Calculate the a^n % b where a, b and n are all 32bit integers.Note:1.注意a,b,n都是32位的integers,所以不能硬算,网上找到了一个公式:(a * b) % p = ((a % p) * (b % p)) % p可以根据这个公式进行递归运算。2.注意a,b,n三个的原创 2017-08-27 11:39:47 · 190 阅读 · 0 评论 -
LintCode 159:Construct Binary Tree from Preorder and Inorder Traversal
Description:Given preorder and inorder traversal of a tree, construct the binary tree.Note:1.对前序的观察:若a在b前被遍历,则a为b的父节点或a为b的左兄弟。2.对中序的观察:若a在b前被遍历,则a在b左边。一开始(错误的)思路:一开始想到树结点是不保存父节点的,因原创 2017-08-26 00:09:45 · 236 阅读 · 0 评论 -
LintCode 159:Find Minimum in Rotated Sorted Array
Description:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.Note:1.用二分法。当nums[low]原创 2017-08-24 21:38:54 · 164 阅读 · 0 评论 -
Lintcode 40:Implement Queue by Two Stacks
Problem:As the title described, you should only use two stacks to implement a queue's actions.The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) elem原创 2017-08-24 17:36:31 · 305 阅读 · 0 评论 -
LintCode 7:Binary Tree Serialization
Problem:Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact sa原创 2017-04-18 16:32:25 · 286 阅读 · 0 评论 -
Lintcode 617:Maximum Average Subarray
一开始用了最简单的遍历循环法,时间复杂度为O(N^2),然后被提醒超时,上网查了之后做了一个小改动,用一个sum数组存储num[0]到num[i]的总和,这样算num[i]到num[j] 只要算sum[j]-sum[i]就好了。这个想法的代码如下:class Solution {public: /** * @param nums an array with positi原创 2017-02-21 20:27:34 · 3580 阅读 · 2 评论 -
LintCode 178:Graph Valid Tree
问题:给定一幅图,问是否能转换成树。转换:给定一幅图,问是否存在环(loop)或孤立点。实现:首先确保边的个数等于结点个数减一。这样,只要检查有没有环就可以。当边的个数等于结点个数减一时,若存在环,则也一定也存在孤立点,反之,若不存在环,则也一定不存在孤立点。其次我们检查是否存在环。这里我们另维护了一个二维数组block,block每列记录的是相连的点。当加入的一组点存原创 2017-02-22 18:53:36 · 253 阅读 · 0 评论 -
LintCode 528:Flatten Nested List Iterator
class NestedIterator { vector flattenList; vector::iterator p; void flatten(vector nestedList){ int i; for(i=0;i<nestedList.size();i++){ if(nestedList[i].isIn原创 2017-02-22 16:22:10 · 292 阅读 · 0 评论 -
LintCode 104:Merge k Sorted Lists
用了数据库中学的方法。由于每个list都是排好序的,所以就比较每个list开头的数,拿出最小的数push入结果,push完之后扔掉这个数(指针指向下一个数)。循环直至当最小的数为256。(即tmp没有发生改变,没有需要排序的数了)时间复杂为O(vector长度*最长lists)(是NlogN吗,这个不太懂)。class Solution {public:原创 2017-02-21 21:59:18 · 335 阅读 · 0 评论 -
LintCode 442:Implement Trie
class TrieNode {public: // Initialize your data structure here. TrieNode* children[26]; bool flag;//check if it's the end of a word. TrieNode() :flag(0){ for(int i=0;i<26;i++)原创 2017-02-10 23:09:54 · 281 阅读 · 0 评论 -
LintCode 113:Remove Duplicates from Sorted List II
Description:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.Note:1.由于重复的结点全要删掉,所以使用一个int变量num记录当前判断是否有重复的结点,n原创 2017-10-13 15:19:03 · 195 阅读 · 0 评论