- 博客(386)
- 资源 (3)
- 收藏
- 关注
原创 [LeetCode] Restore IP from string 从字符串恢复IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example: Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does
2014-12-07 13:43:58 689
原创 [LeetCode] Interleaving String 交叉字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return
2014-12-07 10:16:21 845
原创 [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?思路:用滚动数组,从后往前加
2014-12-07 08:52:47 542
原创 [LeetCode] Unqiue Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, given n = 3, your program should return all 5 unique BST's shown below. 1 3
2014-12-07 06:32:55 525
原创 [LeetCode] Unqiue Binary Search Trees I
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \
2014-12-06 13:51:54 543
原创 [LeetCode] Populate the next right pointer in binary tree II
这篇文章给出的算法,只适用于complete binary tree;如果是任意形状的二叉树,则不适用。下面给出一个算法,适用于任意形状的二叉树。算法一:思路见geekforgeeks。该算法被leetcode给出了超时的错误。void connect(TreeLinkNode *root) { // main function if(root==NULL)
2014-12-06 12:04:42 549
原创 [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-12-06 05:01:18 577
原创 [LeetCode] Subsets II 子集
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplica
2014-12-06 01:18:47 497
原创 [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
2014-12-05 07:34:09 544
原创 [LeetCode] distinct subsequence
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non
2014-12-05 04:43:54 770
原创 [LeetCode] 路径和 Path Sum
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-12-04 23:40:29 578
原创 [LeetCode] surrounded region包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O X
2014-12-04 01:15:28 816
原创 [LeetCode] 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], ret
2014-12-03 12:35:03 735
原创 [LeetCode] 发糖果 Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one cand
2014-12-03 11:03:27 813
原创 [LeetCode] flatten binary tree 扁平化二叉树
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1
2014-12-03 10:27:24 1518 1
原创 [LeetCode] Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.For example, given s = "aab", Return [ ["aa","b"],
2014-12-03 09:11:06 650
原创 [LeetCode] Best Time to Buy and Sell Stock II
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 as many transactions as you like (ie, buy one a
2014-12-03 07:26:52 655
原创 从根到叶子的和 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 tota
2014-12-03 06:26:54 610
原创 [LeetCode] word break 字符串的划分
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2014-12-02 13:52:53 659
原创 只出现一次的数
一个数组中,一个数字只出现了一次,其余数字都出现了三次。找出只出现一次的数字。思路:计算机是用二进制表示数字。如果我们把所有数字的第 ith 个bit是1的个数除以3,那么只会有两个结果 0 或 1. 这个结果就是那个single number在第i个bit上的值。一个直接的实现就是用大小为 32的数组来记录所有 位上的和。int singleNumber(int A[], int n
2014-12-02 10:33:18 560
原创 二叉树最大路径和 Binary Tree Max Path Sum
本题有两个相关问题:1)所求路径可以位于任意两个节点之间;2)所求路径位于两个叶子节点之间。下面给出问题1)的解法。问题2)的解法可以根据解法1)很容易得出。问题1)的解法:遍历所有节点,求出经过每一个节点的最大路径,最后再求出经过每个节点的最大路径的最大值。用递归的思路,自底向上递归。下面是代码: void maxPathSumUtil(Node* root, int& l
2014-12-02 06:49:58 2167
原创 [LeetCode] 重排链表 reorder linked list
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it t
2014-12-02 05:16:05 772
转载 median of two sorted array
转自:http://blog.csdn.net/zxzxy1988/article/details/8587244题目是这样的:给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素。另外一种更加具体的形式是,找到所有元素的中位数。本篇文章我们只讨论更加一般性的问题:如何找到两个数组中第k大的元素?不过,测试是用的两个数组的中位数的题目,Leetcode第4题 Median
2014-12-01 07:45:56 1073
翻译 在排序数组中找到第k个元素 find the k-th element in two sorted arrays
在排序数组中找到第k个元素 find the k-th element in two sorted arrays给定两个数组A和B,长度分别是m和n。找出把A和B合并成一个排序数组后的第k个元素。假设A和B中没有重复元素。下面给出一个时间复杂度是Lg(m)+Lg(n)的算法。We try to approach this tricky problem by comparing
2014-12-01 06:47:04 1171
原创 [LeetCode] 在矩阵中查找字符串 Search a string in a matrix of chars
在矩阵中查找字符串 Search a string in a matrix of chars [Leetcode 124: word search]Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequent
2014-11-30 06:27:11 1923
原创 [leetcode] 区间合并问题 Merge Interval
Given a collection of intervals, merge all overlapping intervals.类似问题1:区间覆盖问题相关问题2:Rectangle Area IIFor example, given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].来源:leetcode思路:用...
2014-11-30 04:49:46 2897
翻译 把零移动到数组的一端 Move all zeroes to end of array
把零移动到数组的一端 Move all zeroes to end of array给定一个数组,把数组中所有的 0 移动到数组的右端,并保持其他非零元素的相对顺序。例如数组 {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, 把 0 移动到右端,并保持相对顺序,结果是{1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}.下面给出一个O(n)算法,空间复杂度
2014-11-29 10:57:21 659
原创 与二分搜索相关的几个问题
在先升后降的数组中查找 Search for an element in an array which has elements who’s values are first increasing 在旋转过的排序数组中查找 Search in a rotated array在排序数组中查找某个值出现的次数,元素可以重复出现。
2014-11-29 05:12:29 781
原创 三角形的数目 Count the number of possible triangles
给定一个无序数组,数组的每个元素代表了三角形可能的边长,找出所有可能的三角形的数目。注意:三角形的边长必须满足1)a+b>c; 2) b+c>a; 3) c+a > b.详见:http://www.geeksforgeeks.org/find-number-of-triangles-possible/代码如下:// Program to count number of triangle
2014-11-29 04:56:00 830
翻译 二叉树的高度 - 父节点序列 Find Height of Binary Tree by Parent Array
二叉树的高度 - 父节点序列 Find Height of Binary Tree by Parent ArrayA given array represents a tree in such a way that the array value gives the parent node of that particular index. The value of the root node
2014-11-29 03:47:24 648
转载 二叉树的串行化 Serialization of Binary Tree
二叉树的串行化 Serialization of Binary Tree转自:http://www.geeksforgeeks.org/serialize-deserialize-binary-tree/Serialization is to store tree in a file so that it can be later restored. The structure o
2014-11-28 07:21:09 2124
原创 找出整数的开方的floor -- 找出最近的平方数
Implement a fast integer square root function that takes in an integer and returns another integer that is the floor of the square root of the input.用二分搜索的思想,但是要注意本算法的递归搜索区间和二分搜索递归区间的区别。下面的正确性已经得到
2014-11-28 04:41:04 1276
原创 是否是二叉搜索树 check if it is binary search tree
是否是二叉搜索树 check if it is binary search treegeekforgeeks 给出了一个自顶向下的递归算法。自顶向下的思路:从根节点开始,向左右子树进发,逐步缩小范围。我给出一个自下朝上的递归算法。思路是:先判断左子树和右子树是不是BST。如果有一个不是,那么整个树就不是。如果两个子树都是,那么再看根节点值的大小。#include#includ
2014-11-27 11:26:18 704
原创 把两个链表加起来 add two numbers represented by two linked lists
把两个链表加起来 add two numbers represented by two linked lists如果从链表的头到尾是数字的低位到高位的方向,这道题非常好做,详见:http://www.geeksforgeeks.org/add-two-numbers-represented-by-linked-lists/。如果从链表的头到尾是数字的高位到低位的方向,那么可以先把这两个链表
2014-11-27 09:40:57 547
原创 [LeetCode] Next Permutation - Next bigger number with the same set of digits
下一个有着同样数字的数 Next bigger number with the same set of digits转自 http://stackoverflow.com/questions/9368205/given-a-number-find-the-next-higher-number-which-has-the-exact-same-set-of-digiI just
2014-11-27 07:31:25 1160
原创 最低公共祖先 lowest common ancestor
最低公共祖先 lowest common ancestorThe idea is to traverse the tree starting from root. If any of the given keys (n1 and n2) matches with root, then root is LCA (assuming that both keys are present). If
2014-11-27 06:38:28 670
原创 [LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
非递归中序遍历二叉树 non recursive inorder traversal 用两个 while 循环来做, 思路如下.1) Create an empty stack S.2) Initialize current node as root3) Push the current node to S and set current = current->left until
2014-11-27 00:30:00 1266
原创 在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST
在二叉搜索树里,寻找两个节点 Find a pair with given sum in a Balanced BST给定一个平衡二叉搜索树和一个目标和sum,写一个函数,如果有一对节点的和等于目标和, 返回true,否则返回false。预期时间复杂度是O(n),并只只能有O(logN)的额外的空间可以使用。修改二叉搜索树是不允许的。注意平衡BST的身高是O(LOGN)。一个更好的解决
2014-11-26 11:08:38 1186 1
转载 相同数目的0和1的子数组 Largest subarray with equal number of 0s and 1s
相同数目的0和1的子数组 Largest subarray with equal number of 0s and 1sFollowing is a solution that uses O(n) extra space and solves the problem in O(n) time complexity.Let input array be arr[] of size n and
2014-11-26 07:57:43 786
原创 [LeetCode] Merge Two Sorted Lists 归并两个排好序的单链表
归并两个排好序的单链表给定两个排过序的单链表,头指针分别是head1和head2,把他们合并成一个排序的单链表。思路:用递归的方法,把单链表head1中的节点,依次插入head2中。#include #include #include #define MAX 256using namespace std;struct Node{ int data;
2014-11-26 01:21:42 521
Monte carlo模拟的Mathematica代码
2010-04-10
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人