自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Peep Hole

给思考留下时间和空间。

  • 博客(84)
  • 收藏
  • 关注

原创 iostat -x 1 查看磁盘的IO负载

Linux系统出现了性能问题,一般我们可以通过top.iostat,vmstat等命令来查看初步定位问题。其中iostat可以给我们提供丰富的IO状态数据$ iostat -x -1 avg-cpu:  %user   %nice %system %iowait  %steal   %idle          10.43    0.00    1.51    1.

2014-10-16 17:54:12 13155

原创 deque迭代器失效的困惑?

在实现LRU算法的时候lru_list 开始用的是deque 但是因为害怕其在插入删除上的迭代器失效情况的诡异情况。遂用list代替之。在数据量比较大的时候性能不是很好。性能优化分析的时候决定用deque替换回来。于是对deque迭代器失效的情况好好研究了一下:c++ primer如此写道:1.在deque容器首部或者尾部插入元素不会使得任何迭代器失效。 2.在其首部或尾部

2014-10-14 16:20:43 2044

原创 mprotect() failed: Cannot allocate memory

遇到这个问题是在测试项目的性能时发现的,每个对象分配一页大小的空间然后mprotect() 保护起来,当系统分配3W多个页的时候会出现这个问题。google到在一份邮件列表中也曾提到该问题.https://sourceware.org/ml/libc-help/2010-04/msg00026.html摘抄部分如下:-----------------------

2014-10-10 13:41:02 4930

原创 LeetCode之Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total numb

2014-07-03 23:35:26 1012

原创 Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?有f(n) = f(n - 1) + f(n

2014-07-03 21:10:10 1123

原创 Gray Code

The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of

2014-07-03 20:34:41 843

原创 The Singleton Pattern 单例模式

单例模式确保一个类只有一个实例,并提供一个全局访问点。用来创建独一无二的,只能有一个实例的对象的入场劵。类图非常简单: 实现:

2014-04-16 12:18:27 860

原创 C++智能指针

Smart_ptr is the same as a normal pointer, but it provides safety via automatic memory. It avoids dangling pointers, memory leaks, allocation failures etc. The smart pointer must maintain a si

2014-04-11 22:26:21 851

原创 LeetCode之Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting

2014-04-11 18:49:17 805

原创 LeetCode之Reverse Integer

Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if

2014-04-11 16:10:13 727

原创 LeetCode之Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?class Soluti

2014-04-11 15:56:40 770

原创 LeetCode之Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solut

2014-04-11 15:31:20 808

原创 LeetCode之Pow(x, n)

Implement pow(x, n).Recursive solution is easier to understand. It uses the divide-and-conquer approach, which also runs in  time. The formula is shown below:class Solution {public:

2014-04-11 14:43:59 913

原创 LeetCode之Roman to Integer

Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.转换规则:wiki/罗马数字拼写规则罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述

2014-04-11 13:33:49 1249

原创 回溯算法

搜索算法介绍(1)穷举搜索(Exhaustive Search)(2)盲目搜索(Blind or Brute-Force Search)深度优先(DFS)或回溯搜索(Backtracking);广度优先搜索(BFS);迭代加深搜索(Iterative Deepening);分枝限界法(Branch & Bound);博弈树搜索(α-β Search)(3)启发式搜索(H

2014-04-07 20:38:37 2827 1

原创 The Decorator Pattern 装饰者模式

本模式可以称为“给爱用继承的人一个全新的设计眼界”。利用对象组合的方式,做到在运行时装饰类,使得能够在不修改任何底层代码的情况下,给对象赋上新的职责。装饰者模式:动态的将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。来看看类图:装饰者和被装饰者有相同的超类型,因此可以在任何需要原始对象的场合,可以用装饰过的对象替代它。可以

2014-04-07 13:25:11 1028

原创 Facade Pattern 外观模式

目的:让接口更简单。为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。它将一个或者数个类的复杂的一切都隐藏在背后,只显露出一个干净美好的外观。与适配器模式的差异在于它们的意图: 适配器模式的意图是:“改变”接口使之符合客户的期望。 而外观模式的意图是:提供子系统的一个简化接口。这个模式的类图:

2014-04-05 15:04:22 939

原创 LeetCode之Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary

2014-04-05 14:26:37 859

原创 LeetCode之Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f

2014-04-05 14:18:18 927

原创 LeetCode之Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},

2014-04-05 13:12:15 870

原创 LeetCode之Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 /

2014-04-05 12:57:57 976

原创 The Adapter适配器模式

目的:将一个方块放入一个圆洞中包装某些对象,让它们的接口看起来不像自己而像是别的东西,这样就可以在设计中,将类的接口转换成想要的接口,以便实现不同的接口。适配器模式将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。这让用户可以从实现的接口中解耦。如果一段时间以后我们想要改变接口,适配器可以将改变的部分封装起来,客户就不必为了对应不同的接口而每次跟着

2014-04-04 17:00:26 894

原创 The Template Method Pattern 模板方法模式

模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类在不改变算法结构的情况下,重新定义算法中的某些步骤。模板就是一个方法。具体地说,这个方法将算法定义成一组步骤,其中的任何步骤都可以是抽象的,由子类负责提供部分实现。钩子的真正目的:钩子可以让子类实现算法中的可选部分,或者在钩子对于子类的实现并不重要的时候,子类可以对钩子置之不理。钩子让

2014-04-04 14:47:33 911

原创 The State Pattern 状态模式

A little known fact: the Strategy and State Patterns were twins separated at birth. As you know, the Strategy Pattern went on to create a wildly successful business around interchangeable algorithms

2014-04-04 14:22:47 1240

原创 The Strategy Pattern 策略模式

策略模式定义了算法族,分别封装起来,让它们之间可以相互替换,此设计模式让算法的变化独立于使用算法的客户。The Strategy Pattern defines a family of algorithms,encapsulates each one, and makes them interchangeable.Strategy lets the algorithm vary i

2014-04-04 14:14:37 1013

原创 LeetCode之Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.与Construct Binary Tree from Preorder and Inorder Traversal

2014-04-03 20:37:20 844

原创 LeetCode之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 the tree.preorder:DLRinorder:  LDR/** * Definition for bina

2014-04-03 16:59:57 1337

原创 LeetCode之Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \

2014-04-03 16:02:47 1043

原创 LeetCode之Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all vali

2014-03-31 15:16:20 763

原创 LeetCode之Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the a

2014-03-31 13:57:15 791

原创 LeetCode之Search 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).You are given a target value to search. If found in the array return it

2014-03-31 13:47:14 785

原创 LeetCode之Sqrt(x)

Implement int sqrt(int x).Compute and return the square root of x.注意:越界问题class Solution {public: int sqrt(int x) { if(x<0) return -1; if(x==0||x==1) return x; i

2014-03-31 12:10:16 944

原创 LeetCode之Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in t

2014-03-26 18:39:14 717

原创 LeetCode之Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each

2014-03-26 18:05:21 796

原创 LeetCode之Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.

2014-03-26 17:52:17 688

原创 LeetCode之Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.此题与Maximum Depth of Binary Tree有

2014-03-24 22:51:15 906

原创 LeetCode之Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for binary

2014-03-24 22:28:26 1041

原创 LeetCode之Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum

2014-03-24 22:15:05 772

原创 LeetCode之Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total

2014-03-24 22:01:50 1158

原创 蓄水池抽样(Reservoir Sampling)分析

其目的在于从包含n个项目的集合S中选取k个样本,其中n为一很大或未知的数量,尤其适用于不能把所有n个项目都存放到主内存的情况。应用的场景一般是数据流的情况下,由于数据只能被读取一次,而且数据量很大,并不能全部保存,因此数据量N是无法在抽样开始时确定的,但又要保持随机性。在高德纳的计算机程序设计艺术中,有如下问题:“可否在一未知大小的集合中,随机取出一元素?”

2014-03-24 14:47:07 999

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除