自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

L.J.SHOU的专栏

博客搬家啦,新地址 ljshou.gitcafe.io

  • 博客(31)
  • 资源 (2)
  • 收藏
  • 关注

原创 Interview---一道有趣的推理题

题目描述:一个岛上

2014-02-28 16:44:04 1597

原创 Interview----判断两个链表是否相交?

题目描述:判断两个单链表是否相交?假设链表没有环。假如链表有环呢? 1.  假如没有环那么如果两个链表相交的话,必然最后的节点一定是同一个节点。所以只需要各自扫描一遍链表,找到最后一个节点,比较是否相同即可。O ( M + N)// version 1// test whether two lists are intersected// assume each

2014-02-28 12:37:20 1440

原创 数据结构《14》----并查集 Union-Find

描述:    并查集是一种描述解决等价关系。能够方便地描述不相交的多个集合。    支持如下操作    1. 建立包含元素 x 的集合  MakeSet(x)                              2. 查找给定元素所在的集合 Find(x), 返回所在集合的代表                              3. 将两个不相交的集合合并 Union

2014-02-26 19:51:24 2552 2

原创 Interview----判断整数序列是否是二叉搜索树的后序遍历结果

题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:      8    /   \  6    10 /  \    /   \5  7  9   11因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这

2014-02-26 09:04:47 1340

原创 二叉树中,找出和为某值的所有路径

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-02-24 20:48:51 1242

原创 STL 源码分析《2》----nth_element() 使用与源码分析

Select 问题: 在一个无序的数组中 找到第 n 大的元素。本文介绍 STL 算法库中 nth_elemnt 的实现代码。一、使用说明void nth_element (RandomAccessIteratorbeg,                         RandomAccessIterator nth,

2014-02-23 17:22:02 3227

原创 算法----Magic Index

给定一个数组 A,如果 某个下标 i, 满足 A[i] = i, 则 i 称为 Magic Index。现在假设 A 中的元素是递增有序的、且不重复,找出 Magic Index.更进一步,当数组中有重复的元素呢??分析:首先题目不难。最简单的当然是 扫描一遍数组,当然这个 O(N)的算法不是最优的。进一步思考,如今数组是递增的,可否采用 二分搜索,从而加速到 O(

2014-02-22 23:37:04 1621

原创 链表操作----将单链表向右旋转 K 个位置

给定一个单链表,设计一个算法实现链表向右旋转 K 个位置。 举例: 给定 1->2->3->4->5->6->NULL, K=3             则     4->5->6->1->2->3->NULL分析:1. 这道题目的关键在于找到 尾节点和旋转后新链表的尾节点。 假设是 tail, new->tail.然后只要进行 tail->next  = head;

2014-02-22 11:40:01 3346

原创 3 Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c

2014-02-21 22:43:44 1350 2

原创 Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

2014-02-21 20:34:30 998

原创 Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y

2014-02-20 23:43:21 1193

原创 Merge K sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.采用最小堆,首先将 k 个首节点放入堆中,弹出最小的节点并插入到新的链表中;                     弹出的节点如果next 非空,就将它的 下一个节点进 堆。

2014-02-20 23:04:40 2332

原创 STL 源码分析《1》---- list 归并排序的 迭代版本, 神奇的 STL list sort

SGI STL 的源代码(选自 STL 源码分析)如下:// list 不能使用 STL 算法 sort ()// 因为 STL 算法 sort() 只接受 RandomAccessIterator// 本函数采用 mergesorttemplate void list::sort () {  // 以下判断,如果是空链表, 或仅有一个元素,就不进行任何操作  if (

2014-02-20 19:38:16 2380

转载 STL源码分析----神奇的 list 的 sort 算法实现

STL中有一个std::sort算法,但它是不支持std::list的,因为list不提供RandomIterator的支持,但list自己提供了sort算法,把list的元素按从小到大的方式来排序,代码长度到不长,但真是难以读懂,后来扣持了一下午终于搞明白了,贴个总结上来。list::sort的代码如下(sgi stl):[cpp] view plai

2014-02-19 20:23:24 2842 1

原创 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)

普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间。题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个数组记录下来。。// copyright @ L.J.SHOU Feb.17, 2014// design a stack// min, push, pop take O(1)

2014-02-19 08:48:29 3394

原创 Longest Palindromic Substring

Description:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

2014-02-16 20:54:06 947

原创 Insertion sort list

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* in

2014-02-16 17:11:40 941

原创 Sort List

Sort a linked list in O(n log n) time using constant space complexity.采用 归并排序。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN

2014-02-16 17:10:06 1226

原创 Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

2014-02-16 15:00:27 1022

原创 Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating let

2014-02-16 13:14:58 1097

原创 微软面试100题---将 二叉搜索树 转化成 有序的双向链表

Description:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。二叉搜索树和双向链表的定义与实现参考: http://blog.csdn.net/shoulinjun/article/details/18449445                                                                 http://blog

2014-02-15 22:52:30 1120

原创 Longest Consecutive Sequence

Description:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence

2014-02-15 20:10:14 937

原创 算法----序列和的 top N

Description:两个长度为 n 的数组 A 和 B, 各从中选出一个元素相加 A[i] + B[j], 求 top n 小的那些和。http://www.cnblogs.com/372465774y/archive/2012/07/09/2583866.html

2014-02-14 13:08:02 2908

原创 Max Points On a Line

Max Points on a LineTotal Accepted: 4246 Total Submissions: 42602 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.分析: 任取两点组成的直线,看有多少点在其上面

2014-02-13 10:04:39 1463

原创 Sqrt(x)

解法1: 牛顿切线法解法2: 二分法

2014-02-12 21:59:09 1057

原创 First Missing Positive

Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant spa

2014-02-12 21:33:32 956

原创 数据结构《13》----二叉树 Morris 前序遍历

本文分析三种二叉树的后序遍历的方法: 1. 递归 O(n) 时间复杂度, O(n) 空间复杂度 2. 迭代(用栈) O(n) 时间复杂度, O(n) 空间复杂度 3. Morris 后序遍历 O(n) 时间复杂度, O(1) 空间复杂度

2014-02-12 14:02:42 1223

原创 数据结构《12》----二叉树遍历的非递归版本(不用栈)

二叉树遍历的非递归版本除了可以使用 stack 外, 还可以采用 parent 指针和标记来实现。二叉树节点的数据结构如下:/*** struct TreeNode * {* char value; * TreeNode *parent;* TreeNode *left_child;* TreeNode *right_child;* bool visite

2014-02-12 11:53:21 2524 1

原创 数据结构《11》----二叉树遍历的非递归版本(栈)

二叉树的前序,中序,后序遍历的非递归版本有许多种。较为简单的是采用栈,来模拟递归。此外,还可以采用标记,或者父亲指针来实现。前序和中序相对比较简单,代码仅仅相差一行。后序相对比较复杂,需要记录右子树是否已经访问。前序遍历:void PreOrderVisitStack(struct TreeNode * root){ stack s; while(root

2014-02-12 11:08:03 1813

原创 数据结构《10》----二叉树 Morris 中序遍历

无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度。Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), 本质上其实就是利用二叉树中 n+1 个指向NULL的指针。关于 线索二叉树 见 http://blog.csdn.net/shoulinjun/article/details/19037819

2014-02-11 19:33:08 6444 1

原创 数据结构《9》----Threaded Binary Tree 线索二叉树

Definition:"A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be

2014-02-10 22:30:01 3537 5

doxygen ppt

doxygen是自动化的文档产生工具,该ppt让你能快速掌握

2015-05-05

空空如也

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

TA关注的人

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