摘要: 判断链表中是否有环存在: 设置两个指针p,q,开始时两个指针均指向head; p每次向前移动一步,q每次移动两步;不断移动,若在移动过程中p和q重合(p == q)则说明有环(证明略)。 代码: struct ListNode { int val; struct ListNode... 阅读全文
posted @ 2015-08-22 23:33 sunp823 阅读(249) 评论(0) 推荐(0) 编辑
摘要: Head First一书中对于策略(strategy)模式的正式定义是:策略模式定义了算法族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。 为了介绍这个算法,书中讲了一个例子: 在某个游戏中,有各种各样的鸭子,系统的内部设计使用了严格的OO技术,设计了一个鸭子(... 阅读全文
posted @ 2015-08-22 21:52 sunp823 阅读(314) 评论(0) 推荐(0) 编辑
摘要: 外观模式(Facade)是结构型设计模式,外观模式就是为一个子系统中的多个类提供统一接口的方法,把一个组件单元构建为一个子系统有助于减少软件的复杂性,而且可以使子系统与其他组件的通信关系更加间接、易控制,可以最大限度的降低组件间的耦合度。达到这一个目标的最好方法就是引进一个外观类... 阅读全文
posted @ 2015-08-22 01:07 sunp823 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 寻找最近公共祖先节点(LCA) 在一棵二叉树中,对于节点X和节点Y,X和Y的LCA是这棵树中X和Y的第一个共同祖先。寻找公共节点的算法思路很简单:对于节点x和y,找到树的根节点分别到x节点和y节点的路径(并不是遍历),并分别记录在两个数组中(或其他),数组中索引为0的元素为树的根节点,索引越小... 阅读全文
posted @ 2015-07-13 15:39 sunp823 阅读(2796) 评论(0) 推荐(0) 编辑
摘要: 题目: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / op... 阅读全文
posted @ 2015-06-30 23:47 sunp823 阅读(232) 评论(0) 推荐(0) 编辑
摘要: 题目: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the... 阅读全文
posted @ 2015-06-30 23:43 sunp823 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a... 阅读全文
posted @ 2015-06-04 14:42 sunp823 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve ... 阅读全文
posted @ 2015-05-29 19:48 sunp823 阅读(122) 评论(0) 推荐(0) 编辑
摘要: 题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 len2?len1:len2; //所有子串转化为整数形式 int num1[] = ne... 阅读全文
posted @ 2015-05-27 23:22 sunp823 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given [100, 4, 200, 1, 3... 阅读全文
posted @ 2015-05-20 18:07 sunp823 阅读(92) 评论(0) 推荐(0) 编辑