- 博客(30)
- 收藏
- 关注
原创 股票最大收益问题3 Best Time to Buy and Sell Stock III
问题: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 two transactions.给出一个数组,这一次,要求的
2014-02-28 21:49:36 1752
原创 Reorder List
问题:Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…思路:采用的原来的简单做法,http://blog.csdn.net/ojshilu/article/details/12222035代码:/** * Definition for singly-linke
2014-02-28 20:23:11 850
原创 单词变换距离 Word Ladder (图的最短路径)
问题:Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate
2014-02-28 20:15:53 2604
原创 蛇形螺旋矩阵的生成和遍历 Spiral Matrix
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]一个n*n矩阵像上面这样,自然数从1到n,由外到里螺旋者走,这样叫Spiral Matrix。现在有两件事:第一件事是让你生成一个这样的矩阵;第二件事是给你一个普通的矩阵,让你按照这样的顺序(Spiral Order)来遍历这个矩阵。数学上位置和数字似乎没有什么特别
2014-02-27 20:10:51 2341 1
原创 寻找最长的括号匹配 Longest Valid Parentheses
问题:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",
2014-02-27 14:02:07 1340
原创 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).思路:层序遍历的一个小小的变种。偶数层把顺序逆置
2014-02-26 21:26:10 982
原创 Triangle Path 寻找最小和路径
问题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4],
2014-02-26 20:14:01 1069
原创 实现指针引用计数的类 Reference Counter
使用一个指针指向一个动态对象,当不再使用的时候要销毁这个对象。如果会有多个指针指向同一个动态对象,那么需要知道,什么时候才能销毁这个对象。提前销毁对象可能导致指针指向并且销毁一个不存在的对象,不销毁则会是内存泄露。因此,有必要为指针设计一个引用计数机制,这个机制封装在一个类中。其实这就是一种智能指针。http://blog.csdn.net/ojshilu/article/details/19
2014-02-26 18:58:39 1698
原创 实现深拷贝的类 Deep Copy
如果一个类中有指针对象,那么在拷贝这个类的对象的时候,默认的拷贝方式是只拷贝指针本身,而不重新构建并拷贝指针所指内容。这就叫做浅拷贝Shallow Copy。如果拷贝的方式是不仅仅拷贝指针,而且把指针所指的内容也新建一份,那就叫深拷贝Deep Copy。注意两点:C++在构建默认的拷贝构造函数和赋值运算符重载的时候,对指针的拷贝都是浅拷贝;但是对数组的拷贝是深拷贝!所以,类中有数组成员和指针成
2014-02-26 18:41:42 2475
原创 字符串窗口覆盖 Minimum Window Substring
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "
2014-02-25 21:37:36 1074
原创 Partition List
问题: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 the original relative order of the nodes in ea
2014-02-25 18:41:40 770
原创 读取文件中的随机一行 Random Probability
假设有一个文本文件,文件中有若干行。要求返回随机的一行。每行的被选概率相同。两种情况:1、如果文件很大,不能全放入内存2、如果是文件流大文件的特点是不能载入内存随机读取,文件流的特点是只能读取一次。//伪代码i = 1chosen_line = ""while line has next:if random() < 1/i: # random return
2014-02-25 10:49:01 4031 4
原创 backtrack - linux 简介
BackTrack是基于ubuntu的信息安全方面功能超强的linux发行版,它里面集合了一整系列的安全工具,在渗透测试、安全评估、信息获取等方面都有很大帮助。目前最新的版本是去年8月发布的BackTrack 5 R3。这款系统不仅可以装在PC上,还可以在ARM平台上使用,已经测试可以的有Nokia N900,motorala,索爱XperiaX10,还有motoralaXoox平板,太geek了
2014-02-24 16:39:50 1816
原创 组合生成 Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of
2014-02-24 10:59:35 939
原创 简化文件的绝对路径 Simpify Path
在操作系统中,每个文件都有一个绝对路径。并且其最简化的形式是唯一的。对于Unix系列系统,绝对路径是从根路径/开始的,用正斜杠表示。一个句点.表示当前路径,两个句点..表示上一级路径。对于一个路径字符串,从左到右依次读。下面的程序是用来简化Unix文件系统的路径字符串,使其达到最简化的程序,取出中间多余的/或者重复的层次进出。思想:用一个栈来维护绝对路径中从根到文件的纯净的层次次序,去除重
2014-02-23 14:37:18 3752
原创 找出二叉树中和为k的所有路径 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.该问题和之前的路径和问题相比,进阶了一步。之前的问题是,判断是否有这样的一条路径,返回布尔值即可。本问题是要找出所有的路径,并且把它们返回。注意:二叉树的结点元素可以是负数的,所有不走到
2014-02-23 11:51:14 3335
原创 二进制加法的实现(字符串形式) Add Binary
题目:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".思路:二进制加法和普通加法的思路没什么差别,甚至更简单。所需要注意的细节就是进位。从最低位开始,进位可能伴随计算直到最高位。所以每一位的加法运算都要将上一
2014-02-21 20:31:05 5954
原创 二叉树的层序遍历 改进版 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).思路:由于题目要求输出结果中每一层的结点是分开的。所以需要再层序遍历的过程中加入一个
2014-02-21 19:49:06 990
原创 寻找01矩阵中最大的子矩阵 Maximal Rectangle
题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.即寻找01矩阵中最大的纯1矩阵。思路:DFS/BFS的图算法难以判断所找到的聚类是否是矩形的。DP算法似乎需要很复杂的状态量。因此这两种方法我都没走到底
2014-02-20 22:27:24 2679
原创 寻找直方图中的最大矩形 Largest Rectangle in Histogram
题目:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.For example,Given height = [2,1,5,6,2
2014-02-20 22:18:35 1423
原创 插入时间段 Insert Interval
题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.
2014-02-20 21:57:43 1130
原创 直方图蓄水问题 Trapping Rain Water
问题:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1],
2014-02-20 21:45:57 2014
原创 十进制数转换为二进制数
这是个非常基础的问题。一般在计算机引论书籍中的第一章出现。对于十进制数的整数部分,一般使用除2取余法。每次模2取得余数,然后除以2。直到除成0为止。将余数倒置相连就得到整数部分对应的二进制数。除2取余法的理论依据是,整数部分的权值是2的幂数,每次除2都是将整体的权值右移一位,模2是取得最右边的数值。对于十进制数的小数部分,一般使用乘2取整法。不断乘2,如果结果小
2014-02-20 20:15:51 2057
原创 右旋单链表 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.思路:对于数组来说,右旋的方法是两轮逆转。对于链表,
2014-02-18 22:46:28 1077
原创 生成一个集合的所有子集 Subset
典型的递归状态生成问题。类似于全排列的生成问题。题目:Given a set of distinct integers, S, return all possible subsets.思路:借助一个只含0和1的数组来表示存在状态。不断切换状态进行深层递归,在递归最深处生成子集。代码:class Solution {public: vector > subset
2014-02-18 22:06:55 2985
原创 原地归并数组 Merge Sorted Array
题目源自于leetcode。题目:将有序数组B归并到有序数组A中,已知A中的剩余空间足够。思路:1、尽量不用额外空间来进行归并。2、尽可能减少移动元素的次数,最好能一次性移到最终位置上。方法:从尾部开始归并。原地进行。新数组的尾部游标是A、B数组元素之和,因此归并过程中新数组的尾部游标不会超过A数组的尾部游标。代码:class Solution {public:
2014-02-17 16:27:37 1200
原创 二叉树的中序遍历 Binary Tree Inorder Traversal
方法:二叉树的先序遍历形式有递归和非递归两种。递归方式的实现:class Solution {public: vector inorderTraversal(TreeNode *root) { vector out; fun(root, out); return out; } void fun(Tree
2014-02-17 14:34:39 1050
原创 Hash表的一种实现
Hash是在数据统计和海量数据处理中经常使用到的一个方法和数据结构。Hash支持的外部操作:插入新数据、查找数据。(一般不支持删除数据)Hash的使用包括两个重要部分:一个是Hash函数,一个是存储方法。Hash函数:把数据集的一个单元转换成hashID。比如要存储一个个字符串,就需要把字符串转换为hashID。存储方法:如何组织数据集的数据。这里涉及到一个问题就是,相同h
2014-02-17 09:56:57 924
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人