- 博客(183)
- 收藏
- 关注
原创 Additive number
Additive number is a positive integer whose digits can form additive sequence.A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent num
2015-11-18 20:33:44
564
原创 Same Tree
Same TreeSep 3 '12Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the
2015-11-16 20:05:57
354
原创 Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).The above rectangle (with the red bo
2015-11-15 18:03:47
386
原创 段错误调试
我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的。实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难 免会在此处犯些小错误,而通常这些错误又是那么的浅显而易于消除。但是手工“除虫”(debug),往往是效率低下且让人厌烦的,本文将就"段错误"这个 内存访问越界的错误谈谈如何快速定位这些"段错误"的语句。下面将就以下的一个存在段错误的程序介绍几种调试方
2015-11-12 10:46:28
808
原创 Wildcard Matching
isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aab", "c*a*b") → falsebool
2015-11-10 20:06:33
328
原创 Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan
2015-11-10 20:00:52
423
转载 LeetCode-Valid Number - 有限状态机
判断合法数字用有限状态机,非常简洁,不需要复杂的各种判断!先枚举一下各种合法的输入情况:1.空格+ 数字 +空格2.空格+ 点 + 数字 +空格3.空格+ 符号 + 数字 + 空格4.空格 + 符号 + 点 + 数字 +空格5.空格 + (1, 2, 3, 4) + e + (1, 2, 3, 4) +空格组后合法的字符可以是:
2015-11-05 17:48:22
316
原创 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",
2015-11-04 20:31:09
290
原创 Palindrome Partitioning I,II(DFS,DP)
分析:题意给我们一个字符串,求出所有这个字符串的子串,并且子串都为回文字符串的情况,输出它的集合结果解题思路:(跟DFS深度遍历有点像!)字符串Str = "aab";分析了题目的数据之后,我们知道它的结果,可能是 a, a, b 或者 aa, b 这样的情况!也就是说,我们需要去考虑 i = 1, 2 .... n 的情况,比如Str = "
2015-11-04 20:28:10
285
原创 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0
2015-11-03 21:59:11
281
原创 Triangle
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], [
2015-11-01 19:22:03
300
原创 Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2015-10-29 17:40:45
226
原创 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I
2015-10-28 20:21:49
295
原创 栈与队列相互实现
用两个队列模拟一个堆栈:队列a和b(1)取栈顶元素: 返回有元素的队列的首元素(2)判栈空:若队列a和b均为空则栈空(3)入栈:a队列当前有元素,b为空(倒过来也一样)则将需要入栈的元素先放b中,然后将a中的元素依次出列并入列倒b中。(保证有一个队列是空的)(4)出栈:将有元素的队列出列即可。比如先将1插入队a中 ,现在要将2入栈,则将2
2015-10-27 19:40:22
306
原创 Find Median from Data Stream
这道题的关键在于要维护两个堆,一个大顶堆,一个小顶堆,这样保证在插入的时候就已经是有序的。对于大顶堆,堆顶一定是堆中最大的值。对于小顶堆,堆顶一定是堆中最小的值。现在我们假设一个有序序列,并把这个有序列分为两半,左边一半为较小数,右边一半为较大数。我们把较小数用大顶堆存储,较大数用小顶堆来存储。那么大顶堆的根一定是较小数里面的最大数,小顶堆的根一定是较大数里面的最小数,也就
2015-10-21 16:56:35
336
原创 merge k sorted lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.solution:快排的思想public class Solution { ListNode merge2Lists(ListNode list1, ListNode list2) {
2015-10-20 15:29:20
258
原创 Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest co
2015-10-19 16:25:32
264
原创 DFS之Restore IP Addresses
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
2015-10-17 18:08:31
304
原创 Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the
2015-10-16 17:01:33
254
原创 DFS求回文串
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","
2015-10-15 10:11:36
379
原创 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
2015-10-15 09:49:56
302
原创 数组第k大的数
快排的思想:class Solution {public: int findKthLargest(vector& nums, int k) { int L = 0, R = nums.size() - 1; while (L < R) { int left = L, right = R; int key
2015-10-14 19:42:48
441
原创 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers wi
2015-10-13 14:52:22
350
转载 DFS经典组合问题
下面将遇到的可以用递归求解的问题归纳于此1. CombinationGiven two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[[2,4
2015-10-12 15:35:43
753
原创 卡特兰数类似问题通解
描述:给定一个非负整数n,生成n对括号的所有合法排列。解答:该问题解的个数就是卡特兰数,但是现在不是求个数,而是要将所有合法的括号排列打印出来。 该问题和《编程之美》的买票找零问题一样,通过买票找零问题我们可以知道,针对一个长度为2n的合法排列,第1到2n个位置都满足如下规则:左括号的个数大于等于右括号的个数。所以,我们就可以按照这个规则去打印括号:假设在位置k
2015-10-12 10:52:39
435
原创 走格子经典题
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the
2015-10-12 10:39:02
1598
原创 Maximum Subarray
int max(int a,int b){ return a>=b?a:b;}int maxSubArray(int* nums, int numsSize) { int sum = nums[0] , maxSum = nums[0]; int i=0; for(i = 1; i < numsSize; i++){ if(sum < 0) sum
2015-10-10 16:31:53
294
原创 long long 与 _int64使用总结
在16位环境下,int/unsigned int 占16位,long/unsignedlong占32位,在32位环境下,int占32位,unsigned int占16位,long/unsignedlong占32位何时需要使用: long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647,而unsigned范围是[0,2^32),即0~429496
2015-10-10 15:51:32
860
原创 Compare Version Numbers
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and
2015-10-09 09:07:52
278
原创 Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Examples:pattern = "abba", str = "dog cat cat dog" should return true.pattern = "abba", str = "dog cat cat fish"
2015-10-08 08:47:43
369
原创 Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate element must exist. Assume that there is only one duplicate number,
2015-09-28 15:27:20
850
原创 两个排序数组第k大的数
double findKth(int a[], int m, int b[], int n, int k){ //always assume that m is equal or smaller than n if (m > n) return findKth(b, n, a, m, k); if (m == 0) return b[k -
2015-09-25 12:06:14
936
原创 Valid Phone Numbers
题目描述:Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.You may assume that a valid phone number must
2015-09-23 10:00:41
408
原创 move zeros
solution1:双指针思路,遍历一次class Solution {public: void moveZeroes(vector& nums) { int i=0; //Not Zero int j=0; //Zero int l=nums.size(); while(j<l && nums[j]!=0)
2015-09-22 11:54:12
601
原创 Kth Smallest Element in a BST
题目描述:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note:You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:W
2015-09-22 09:11:35
335
原创 翻转二叉树
nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by Max Howel
2015-09-22 08:59:42
395
原创 在数组中找到出现频率大于1/4的数
算法的核心思想很简单,每次删去不同的4个数,最后剩下的元素有可能是频繁项。假设数组有15个元素,若一个元素的出现频率大于1/4,其必须出现4次。不妨设数组为{1,2,1,4,1,4,2,9,1,7,4,3,9,4,3},d表示删去该数。我们来模拟一下算法的过程。第一次 1d,2d,1,4d,1,4,2,9d,1,7,4,3,9,4,3 剩下 1,1,4,2,1,7,4,3,9,4,
2015-09-17 09:04:27
930
原创 Lowest Common Ancestor of a Binary Search Tree (BST)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee
2015-09-16 14:52:03
350
原创 Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O
2015-09-16 14:40:50
385
转载 子树判断
//遍历Tree1,查找与Tree2 root相同的节点 boolean HasSubtree(TreeNode root1, TreeNode root2){ boolean result = false; if(root1 != null && root2 != null){ if(root1.val == root2.val){
2015-09-16 13:22:42
437
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人