上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 34 下一页
摘要: 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 阅读全文
posted @ 2014-01-12 16:55 七年之后 阅读(154) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2014-01-12 16:45 七年之后 阅读(165) 评论(0) 推荐(0) 编辑
摘要: 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) ... 阅读全文
posted @ 2014-01-12 14:53 七年之后 阅读(208) 评论(0) 推荐(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... 阅读全文
posted @ 2014-01-12 13:38 七年之后 阅读(197) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-01-12 13:32 七年之后 阅读(384) 评论(0) 推荐(0) 编辑
摘要: 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... 阅读全文
posted @ 2014-01-11 14:31 七年之后 阅读(155) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique perm... 阅读全文
posted @ 2014-01-11 13:57 七年之后 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 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,... 阅读全文
posted @ 2014-01-11 12:30 七年之后 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 一.heap 在STL中,priority_queue(优先权队列)的底层机制是最大堆,因此有必要先来了解一下heap。heap采用完全二叉树的结构,当然不是真正的binary tree,因为对于完全二叉树,我们通常用一个数组来表示。 以下几个算法都是STL的泛型算法,包含在头文件algorithm里,priority_queue的push(),pop()都有直接调用它们。1.push_heap算法 首先,将新元素插入到底层vector的end()处。为了满足最大堆原理(每个结点的值必须大于或等于其子结点的值),于是将新结点与其父亲结点比较,如果其值大于父亲结点,就交换父子结点,直到不需要交换 阅读全文
posted @ 2014-01-10 17:06 七年之后 阅读(1996) 评论(0) 推荐(1) 编辑
摘要: 怎么说呢,deque是一种双向开口的连续线性空间,至少逻辑上看上去是这样。然而事实上却没有那么简单,准确来说deque其实是一种分段连续空间,因此其实现以及各种操作比vector复杂的多。一.deque的中控器 deque是有一段一段的定量连续空间构成,采用一块所谓的map(当然不是map容器)作为主控。map是一小块连续空间,其中每一个元素都是一个指针,指向另一段连续性空间(缓冲区)。缓冲区才是deque的储存空间主体。我们可以指定缓冲区大小,默认值0表示使用512字节缓冲区。deque设计结构如下图所示:二.deque的数据结构 deque除了维护map的指针外,也要维护start,fi. 阅读全文
posted @ 2014-01-09 21:39 七年之后 阅读(7510) 评论(0) 推荐(1) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 34 下一页