- 博客(39)
- 收藏
- 关注
原创 八皇后问题(排列+递归+剪枝=回溯法)
八皇后问题简述:8*8的棋盘上放8个棋子,保证每一行、每一列、每个对角线上只有一个棋子,问共有几种排法。想法:每一行、每一列只能放一个棋子,我们可以用一个int a[8]数组来存放棋子的位置,其中,下标代表行数,数组内存的数代表列数。每个棋子的行数和列数要互异,所以列数用0~7初始化。枚举数组的所有可能排列,并检查对角线上是否有两颗或以上的棋子占据即可。实现
2017-10-26 15:09:08
2324
原创 LintCode 192:Wildcard Matching
Description:Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matc
2017-10-16 15:38:57
223
原创 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
266
原创 LintCode54: String to Integer II
Description:Implement function atoi to convert a string to an integer.If no valid conversion could be performed, a zero value is returned.If the correct value is out of the range of repr
2017-10-04 10:11:02
242
原创 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
260
原创 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
354
原创 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
224
原创 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
465
原创 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
219
原创 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
297
原创 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
274
原创 LintCode 376: Binary Tree Path Sum
Description:Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.Note:1.a valid path的定义是 from root node to any of the leaf nodes.所以当满足target条
2017-08-27 12:16:48
270
原创 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
233
原创 LintCode 174:Remove Nth Node From End of List
Description:Given a linked list, remove the nth node from the end of list and return its head.Note:需要注意的边界情况:当需要删除的结点是head结点时,head会发生改变。当n超过结点个数时,表明没有需要删除的结点。Code:/** * Definiti
2017-08-26 21:49:08
238
原创 LintCode 372: Delete Node in the Middle of Singly Linked List
Description:Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.Note:1.拿到的是当前的结点,也就是说拿不到当前结点前一个结点,所以我把当前结点用下一个结点的值覆盖掉,再删掉下一个结点。
2017-08-26 21:46:21
222
原创 LintCode 245 : Subtree
Description:You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.Notice:A tree T2 is a su
2017-08-26 20:30:32
294
原创 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
270
原创 杂
泊松分布什么是泊松分布?符合以下3个特点就是泊松分布:1)事件是独立事件(之前如果你看过我的《投资赚钱与概率》已经知道赌徒谬论了,所以类似抽奖这样的就是独立事件)2)在任意相同的时间范围内,事件发的概率相同(例如1天内中奖概率,与第2天内中间概率相同)3)你想知道某个时间范围内,发生某件事情x次的概率是多大(例如你搞了个促销抽奖活动,想知道一天内10人中奖的概率)作者:猴子
2017-08-24 22:00:10
285
原创 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
208
原创 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
346
原创 Hirschberg 's method for LCS
一种采用了分治思想和动态规划的LCS算法,仅需O(NM)时间复杂度和O(min(N,M))空间复杂度就可得最大子串及其长度,具体参见论文:A Linear Space Algorithm for Computing Maximal Common Subsequences。这里介绍一下自己的理解。如何分治?给定两个字符串X(长度为m),Y(长度为n)。将X平分为两个子串X(1,m/2
2017-05-11 22:02:19
2119
1
原创 堆-优先队列
面试的时候被问到了heapsort,发现自己对数据结构这块的理解实在不够,最近打算推翻重新来学一遍。这里先简单地说一下自己对堆的理解。堆的特质:1.平衡二叉树=》保证了操作时间复杂度为logN2.以最小堆为例,对任意结点,其父节点均小于子节点。=》这样几乎各种操作都是在纵向上(深度)进行的。由于在堆这个数据结构中,经常需要用到父节点和子节点这个概念,因而为了方便父节点与
2017-04-18 23:23:14
247
原创 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
313
原创 hihoCoder-week144:机会渺茫
描述小Hi最近在追求一名学数学的女生小Z。小Z其实是想拒绝他的,但是找不到好的说辞,于是提出了这样的要求:对于给定的两个正整数N和M,小Hi随机选取一个N的约数N',小Z随机选取一个M的约数M',如果N'和M'相等,她就答应小Hi。小Z让小Hi去编写这个随机程序,到时候她review过没有问题了就可以抽签了。但是小Hi写着写着,却越来越觉得机会渺茫。那么问题来了,小Hi能够追到小Z的几率是
2017-04-02 11:15:43
304
原创 LintCode 81:Data Stream Median
维护了一个排好序的vector,每次用二分法插入数,插入的平均时间复杂度为O(logN)。class Solution { void InsertAndSort(vector& sortVector,int num){ int low=0; int high=sortVector.size()-1; while(low<high)
2017-02-22 20:37:11
221
原创 LintCode 86:Binary Search Tree Iterator
虽说是hard难度但是实际很简单。在构造时直接将树按inorder顺序保存入vector中,时间复杂度为O(N),空间复杂度为O(N),之后再获取node时的复杂度全都为O(1)。class BSTIterator { vector inOrderList; vector::iterator p; void inOrderTree(TreeNode* node)
2017-02-22 19:20:47
479
原创 LintCode 178:Graph Valid Tree
问题:给定一幅图,问是否能转换成树。转换:给定一幅图,问是否存在环(loop)或孤立点。实现:首先确保边的个数等于结点个数减一。这样,只要检查有没有环就可以。当边的个数等于结点个数减一时,若存在环,则也一定也存在孤立点,反之,若不存在环,则也一定不存在孤立点。其次我们检查是否存在环。这里我们另维护了一个二维数组block,block每列记录的是相连的点。当加入的一组点存
2017-02-22 18:53:36
301
原创 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
333
原创 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
383
原创 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
3697
原创 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
307
原创 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
240
原创 LintCode 30:Insert Interval
跟merge intervals差不多的,没什么好说的。class Solution {public: /** * Insert newInterval into intervals. * @param intervals: Sorted interval list. * @param newInterval: new interval.
2017-02-08 16:15:30
233
原创 LintCode 423:Valid Parentheses
Problem:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.Solution:采用stack,当字符为上括号时push进stack,否之则从stack pop出字符看是否匹配。
2017-02-08 15:34:35
228
原创 LintCode 480:Binary Tree Paths
Problem:Given a binary tree, return all root-to-leaf paths.Solution:每个结点记录从root至其的path,当结点无左右孩子时,将path传入进vector。这里需要注意的是,recordPath函数中,path记录着root至结点的path,不可改变,不能加&(reference),而pathResul
2017-02-08 13:56:59
245
原创 LintCode 156: Merge Intervals
Problem: Given a collection of intervals, merge all overlapping intervals.Solution:先按interval.start按升序进行冒泡排序,再逐一进行merge(如i1和i2比较,若可以merge再i1和后续比较,不可以merge则i2和i3等比较merge)。Problem:冒泡排序耗时太长。Mod
2017-02-08 11:09:11
312
原创 LintCode 433: Number of Islands
直接用了序贯算法写。然后发现!太麻烦了啊啊啊啊啊啊一道easy题写的心好累(虽然时间复杂度挺好的。class Solution {public: /** * @param grid a boolean 2D matrix * @return an integer */ int checkEqualList(vector >& equalL
2017-02-07 11:00:51
295
原创 LintCode 407:Plus One
Note:The use of Vector:1.initializeint x[10]={9,8,7,6,5,4,3,2,1,0};vector test(x,x+10);2.end();begin();vector.begin() points to the first item,while vector.end() doesn't point to the
2017-02-06 08:39:29
368
原创 Reference:alias
Rule:1.References must be initialized when defined. Initialization establishes a binding.eg: type& refname = name; //thus the value of refname is the same as name.2.Bindings don’t cha
2017-02-06 08:29:59
287
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人