- 博客(39)
- 资源 (15)
- 收藏
- 关注
原创 [LeetCode]Word Break
Question:Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",
2014-04-28 18:26:42
860
原创 [LeetCode]Linked List Cycle
Question:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Answer
2014-04-28 01:05:34
850
原创 [LeetCode]Linked List Cycle II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *de
2014-04-28 00:49:19
771
转载 [LeetCode]Max Points on a Line
Question:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.分析:任意一条直线都可以表述为y = ax + b假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有
2014-04-27 02:58:28
578
原创 [LeetCode]Evaluate Reverse Polish Notation
Question:Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples:
2014-04-27 01:56:11
651
原创 [LeetCode]Reverse Words in a String
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes
2014-04-26 23:17:41
570
原创 虚函数和类的多态性
多态特性让程序员省去了细节的考虑,提高了开发效率,使代码大大的简化,当然虚函数的定义也是有缺陷的,因为多态特性增加了一些数据存储和执行指令的开销,所以能不用多态最好不用。(为了实现virtual函数,类中间必须要增加一个pointer指向虚函数表,这样增大了类的体积。所以没有必要的话,还是不要随意声明virtual的析构函数。普遍的规则是只有当类当中有virtual的函数时,析构函数才声明为vir
2013-12-12 01:10:25
734
原创 [LeetCode] Reorder List
Question:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4},
2013-11-23 01:04:42
902
原创 [LeetCode] Binary Tree Preorder Traversal
Question:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: R
2013-11-22 02:26:51
900
原创 [LeetCode] Binary Tree Postorder Traversal
Question:Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Re
2013-11-21 22:55:58
840
原创 [LeetCode] LRU Cache
Question:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of
2013-11-21 01:40:06
1241
原创 24点算法
计算24点最简单有效的方法就是递归穷举法。假如数组number[COUNT ]存放了要计算的数值(COUNT可以设为4或者其他)。算法基本思想:第一步:先从数组中选取两个数值number[i]和number[j],然后分别用"加/减/乘/除"进行计算,将得到的结果保存到数组中并替换number[i]的值,然后用数组最后一个值number[n-1]替换number[j]的值(即令numb
2013-11-19 00:26:17
1864
原创 KMP算法中的前缀函数
虽然在上一篇文章中已经全面介绍了KMP算法和前缀函数的计算过程。但是鉴于前缀函数的计算比较难以理解,这里再专门详细解释一下KMP算法中next[]数组的含义和实现过程,帮助加深理解。简单的说,前缀函数主要是求出模式串中的next数组,那么什么是模式串呢?模式串模式串的概念很简单。举个例子:“给出一个字符串 T,再给出 n 个字符串 S1、S2...Sn,问 S1、S2...Sn 中有哪些是
2013-11-18 00:48:58
3272
翻译 KMP算法总结
【KMP算法简介】 KMP算法是一种改进后的字符串匹配算法,由D.E.Knuth与V.R.Pratt和J.H.Morris同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法)。通过一个辅助函数实现跳过扫描不必要的目标串字符,以达到优化效果。 【传统字符串匹配算法的缺憾】 Bi
2013-11-17 21:00:29
1191
原创 [LeetCode] Implement strStr() 字符串子串
Question:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.Solution (C++):class Solution {public: char *s
2013-11-17 18:12:46
809
原创 [LeetCode] Spiral Matrix 螺旋输出矩阵
Question: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8
2013-11-17 01:55:29
1091
转载 C++中的smart pointer简单实现
Why Smart Pointer?为什么需要智能指针?因为c++的内存管理一直是个令人头疼的问题。假如我们有如下person对象:每个person有自己的名字,并且可以告诉大家他叫什么名字:////a person who can tell us his/her name.//#include#includeusing namespace std;clas
2013-11-16 22:46:53
1095
原创 指向继承类的基类指针解引用后,是否还具备多态性
指向继承类的基类指针解引用后,是否还具备多态性? 如下例:#include using namespace std;class Base{ public: int a; virtual void fun() { cout << "Base" << endl; }}; class Extend : public Base{ public: int b; vir
2013-11-16 22:24:50
1274
转载 类的对象和类的指针的区别
如下程序: #include #include using namespace std; class Student { public: static int number; string name; public: Student() { } void set(string str) { name = str;
2013-11-16 21:50:31
835
原创 [LeetCode] Generate Parentheses
Question:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "(
2013-11-16 17:53:06
711
原创 [LeeCode] Insertion Sort List
Question:Sort a linked list using insertion sort.Solution:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),
2013-11-16 05:23:07
826
原创 [LeetCode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.解法:游标指针从头节点向后移动,当指向尾节点时,得到链表长度len
2013-11-16 04:30:22
763
转载 [LeetCode]判断回文数(Palindrome Number)
题目来源:Leetcode(www.leetcode.com)Question:Determine whether an integer is a palindrome. Do this without extra space.Solution:First, the problem statement did not specify if negative in
2013-11-16 03:28:31
1165
原创 一道关于signed和unsigned的微软面试题
题目:unsigned int i=3;cout问输出结果是多少?我觉得大部分人的第一反应是-3。但是结果却不是这样的,写了个程序,运行了一下,输出结果是:4294967293。很诡异的一个数字,为什么会是这么个奇怪的数字呢?当你发现这数的十六进制数是FFFFFFFD时,你就离答案很近了... 这个涉及到了混合着不同数据类型的表达式中的数据类型的转换问题。在总结转换问题之前,
2013-11-15 15:43:21
1074
原创 排序算法总结
一、冒泡排序基本思想是:两两比较相邻记录的关键字,如果反序则交换冒泡排序时间复杂度最好的情况为O(n),最坏的情况是O(n^2) 改进思路1:设置标志位,明显如果有一趟没有发生交换(flag = false),说明排序已经完成改进思路2:记录一轮下来标记的最后位置,下次从头部遍历到这个位置就Ok 二、直接插入排序将一个记录插入到已经排好序的有序表中, 从而得到一个新的,
2013-11-15 15:28:05
931
原创 判断两个矩形是否重叠/相交
题目:有两个矩形,别且已知两个矩形的左上角和右下角顶点的坐标值。如何判断两个矩形是否重叠/相交。分析:首先要明确一点,据题意,每个矩形只知道左上角和右下角的点,所以题目暗示(或者说默认)矩形的边与坐标轴平行,否则仅知两个顶点无法确定一个唯一的矩形。矩形A内的任意点(x,y),包括四边上的点,应满足如下不等式组Xa1 ≤ x ≤ Xa2 ①Ya1 ≤ y ≤ Ya
2013-11-15 15:20:55
5335
转载 有一亿个随机数,不排序如何找出其中位数
题目:在一个文件中有 10G个整数,乱序排列,要求找出中位数。内存限制为 2G。只写出思路即可(内存限制为 2G的意思就是,可以使用2G的空间来运行程序,而不考虑这台机器上的其他软件的占用内存)。 关于中位数:数据排序后,位置在最中间的数值。即将数据分成两部分,一部分大于该数值,一部分小于该数值。中位数的位置:当样本数为奇数时,中位数=(N+1)/2 ;当样本数为偶数时,中位数
2013-11-15 15:13:35
3427
原创 C++中 public,protected, private 访问标号
第一:private, public, protected 访问标号的访问范围。private:只能由1.该类中的函数、2.其友元函数访问。不能被任何其他访问,该类的对象也不能访问。protected:可以被1.该类中的函数、2.子类的函数、以及3.其友元函数访问。但不能被该类的对象访问。public:可以被1.该类中的函数、2.子类的函数、3.其友元函数访问,也可以由
2013-11-15 14:55:02
973
原创 最大堆(max-heap)和最小堆(min-heap)
在STL中,二叉堆是通过priority_queue的类模板实现的,标准头文件是。在STL中实现的是最大堆(max-heap)。成员函数有:void push (const Object & x);const Object & top() const;void pop();bool empty();void clear();优先队列模版用如下参数初始化:项类型,容器类型(几乎总
2013-11-15 14:51:40
8639
翻译 优先队列(堆)
优先队列和队列一样优先队列也支持入队和出队操作,不过区别在于优先队列的出队操作出队的是优先级最高的元素,这里以元素的大小来判定任务的优先级,越小,优先级越高。优先队列的模型如下图: 基于这种优先队列的模型,我们可以多种方法对其进行实现,一种常用的方法就是使用链表以O(1)执行 插入操作,,遍历最小元素并删除花费O(N)时间。基于优先队列的
2013-11-15 00:51:37
1923
转载 AVL树的插入和删除
1. 概述AVL树是最早提出的自平衡二叉树,在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis。AVL树种查找、插入和删除在平均和最坏情况下都是O(log n),增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。本文介绍了AVL树的设计思想和基本操作。2. 基本术语
2013-11-13 19:31:58
2519
原创 寻找两个相交链表的第一个公共节点
Q:已知有两个链表,他们可能相交于某一点,求出该点。方法1. 最简单的方法就是先顺序访问其中一个链表,在每访问一个节点时,都对另外一个链表进行遍历,看节点是否相等 直到找到一个相等的节点位置,如果链表长度分别是m,n 则时间复杂度为O(mn);方法2. 对于第一个链表,每访问一个节点,对该节点做标记。访问第二个链表,如果该元素已经访问,则第一个这样的元素就是所求点。
2013-11-13 14:59:52
1089
翻译 检验某符号(如圆括号,方括号,花括号等)是否都成对出现
检验某个符号(如圆括号,方括号,花括号等)是否都成对出现。如,[()]是合法的,但[(])是非法的。这个算法的最简单方式是用栈实现,具体思路如下:做一个空栈。读入字符至文件尾。如果字符是一个开放符号,则将其压入栈中。如果字符是一个封闭符号那么若栈为空,则报错;若栈不为空,则将栈元素弹出。# 如果弹出的符号不是对应的开放符号,则报错。在文件尾,如果栈非空则
2013-11-13 14:45:43
1871
原创 中缀表达式转后缀表达式算法及实现—栈的应用
我们在数学中常见的计算式,例如2+(3*4)叫做中缀表达式。表达式中涉及到了多个运算符,而运算符之间是有优先级的。计算机在计算并且处理这种表达式时,需要将中缀表达式转换成后缀表达式,然后再进行计算。中缀表达式:0.3/(5*2+1) 的等价后缀表达式是:0.3 5 2 * 1 + / 中缀表达式转后缀表达式遵循以下原则: 1.遇到操作数,
2013-11-10 22:33:07
1468
原创 Reverse a string and reverse a sentence
#include #include #include #include #include //include reverse()#include using namespace std;int main(){ //用STL库里的函数reverse()翻转字符串; string str = "I love you!"; cout << "Original string:
2013-11-06 13:12:16
826
原创 The problem of Money Changes
/* * Input: n: the money you want to change; unit: starts with the maximum change's unit, e.g. 50 (if there are changes 1, 5, 10, 25, 50); Output: ways: the number of ways of chaning the mon
2013-11-06 01:53:02
782
原创 The number of nodes within k-hops in a n-D Torus
Torus is a widely used interconnection in high-performance computing systems. Torus can be built as 1D, 2D, 3D, 4D, 5D, 6D, and even higher dimensional topology, and they can be both server-centric an
2013-11-06 01:30:27
964
HiCut算法代码
2013-11-16
OpenFlow and SDN-Network Substrade for the Cloud
2012-03-22
复数集合的运算C++实现
2011-05-22
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人