数据结构&算法
文章平均质量分 70
wongson
这个作者很懒,什么都没留下…
展开
-
Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayMy SubmissionsQuestion Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new原创 2015-10-14 12:34:01 · 534 阅读 · 0 评论 -
求二叉树最远距离
int max=Integer.MIN_VALUE; int maxHeightSum(TreeNode root) { if (root == null) return 0; int maxL = Math.max(maxHeightSum(root.left), 0); int maxR = Math.max(maxHeightSum原创 2015-04-25 19:14:19 · 653 阅读 · 0 评论 -
变位分词
static int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; static public int mapping(String s) { int val = 1; for原创 2015-04-24 22:44:32 · 727 阅读 · 0 评论 -
判断二叉树是否对称
static boolean isMirror(TreeNode left, TreeNode right) { if (left == null && right == null) return true; else if (right == null) { return false; } else if (left ==原创 2015-04-24 21:42:04 · 1583 阅读 · 0 评论 -
LRU cache
实现lru 缓存LRU Cache Total Accepted: 35641 Total Submissions: 241374My SubmissionsQuestion Solution Design and implement a data structure for Least Recently Used (LRU) c原创 2015-04-09 15:28:19 · 891 阅读 · 0 评论 -
BST迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and has原创 2015-04-16 16:19:26 · 1176 阅读 · 0 评论 -
复制无向带环图
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each原创 2015-04-13 16:06:04 · 1047 阅读 · 0 评论 -
先序和中序建立二叉树
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in th原创 2015-04-23 12:50:36 · 1667 阅读 · 0 评论 -
合法ip序列
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order原创 2015-04-23 17:35:14 · 743 阅读 · 0 评论 -
层次遍历二叉树
public class Solution { static class Queue { static final int MAX_SIZE = 1 << 10; private TreeNode list[]; int head; int tail; int cap; Queue() {原创 2015-04-10 17:29:18 · 647 阅读 · 0 评论 -
平衡二叉树判定
1. int height(TreeNode root) { if (root == null) return 0; return Math.max(height(root.left), height(root.right)) + 1; } public boolean isBalanced(TreeNode root) { if原创 2015-04-29 18:44:48 · 621 阅读 · 0 评论 -
将升序数组转化为平衡二叉树
public class Solution { TreeNode convert(int num[], int l, int r) { if (l >= r) return null; int m = (l + r) / 2; TreeNode left = convert(num, l, m); TreeNode root原创 2015-04-29 20:13:17 · 1728 阅读 · 0 评论 -
文件路径化简
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did you consider the case where path原创 2015-04-29 16:37:14 · 822 阅读 · 0 评论 -
Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.解决思路首先想到的还是把链表当做有向图来深度遍历,但原创 2015-10-13 22:01:42 · 693 阅读 · 0 评论 -
弗洛伊德判圈算法
先来看链表是否有环的判断设链表起始节点为H,回路起始节点为S,两指针第一次相遇节点为M。设回路的长度为c,S到M的距离为c1,M到S的距离为c2。设H到S的距离为d。设hare每次移动2,tortoise每次移动1。移动k次后两者相遇。不难发现,H到M的距离为k。为了便于理解,请参考下图:由已知条件,可得到下列等式:hare和tortoise相遇时,hare套了tortoi原创 2015-10-08 21:29:29 · 2640 阅读 · 0 评论 -
Merge k Sorted Lists
法1.分而治之ListNode merge2(ListNode a, ListNode b) { ListNode dummyHead, curr; dummyHead = new ListNode(-1); curr = dummyHead; while (a != null && b != null) {原创 2015-10-18 12:29:52 · 541 阅读 · 0 评论 -
Searching a 2D Sorted Matrix Part II
二维整型矩阵Table [m][n]. 满足Table[i][j] ≤ Table[i][j + 1], Table[i][j] ≤ Table[i + 1][j]在此中进行查找元素。1.阶梯搜索从右上角或者左下角开始,如下图红线所示的查找13的过程bool stepWise(int mat[][N_MAX], int N, int targ翻译 2015-09-10 12:27:25 · 642 阅读 · 0 评论 -
Gas Station
Gas StationThere are N gas stations along a circular route, where the amount of gas at stationi is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to trave原创 2015-08-26 20:55:39 · 478 阅读 · 0 评论 -
用dfs实现拓扑排序
拓扑排序,详见算法导论There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which原创 2015-05-16 00:27:56 · 1958 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions.Note:You may原创 2015-05-03 14:43:02 · 1345 阅读 · 0 评论 -
N皇后问题
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2015-05-10 12:57:12 · 585 阅读 · 0 评论 -
矩形相交判断超简单方法
设矩形A(xa1,ya1),(xa2,ya2),B(xb1,yb1),(xb2,yb2)其中:xa1xb1先看线段相交的判断:La:xa1,xa2 xa1Lb:xb1, xb2 xb1考虑对立面(只有两种情况):不相交的条件:xa2xb2由摩根律,得出相交条件:xa2>=xb1&&xa1矩形A B相交的条件为:分别在X和Y轴上的投影相交原创 2015-04-27 19:26:44 · 8216 阅读 · 0 评论 -
二分搜索
Sqrt(x) Implement int sqrt(int x).Compute and return the square root of x.原创 2015-04-01 18:20:52 · 482 阅读 · 0 评论 -
google 2013 校园招聘笔试题
2013 google校园招聘笔试题1、 单项选择题1.1如果把传输速率定义为单位时间内传送的信息量(以字节计算)多少。关于一下几种典型的数据传输速率:1.使用USB2.0闪存盘,往USB闪存盘上拷贝文件的数据传输速率2.使用100M以太网,在局域网内拷贝大文件时网络上的数据传输速率3.使用一辆卡车拉1000块单块1TB装满数据的硬盘,以100转载 2012-10-16 15:41:36 · 859 阅读 · 0 评论 -
C++智能指针
智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露。它的一种通用实现技术是使用引用计数(reference count)。智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象共享同一指针。每次创建类的新对象时,初始化指针并将引用计数置为1;当对象作为另一对象的副本而创建时,拷贝构造函数拷转载 2012-10-14 13:49:27 · 680 阅读 · 0 评论 -
判断链表是否带环
有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环。问题:1、如何判断一个链表是不是这类链表?2、如果链表为存在环,如果找到环的入口点?解答:一、判断链表是否存在环,办法为:设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fas转载 2012-09-26 00:41:44 · 4035 阅读 · 2 评论 -
编写类String的构造函数、析构函数和赋值函数。
已知类String的原型为: class String{public: String(const char *str = NULL);// 普通构造函数 String(const String &other); // 拷贝构造函数 ~ String(void); // 析构函数 String & operate =(c转载 2012-08-24 17:26:11 · 554 阅读 · 0 评论 -
关于拷贝构造函数和赋值运算符
重点:包含动态分配成员的类 应提供拷贝构造函数,并重载"="赋值操作符。以下讨论中将用到的例子:01.class CExample02.{03.public:04.CExample(){pBuffer=NULL; nSize=0;}05.~CExample(){delete pBuffer;}06.void Init(int n)转载 2012-08-24 17:27:58 · 515 阅读 · 0 评论 -
uva 120
BackgroundStacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also impo原创 2012-09-03 10:19:22 · 1459 阅读 · 0 评论 -
Windows程序窗口层次Z-order,顶层窗口,前台窗口,后台窗口,兄弟窗口,活动窗口
窗口Z次序:表明了重叠窗口堆中窗口的位置,这个窗口堆是按照一个假象的轴定位的,这个轴是从屏幕向外伸展的Z轴,上面的窗口覆盖下面的窗口。Windows系统管理三个独立的Z次序----一个用于顶层窗口、一个用于兄弟窗口、还有一个用于最顶层窗口,最顶层窗口覆盖其他非最顶层窗口,而不管它是不是活动窗口或是前台窗口。应用程序通过设置WS_EX_TOPMOST风格创建最顶层窗口。一般情况下,W转载 2012-08-23 19:13:29 · 3058 阅读 · 0 评论 -
群里面的一道题
已知:#include using namespace std;int cnt = 1;void cal(int start){ for (int i = start; i cnt ++; cal(i+1); }}int main() { cal(0); cout return 0原创 2012-05-11 18:34:17 · 1108 阅读 · 0 评论 -
归并排序(简单实现)
#include using namespace std;int a[10] = {2, 3, 5, 8, 1, 3, 6, 7, 9, 10};//合并a[first...mid-1]和a[mid...num-1],O(n)void merge(int first, int mid, int num){ if (mid == first || mid == num - 1)原创 2012-05-07 20:50:05 · 544 阅读 · 0 评论 -
2012腾讯实习生笔试题
/*问题描述:两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]...*a[N-1]/a[i];要求:1.不准用除法运算2.除了循环计数值,a[N],b[N]外,不准再用其他任何变量(包括局部变量,全局变量等)3.满足时间复杂度O(n),空间复杂度O(1)*/分析:以i为界,分两步计算1)b[i]=a[0]*...*a[i-1原创 2012-04-18 15:08:38 · 1043 阅读 · 0 评论 -
海豚算法循环左移
int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b%a, a); } void ROL(int *a, int n, int k) { if (k == 0 || n == 0) { return; } k %= n; int原创 2012-09-03 20:10:19 · 1679 阅读 · 0 评论 -
题目: 一个骰子,6面,1个面是 1, 2个面是2, 3个面是3, 问平均掷多少次能使1、2、3都至少出现一次。
题目:一个骰子,6面,1个面是 1, 2个面是2, 3个面是3,问平均掷多少次能使1、2、3都至少出现一次。解:(没学过《组合数学》的请略过)设P(N=n)表示第n次(n>2)抛出后1,2,3都出现的概率,问题要求n的期望E(N=n).掷1的概率p=1/6,掷2的概率q=1/3,掷3的概率r=1/2.写程序求解#include using names原创 2012-09-13 13:05:43 · 17960 阅读 · 3 评论 -
统计一个数字在排序数组中出现的次数
题目描述:统计一个数字在排序数组中出现的次数。输入:每个测试案例包括两行:第一行有1个整数n,表示数组的大小。1第二行有n个整数,表示数组元素,每个元素均为int。第三行有1个整数m,表示接下来有m次查询。1下面有m行,每行有一个整数k,表示要查询的数。输出:对应每个测试案例,有m行输出,每行1整数,表示数组中该数字出现的次数。样例输原创 2012-09-12 19:24:04 · 3492 阅读 · 0 评论 -
有关C语言运算符优先级和结合律的思考
(1) b=*p++; (2)b=(*p)++; (3)b=++*p; (4)b=++(*p); (5)b=*++p; (6)b=*(++p);上面6式子,哪些是等价的?反汇编代码:b=*p++;00411BD9 mov eax,dword ptr [p] 00411BDC mov ecx,dwo原创 2012-09-21 15:45:41 · 1509 阅读 · 0 评论 -
最小的K个数
输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。输入:每个测试案例包括2行:第一行为2个整数n,k(1第二行包含n个整数,表示这n个数,数组中的数的范围是[0,1000 000 000]。输出:对应每个测试案例,输出最小的k个数,并按从小到大顺序打印。分析:用排序的话排序复杂O(nlogn原创 2012-09-20 18:36:01 · 1865 阅读 · 1 评论 -
关于C++中函数指针的使用(包含对typedef用法的讨论)
(一)简单的函数指针的应用。//形式1:返回类型(*函数名)(参数表)char (*pFun)(int);char glFun(int a){ return;}void main(){ pFun = glFun; (*pFun)(2);} 第一行定义了一个指针变量pFun。首先我们根据前面提到的“形式1”认识到它是一个指向某种函数的指针转载 2012-10-11 11:45:50 · 849 阅读 · 0 评论 -
C语言 switch 语句用法
The C switch StatementThe switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.翻译 2012-10-10 19:45:04 · 19535 阅读 · 2 评论