01 2014 档案
摘要:前言: 放假前偷偷跑到杭州玩了几天,虽然没有预料中那么美丽,不过还是很开心,感谢老严的热情款待。回到南京后,距离完成任务就剩3天时间,而且师兄逼的很紧,于是夜以继日,实现了对比算法《Multimodal Sparse Representation-Based Classification for Lung Needle Biopsy Images》(Google学术搜索)。 遗传算法虽然如雷贯耳,但是一直未有接触,此次从零开始,看算法、看论文,对了,还要招待从云南回来的基友。时间虽然很紧,但是最后还是圆满完成任务,得以回家过年,感觉很充实。 网上有很多GA的参考文章,我就不画蛇添足了,但大多数
阅读全文
摘要:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.思考:在上一题的基础上思考几个边界问题。[1,3] 3 [1,3] 3[3,3] 1 [3,1] 3[1,3,1,1,1] 3[1,1,1,3,1] 1class Solution {pub
阅读全文
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.思考:跟上一题一样,维护左右边界。参考:http://discuss.leetcode.com/questions/260/maximal-rectangle#class Solution {public: int maximalRectangle(vector > &matrix) { if (matrix.empty()) { .
阅读全文
摘要:Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use#as a separator for each node, and,as a separator for node label and each neighbor of the node.As an example, consider the serialized g
阅读全文
摘要:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas
阅读全文
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ...
阅读全文
摘要:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N...
阅读全文
摘要:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?思考:维护数组ans,从后往前更新。class Solution {public: vector getRow(int rowIndex) { vector ans; ans.resize(rowIndex+1,0); a...
阅读全文
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思考:边界单独考虑。class Solution {private: vector > res; vector ans;public: vector > generate(int numRows) { if(numRows==0) return res; ...
阅读全文
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list./** * Definition for singly-linked list. * struct List
阅读全文
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3./** * Definition for singly-linked list. * struct Lis
阅读全文
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val
阅读全文
摘要:Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], []]思考:直接set去重会不会太暴...
阅读全文
摘要:Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not co...
阅读全文
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]Have you been asked this question in an interview?Yesclass Solution {private: vector > res; vector ans;public: void ...
阅读全文
摘要:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC".Note:If there is no such window in S that covers all characters in T, return the emtpy
阅读全文
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ...
阅读全文
摘要:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character思考:DP。dp[i-1][j]+1:删除一个字符dp[i][j-1]+1:插入一个字符dp
阅读全文
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo
阅读全文
摘要:一.Boosting和Bagging 首先,Boosting和Bagging两种算法都是通过构造一系列的预测函数,然后以某种方式将他们组合成最终的预测模型,以此来提高学习算法的准确度。 Bagging的主要思想是给定一个弱学习算法和一组训练集(x1,y1),…,(xn,yn)。让该学习算法训练多...
阅读全文
摘要:Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line
阅读全文
摘要:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.思考:先首尾连成环,head前进(len-k%len)步,拆环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L
阅读全文
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not
阅读全文
摘要:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]思考:ACM蛇形填数。class Solution {public: vector > generateMatrix(int n) { vector > res; if(n==0) ...
阅读全文
摘要:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.思考:同样,满足条件res++即可。class Solution {private: int res;public: bool check(int *a,int k,int i) { for(int j=0;j<k;j++) { if(a[j]==i||abs(a[j]-i)==abs...
阅读全文
摘要:Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinc...
阅读全文
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.思考:map应用。遍历两次strs,第一次建立map,第二次打印符合提议的字符串。class Solution {private: vector res; map m;public: vector anagrams(vector &strs) { int i; for(i=0;i::iterator iter=m.find(t...
阅读全文
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique perm...
阅读全文
摘要:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,...
阅读全文
摘要:一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap。heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全二叉树,我们通常用一个数组来表示。 以下几个算法都是STL的泛型算法,包含在头文件algorithm里,priority_queue的push(),pop()都有直接调用它们。1.push_heap算法 首先,将新元素插入到底层vector的end()处。为了满足最大堆原理(每个结点的值必须大于或等于其子结点的值),于是将新结点与其父亲结点比较,如果其值大于父亲结点,就交换父子结点,直到不需要交换
阅读全文
摘要:怎么说呢,deque是一种双向开口的连续线性空间,至少逻辑上看上去是这样。然而事实上却没有那么简单,准确来说deque其实是一种分段连续空间,因此其实现以及各种操作比vector复杂的多。一.deque的中控器 deque是有一段一段的定量连续空间构成,采用一块所谓的map(当然不是map容器)作为主控。map是一小块连续空间,其中每一个元素都是一个指针,指向另一段连续性空间(缓冲区)。缓冲区才是deque的储存空间主体。我们可以指定缓冲区大小,默认值0表示使用512字节缓冲区。deque设计结构如下图所示:二.deque的数据结构 deque除了维护map的指针外,也要维护start,fi.
阅读全文
摘要:一.list的成员函数Iterators:list.begin()回传指向第一个元素的 Iterator。list.end()回传指向最末元素的下一个位置的 Iterator。list.rbegin()回传指向最末个元素的反向 Iterator。list.rend()回传指向第一个元素的前一个位置的反向 Iterator。Capacity/Size:list.empty()若list内部为空,则回传true值。list.size()回传list内实际的元素个数。lsit.resize()重新分派list的长度。Element Accesslist.front()存取第一个元素。list.bac
阅读全文
摘要:与普通数组array不同,vector是动态空间,它支持随机存取,在集合尾端增删元素很快,但是在集合中间增删元素比较费时。随着元素的加入,它会自行扩充空间来容纳新元素,这些特性都是vector实现技术的关键所在。一.vector成员函数: 首先了解vector的各个成员函数的作用:1.访问元素的方法vec[i]- 访问索引值为 i 的元素引用。 vec.at(i)- 访问索引值为 i 的元素的引用,以 at() 访问会做数组边界检查,如果访问越界将会抛出异常。vec.front()- 回传 vector 第一个元素的引用。vec.back()- 回传 vector 最尾元素的引用。2.新增或.
阅读全文
摘要:考虑到过多“小型区块”可能造成的内存碎片问题,SGI设计了双层级配置器: 第一级配置器直接调用malloc()和free(); 第二级配置器分两种情况:当配置区块大于128字节时,调用第一级配置器;当配置区块小于128字节时,采用内存池管理。一.第一级配置器1.__malloc_alloc_tempalte源码template class __malloc_alloc_template {private: //以下函数用来处理内存不足的情况 static void *oom_malloc(size_t); static void *oom_realloc(void *, size_...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not
阅读全文
摘要:Sort a linked list in O(n log n) time using constant space complexity.思路:分治+递归。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private: int getlen(ListNode *head) { Lis...
阅读全文
摘要:一.I/O复用 在《TCP套接字编程》的同步聊天程序中,我们看到TCP客户同时处理两个输入:标准输入和TCP套接字。考虑在客户阻塞于标准输入fgets调用时,服务器进程被杀死,服务器TCP虽然会给客户TCP发送一个FIN,但是客户客户进程正阻塞于标准输入读入过程,它将看不到这个EOF,直到从...
阅读全文