- 博客(53)
- 资源 (3)
- 收藏
- 关注
原创 [LeetCode] Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr
2013-02-28 04:38:33 1575
原创 Closest 3 Sum
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly
2013-02-28 04:12:05 639
原创 两个排序数组的中位数
给你两个排序数组,大小都是n。找到两个数组归并后的中位数。见leetcode。Question: There are 2 sorted arrays A and B of size n each. Write an algorithm to find the median of the array obtained after merging the above 2 arrays(i.e.
2013-02-28 00:21:10 2065
转载 两个排序的数组,找到第k大的元素
给你两个排序的数组,A和B,大小分别是 m 和 n。找到把 A和B 归并后的数组中,第k大的元素。(假定没有重复元素)。The trivial way, O(m + n):Merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra sp
2013-02-28 00:12:23 3130
原创 [LeetCode] 最长合法括号 longest valid parentheses
相关问题1:https://blog.csdn.net/jiyanfeng1/article/details/8068811(平衡括号问题)给你一个字符串,该字符串仅仅包含 '(‘ 和 ')',找出最长的合法括号子串。例如:"(()" 的最长合法括号子串是 “()” ,长度是2。")()())" 的最长合法括号子串是 “()()” ,长度是4。解题思路1: int lon...
2013-02-28 00:08:18 2070
原创 栈的push、pop序列
题目:输入两个整数序列。其中一个序列表示栈的push顺序,判断另一个序列有没有可能是对应的pop顺序。为了简单起见,我们假设push序列的任意两个整数都是不相等的。 比如输入的push序列是1、2、3、4、5,那么4、5、3、2、1就有可能是一个pop系列。因为可以有如下的push和pop序列:push 1,push 2,push 3,push 4,pop,push 5,pop,pop,p
2013-02-27 14:11:43 681
原创 Number of Unique BST 二叉搜索树的个数
给你一个正整数 n, 代表了BST中节点的个数。那么可以构造出多少个异构的BST。例如: n=3时,有5个异构的BST,如下所示。 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \
2013-02-27 11:57:11 2333
原创 [LeetCode] Populate the next right pointer in binary tree I
Populate the next right pointer in binary treeGiven the structure of node in binary treestructNode { Node* leftChild; Node* rightChild; Node* nextRight;}Populate the nextRight pointer
2013-02-27 11:38:31 746
原创 Sum Root to Leaf Numbers 从根到叶的和
一个二叉树的节点值的范围是0到9,从根节点到每个叶子节点,都表示一个整数。例如 1 / \2 3The root-to-leaf path 1->2 represents the number 12.The root-to-leaf path 1->3 represents the number 13.要求把这些
2013-02-27 11:11:30 688
原创 [LeetCode] 无序数组中的最长连续数列 The Longest Consecutive Sequence in an unsorted array
无序数组中的最长连续数列 The Longest Consecutive Sequence in an unsorted array给你一个无序的数组,{5, 7, 3, 4, 9, 10, 1, 15, 1, 3, 12, 2, 11},那么最长的连续数列是{1, 2, 3, 4, 5}。思路:Dump everything to a hash set.Now g
2013-02-27 09:32:21 2249
原创 [LeetCode] 比n小的所有素数
找到所有比 n 小的素数。思路详见:http://en.wikipedia.org/wiki/Sieve_of_eratosthenes/* Generate a prime list from 0 up to n, using The Sieve of Erantosthenesparam n The upper bound of the prime list (includi...
2013-02-27 09:10:36 1218
原创 螺旋遍历二叉树 Spiral-order traversal
螺旋遍历二叉树指的是,先从左往右层序打印第一层,然后从右往左层序打印第二层,再从左往右层序打印第三层,再从右往左层序打印第四层,。。。思路:可以用两个栈来做。struct Node{ int data; Node* left; Node* right; Node(int d, Node*l, Node*r):data(d), left(l), right(r){}};void
2013-02-27 09:08:06 1361
原创 最近的Fibonacci数
给你一个整数,找到离这个整数最近的Fibonacci数。思路:最近的Fibonacci数既可能比给定的整数大,还可能小于给定的整数。我们可以找到两个Fibonacci数,一个比给定数小,另一个比给定数大。然后从二者里选一个最近的。int closestFibo(int num){ int f1 = 1; int f2 = 1; while(f2<=num && f1<=num)
2013-02-27 09:04:47 1067
原创 最长公子序列 Longest Common Subsequence
设 X = (x0, x1, ... , x_(n-1) ), Y = (y0, y1, ... , y_(m-1) ), 那么用动态规划的方法,可以得到如下的阶段决策方程:代码如下:#include#include#include#include using namespace std;int LCSubsequence(char* s1, char* s2){ if
2013-02-26 02:08:11 906
原创 [LeetCode] 回文数字 Palindrome Number
给你一个正整数,如何判断它是不是回文数字?例如,12344321是回文数字,1234593不是回文数字。方法一:先把原先的数字反转,然后看反转数字和原来的数字是否相等。反转的算法如下:int reverse(int num) { assert(num >= 0); // for non-negative integers only. int rev = 0; while
2013-02-25 11:25:58 1056
原创 [LeetCode] 克隆图 Clone a Graph
克隆图 Clone a Graph给你一个图。图的节点定义如下:struct GNode{ int data; vector neighbors; GNode(int dat, int size): data(dat) { neighbors = vector(size, NULL); }};给你一个图的起始节点,要求克隆这个图。思路:可以在遍历的过程中,逐步复制
2013-02-25 10:47:32 1990
原创 把链表中的奇偶数分开
Segregate even and odd nodes in a Linked List给你一个单链表,修改此单链表,使得前面是偶数,后面是奇数。偶数间的相对顺序不变,奇数间的相对顺序不变。返回修改后的单链表的头节点。例如:Input: 17->15->8->12->10->5->4->1->7->6->NULLOutput: 8->12->10->4->6->17->15-
2013-02-25 06:06:42 4905
转载 链表:删除一个节点,如果这个节点的右边的节点更大
Delete nodes which have a greater value on right sideExamples:a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted
2013-02-25 05:55:36 587
转载 [LeetCode] merge sort for linked list
Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heaps
2013-02-25 05:23:59 1497
原创 合并两个有序链表
给你两个有序的单链表,如 5->10->15 和 2->3->20,要求把它们合并成一个单链表:2->3->5->10->15->20。用递归的方法来做:struct LNode{ int data; LNode* next; LNode(int dat, LNode* nex = NULL): data(dat), next(nex) {}};LNode* Sorted
2013-02-25 05:01:00 666
原创 XOR linked list 异或链表
异或链表是实现双链表的一种方法,可以降低空间复杂度,每个节点只有一个指针域,用于存放前一个节点地址和后一个节点地址的异或(如果XOR链表只有一个节点,那么这个节点的指针域便是NULL xor NULL =NULL)。下面的代码实现来XOR链表的插入函数。该插入函数将一个值插入到链表的前端。代码如下:struct LNode{ int data; LNode* pnx; //
2013-02-25 04:28:41 3375 2
原创 二叉树的垂直和 Vertical Sum in a Binary Tree
二叉树的垂直和 Vertical Sum in a Binary Tree给定如下二叉树: 1 / \ 2 3 / \ / \4 5 6 7这个树有5个vertical line:Vertical-Line-1 has on
2013-02-24 11:34:23 1033
原创 [LeetCode] 从排序的单链表到平衡搜索二叉树
给你一个排过序的单链表,构建一个平衡的搜索二叉树。In this method, we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in
2013-02-24 11:17:56 558
原创 二叉树变成搜索二叉树
给你一个普通的二叉树,把它变成搜索二叉树,要求不改变这个树的结构。例如,给你如下二叉树 10 / \ 2 7 / \8 4结果是: 8 / \ 4 10 / \2 7思路:1. 中序遍历该二叉树,
2013-02-24 10:40:50 1240
原创 从中序遍历序列构建最大堆
已知一个最大堆的中序遍历序里,要求恢复该最大堆。思路:找到这个序列中的最大元素,这个最大元素的左边的元素一定是左子树的元素,右边的元素一定是右子树的元素。用递归的方法,构建左子树和右子树。
2013-02-24 10:26:03 1809
原创 由前序遍历构建一个特殊的二叉树
一个二叉树的每个节点,要么有两个孩子,要么没有孩子。给你一个数组 int pre[],该数组代表这个二叉树的前序遍历序列;数组 char preLN[] 代表相应的节点是不是叶子节点,'L'表示是叶子节点,'N'表示非叶子节点。要求,恢复出原来的二叉树。思路:可以采用和 “从前序遍历序列恢复BST(线性时间复杂度)” 相似的思想来做这道题。代码如下:struct Node{
2013-02-24 09:38:42 572
原创 判断一个二叉树是否是完全二叉树
判断一个二叉树是否是完全二叉树思路:在层序遍历的过程中,找到第一个非满节点(non-full node)。满节点(full-node)指的是同时拥有左右孩子的节点。在找到第一个非满节点之后,剩下的节点不应该有孩子节点;如果有,那么该二叉树就不是完全二叉树。代码如下:bool isCompleteTree(Node* root){ queue que; que.push(root);
2013-02-24 08:22:34 3323
转载 Boundary Traversal of binary tree
Boundary Traversal of binary treeGiven a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20
2013-02-24 01:17:11 528
原创 Boundary Traversal of binary tree
Boundary Traversal of binary treeGiven a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20
2013-02-24 01:15:57 657
原创 [LeetCode] 一个BST的两个节点的值被交换,要求修正这个BST
给你一个搜索二叉树,如下: 6 / \ 10 2 / \ / \1 3 7 12可以看到 10 和 2 的位置发生了交换。要求设计算法,回复原来的BST。思路:可以借助 isValidBST 的思想,找出被交换的两个节点 。找到之后,
2013-02-24 00:48:31 1626
原创 从前序遍历序列恢复BST
相关问题:已知二叉树的前序遍历序列和中序遍历序列,要求重新恢复该二叉树。给你一个二叉搜索树的前序遍历序列,恢复此二叉搜索树文章末尾给出了一个复杂度是O(n)的算法。复杂度是O(n^2)的代码如下:#include #include #include using namespace std; struct Node{ int key; Node* left;
2013-02-23 06:01:42 1457
原创 [LeetCode] 由前序和中序序列,构建二叉树
相关问题:从BST的前序遍历序列恢复该BST已知二叉树的前序遍历序列和中序遍历序列,要求重新恢复该二叉树。#include #include #include #include using namespace std; struct Node{ char key; Node* left; Node* right; Node(char k, Node
2013-02-23 05:31:34 1505
原创 用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)本文采用STL来实现队列,并用这个队列实现Linked Complete Binary Tree的插入函数。代码非常简洁,代码如下:#include #include #include using namespace std;struct Node{ int key;
2013-02-22 02:36:34 1204
翻译 用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 通常情况下,我们是用数组来实现完全二叉树的。如果parent node的下标是 i,那么左孩子、右孩子的下标分别是 2*i+1、2*i+2。但是这篇文章将介绍如何用二叉链表实现完全二叉树。Ho
2013-02-22 01:26:23 1110
原创 找到二叉树的Ceil值
给你一个二叉树和一个数值,找出二叉树中比这个数值大的最小的值(也就是所谓的Ceil值)。代码如下:#include #include /* A binary tree Node has key, left child and right child */struct Node{ int key; Node* left; Node* right;
2013-02-22 01:07:02 625
原创 把二叉搜索树变成一个二叉树,使得原来每个节点的值 变成 (原来的值+所有大于它的值的和)
Given a Binary Search Tree (BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum of all greater keys in BST.Examples:Input: Root of following BST
2013-02-21 14:47:38 567
原创 线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
线索二叉树 Threaded Binary Tree不用递归,不用栈,遍历二叉树Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In
2013-02-21 13:08:08 676
原创 压缩字符串
给你一个字符串,比如 “AAABBBCDEEFFF”,把这个字符串变成 "A3B3CDE2F3",达到压缩的目的。用in-place的方式进行压缩,代码如下:#include using namespace std;void compress(char* str){ char prev = *str; // used to decide whether *curr is an u
2013-02-21 10:18:42 560
原创 Independent Set 独立集问题
和独立集相关的一些图论的概念如下:Independent Set 独立集问题Bipartite Graph 二分图Dominant Set 主导集Cycle Graph循环图问题:给你一个二叉树,求出该二叉树的最大独立集。思路:二叉树问题可以用递归的办法来解决,这个也不例外。更好的是,这个问题还可以用动态规划的方法来解决。设二叉树的根节点是root,那么,此二叉树的最大
2013-02-20 10:20:32 7737
Monte carlo模拟的Mathematica代码
2010-04-10
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人