设计模式之状态模式
摘要:状态模式:state 定义:当一个对象内部状态改变时,允许改变其行为,这个对象看起来像是改变了其类。 例子: 设计模式一书中,举了TCP状态转移的例子。比如tcp连接在listen状态收到syn,并发送syn+ack时,进入syn receive 状态。当在syn receive状态收到ack 则进
阅读全文
设计模式之外观模式
摘要:外观模式: 为子系统的一组接口提供了一个一致的界面,此模式定义了一个高层接口,这个接口使得子系统更加容易的使用。 结构图: 通过外观模式, 可以大大减少客户端和子系统之间的耦合,使得复杂的子系统更加易用。
阅读全文
设计模式之模板方法模式
摘要:模板方法模式:定义操作中的算法的骨架,而将一些具体的步骤延迟到子类中去。 模板方法模式使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤。 结构图: AbstractClass 是一个抽象类,定义并实现了一个模板方法,该方法一般给出顶级的逻辑骨架,而具体的逻辑组成则在相应的抽象 操作中
阅读全文
leetcode 之 Minimum Path Sum
摘要:题目描述: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its
阅读全文
设计模式之代理模式
摘要:意图:为其他对象提供一种代理以控制对这个对象的访问。 适用性: 1. 远程代理:为一个对象在不同的地址空间提供局部代表。 (ps: 这点不理解) 2. 虚代理: 根据需要创建开销很大的对象。 如在打开一篇文档时,如果文档中有很大的图片,在一开始的时候,并不需要加载该图片,只有当视图切换下去时,图片才
阅读全文
设计模式之装饰模式
摘要:先看一个例子: (来自大话设计模式) 设计模式比较晦涩难懂, 个人感觉大话设计模式上的例子比较切近实际, 可以结合大话设计模式和gof一起看. 例子: 一个给人装饰衣物的系统, 类似qq秀的展示,可以有不同的穿衣风格.如 穿裤衩, 西装,打领带,皮靴,运动鞋等,不同风格的组合. 如果定义人的子类的话
阅读全文
leetcode 之 Longest Increasing Subsequence
摘要:题目描述: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The lon
阅读全文
leetcode 之Maximum Product Subarray
摘要:题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-
阅读全文
leetcode 之 Verify Preorder Serialization of a Binary Tree
摘要:题目描述: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null
阅读全文
leetcode 之 Combinations
摘要:描述: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: 运行结果: [[
阅读全文
leetcode 之 Kth Smallest Element in a BST
摘要:题目描述: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤
阅读全文
leetcode 之 Different Ways to Add Parentheses
摘要:题目描述: Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operato
阅读全文