数据结构
文章平均质量分 52
fu_ding1991
勿忘初心,方得始终
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
POJ 2002--Squares
438ms 题目要求四点能组成正方形的个数,若直接枚举四点,会TLE。 可以根据正方形公式,题目有提示,正方形旋转90度与原图形重合。 ******所以我们可以枚举对角线,只有5*10^5个,根据对角线经过中点旋转,来得到 另外的两点,此处可以形象地理解已知A(x1,y1),C(x2,y2)为正方形对角线,经过旋转 AB边在x,y轴上的投影长度会互换调。原创 2014-07-26 18:07:09 · 458 阅读 · 0 评论 -
Facebook Hacker Cup 2015 Round 1--Autocomplete(字典树新建与查询)
题意:给定N个字符串,让你依次先输入到手机的字典中,再打印出来,打印的时候我们只需要输出字符串的前缀或者全部字符串,要求此前缀不是以往任何字符串的前缀。 题解:典型的字典树,可以利用结构体数组方便的新建与查询,速度比链表更快。只需在插入字符串时统计最长相同的前缀即可。代码如下:#include<cstdio>#include<cstring>#include<algorithm>using原创 2015-03-14 17:44:30 · 745 阅读 · 0 评论 -
LeetCode--Reverse Nodes in k-Group
题意:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2015-03-30 15:35:59 · 367 阅读 · 0 评论 -
LeetCode--Convert Sorted Array to Binary Search Tree(平衡二叉搜索树)
题意:给定一有序数组,生成一颗平衡二叉树。 题解:平衡二叉树任意节点的左右子树深度相差最大为1. 因为二叉搜索树任意节点的左子树节点都比它小,右子树节点都比它大。所以对于一个有序数组我们选取中间的节点作为根,再分别递归地调用生成左右两个子树。代码如下:class Solution {public: TreeNode* buildTree(vector<int> &num,int lef原创 2015-04-01 09:50:53 · 414 阅读 · 0 评论 -
POJ 2828--Buy Tickets(线段树)
题意:给定一队列,允许插队原创 2014-10-01 23:45:09 · 383 阅读 · 0 评论 -
POJ 2528--Mayor's posters(线段树)
题意:给定一系列有先后顺序的数对[]原创 2014-09-28 21:07:43 · 422 阅读 · 0 评论 -
POJ 2777--Count Color(线段树)
同POJ2528,线段树的题目,此题唯一的不同就是查看颜色数目也为原创 2014-09-28 21:42:53 · 401 阅读 · 0 评论 -
LeetCode--Binary Tree Postorder Traversal(栈实现三种遍历)
题意:Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1].Note: Recursive solution is triv原创 2015-03-30 11:19:23 · 366 阅读 · 0 评论 -
POJ 2406--Power Strings(kmp算法)
题意:给定一规模为1e6的字符串,原创 2014-11-16 10:58:26 · 517 阅读 · 0 评论 -
数组指针和指针数组的区别
数组指针(也称行指针)定义 int (*p)[n];()优先级高,首先说明p是一个指针,指向一个整型的一维数组,这个一维数组的长度是n,也可以说是p的步长。也就是说执行p+1时,p要跨过n个整型数据的长度。如要将二维数组赋给一指针,应这样赋值:int a[3][4];int (*p)[4]; //该语句是定义一个数组指针,指向含4个元素的一维数组。 p=a;转载 2015-05-05 16:30:42 · 387 阅读 · 0 评论 -
Recover Binary Search Tree(BST中序遍历)
题意:Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.题解:因为BST中序遍历(左中右)的结果肯定组成一个上升序列。 如:1,2,3,4,5,6. 所以若节点有交换,则肯定有逆序对的存在。 如:1,5,3,4,2原创 2015-05-27 16:21:46 · 805 阅读 · 0 评论 -
Largest Rectangle in Histogram (栈)
题意:Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.题解:我们用栈来保存直方图中的递增子序列,若i在栈中,则在i之前比i小的直方图的下标原创 2015-05-28 11:45:19 · 443 阅读 · 0 评论 -
LeetCode--Find Minimum in Rotated Sorted Array
题意: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.You may assume no duplicate exists in the array.题解原创 2015-05-04 12:05:46 · 456 阅读 · 0 评论 -
二分查找面试
经典的面试题:get up from where you fell. 可以使用统一的最终判决,mid计算。int binarySearch_1(const vector<int>& nums, int val) { //找出最大的下标i使nums[i] == val int N = nums.size(); if (N == 0) return -1; int l原创 2015-06-04 22:25:36 · 609 阅读 · 2 评论 -
LeetCode--Merge k Sorted Lists
题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解:建立一个大小为k的最小堆,即堆顶存放最小元素,因为k个链表已排好序,借鉴归并排序的技巧,先取出堆顶的元素,再放入堆顶元素所在链表的表头的元素,再维持最小堆循环迭代。 注意直接链表存入会导致逆序原创 2015-03-30 10:05:02 · 340 阅读 · 0 评论 -
三种排序算法小结
首先是归并排序,基本思想为分治,合并的技巧比较重要,不是原址排序。代码如下;int merge(int* x,int left,int mid,int right){ int i,j,k; int L1 = mid-left+2; int L2 = right-mid+1; int* L = new int[L1]; int* R = new int[L2]原创 2015-03-15 13:55:37 · 639 阅读 · 0 评论 -
POJ 1840--Eqs
此题要求所有满足原创 2014-07-28 14:40:53 · 382 阅读 · 0 评论 -
POJ 1836--Alignment(LIS,LCS)
题意:要求一给定序列的子序列,使其中的元素至少能原创 2014-07-30 15:29:19 · 710 阅读 · 2 评论 -
poj 2513--Colored Sticks
结局方案:根据输入将棍子当成边,两端颜色为点,构成无向图。题目要求计算图中是否存在欧拉路, 存在欧拉路 => 图为连通图且,除了链两端点其他节点的度数皆为偶数,若两端节点相同,则度数 都为偶,若不同则只存在两个奇数度数节点。 同样可证明上述为必要条件,可见图论教材。简单来说,若存在一条最长链W只经过每条边依次,则端 点的边必定都在链W里,若两端点为同一节点,则此链为局部欧拉原创 2014-10-29 10:58:16 · 520 阅读 · 0 评论 -
POJ 3368--Frequent values
题意:给定一不减数组,原创 2014-11-04 15:56:06 · 360 阅读 · 0 评论 -
POJ 3264--Balanced Lineup(RMQ问题)
题意:给定一数组,原创 2014-11-04 15:27:01 · 2521 阅读 · 5 评论 -
POJ 1703--Find them, Catch them
题意:一个城市有两个犯罪团伙原创 2014-11-06 15:28:45 · 450 阅读 · 0 评论 -
POJ 2492--A Bug's Life
题意:有个专家在研究一种虫子原创 2014-11-06 15:30:08 · 473 阅读 · 0 评论 -
poj 2516--Minimum Cost
最小费用最大流问题:在最大流的基础上,使原创 2014-10-19 18:04:57 · 603 阅读 · 1 评论 -
Tarjan双连通分量算法论文翻译
Depth-First Search and Linear Graph Algorithms Tarjan在1972年发表了这篇关于线性图论算法的论文,在论文中详细分析了深度优先搜索的各种性质,并且利用其性质给出了两个应用:无向图的双连通分量以及有向图的强连通分量。 在此将对其中的双连通分量算法做出一些翻译解释。仅需要你对DFS发现时间以及后向边有了解即翻译 2014-10-23 21:12:02 · 1710 阅读 · 0 评论 -
LeetCode--Two Sum(排序or Hash)
题意:从一堆数里面找出两个数,使它们的和满足target。排序代码;class Solution {public: class node {public: int num; int index; friend bool operator< (const node& x,const node& y) {原创 2015-03-15 18:34:25 · 410 阅读 · 0 评论 -
LeetCode--Longest Substring Without Repeating Characters(字符串Hash)
题意:求出一字符串里面的最长不重复子串。 题解:设置一hash数组pos,代表子串中字符在字符串中位置,用tmpL代表遍历i个字符时以i字符为末尾的不重复子串长度。代码:class Solution {public: int lengthOfLongestSubstring(string s) { int maxL = 0; int tmpL = 0;原创 2015-03-15 19:24:13 · 358 阅读 · 0 评论 -
LeetCode--Add Two Numbers(链表)
双链表的加法。代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: Lis原创 2015-03-15 19:58:47 · 377 阅读 · 0 评论 -
LeetCode--Unique Binary Search Trees II(DP求BST)
题意:Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example, Given n = 3, your program should return all 5 unique BST’s shown below.1 3 3原创 2015-03-25 15:27:23 · 647 阅读 · 0 评论 -
将Hadoop中SequenceFile,MapFile转换为文本文件
前段时间一直在准备实习的事情,没有更新项目相关的博客。最近才通过了百度的实习生面试,面试的时候这个小项目助力不少,所以又想来补充一下前面没有分享的一些项目经验。Hadoop输入输出格式Hadoop中mapper的输入必须是(key, value)格式的。若输入文件类型为文本格式,这也是默认的输入文件类型。则key是行号,value就是这一行对应的文本。 同理reducer输出的默认格式也是文本,输原创 2015-06-15 14:36:41 · 2232 阅读 · 0 评论
分享