
LeetCode
文章平均质量分 65
u010850285
这个作者很懒,什么都没留下…
展开
-
裴蜀定理
在数论中,裴蜀等式或裴蜀定理是一个关于最大公约数(或最大公约式)的定理。裴蜀定理得名于法国数学家艾蒂安·裴蜀,说明了对任何整数、和它们的最大公约数,关于未知数和的线性丢番图方程(称为裴蜀等式):有整数解时当且仅当m是d的倍数。裴蜀等式有解时必然有无穷多个整数解,每组解、都称为裴蜀数,可用扩展欧几里得算法求得。例如,12和42的最大公约数是6,则方程有解。事实上有(-3)转载 2015-04-05 17:00:57 · 3225 阅读 · 0 评论 -
Maximum Gap
比较排序的时间复杂度是O(nlogn),而题目要求O(n)。可用的排序算法有桶排序、计数排序、基数排序/ 用桶排序 // 算出相邻两个桶之间的最大差值 // 如果是平均分布,则桶的数目和元素的数目相同时,其排序的时间复杂度是0(n) // 我们假设桶的个数和元素的数目相同,若是平均分布,则每个桶里有一个数,而若某个桶里有两个以上的桶时,这时必有至少一个是空桶,那么最转载 2015-03-13 15:25:24 · 441 阅读 · 0 评论 -
Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2015-03-12 16:08:01 · 386 阅读 · 0 评论 -
4Sum
public List> fourSum(int[] num, int target) { Arrays.sort(num); HashSet> hashSet = new HashSet>(); List> result = new ArrayList>(); for (int i = 0; i < num.length; i++) { for (转载 2015-03-13 09:29:28 · 382 阅读 · 0 评论 -
Median of Two Sorted Arrays
将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、证明也很简单,可以采用反证法。假设A[k/转载 2015-03-21 15:03:13 · 326 阅读 · 0 评论 -
Permutation Sequence
http://blog.youkuaiyun.com/doc_sgl/article/details/12840715The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get th转载 2015-03-11 09:29:42 · 377 阅读 · 0 评论 -
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 dupli原创 2015-03-10 16:58:41 · 341 阅读 · 0 评论 -
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转载 2015-03-10 15:10:56 · 379 阅读 · 0 评论 -
Unique Binary Search Trees II
http://www.tuicool.com/articles/JJVbA3U题目 :Given n , generate all structurally unique BST's (binary search trees) that store values 1... n .For example,Given n = 3, your program sh转载 2015-03-10 11:21:35 · 379 阅读 · 0 评论 -
Unique Binary Search Trees [动态规划]
如果集合为空,只有一种BST,即空树,UniqueTrees[0] =1如果集合仅有一个元素,只有一种BST,即为单个节点UniqueTrees[1] = 1UniqueTrees[2] = UniqueTrees[0] * UniqueTrees[1] (1为根的情况)+ UniqueTrees[1] * UniqueTrees[0] (2为根的情况。转载 2015-03-10 10:48:55 · 525 阅读 · 0 评论 -
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-03-07 18:33:03 · 390 阅读 · 0 评论 -
2Sum/3Sum/3SumClosest/4Sum系列问题
leetcode(http://leetcode.com/onlinejudge)上有好几道关于数组中几个数据和为target的题目。恰好正在看剑指offer中“和为s的两个数组这章”,据此思想,leetcode上的三道题目都被我解决了。总结一下。1.twoSum:输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s。既然题目中已经提到了“递增数组”,那么肯定不会暴转载 2015-03-13 09:18:16 · 460 阅读 · 0 评论 -
【LeetCode】Largest Number 解题报告
http://blog.youkuaiyun.com/ljiabin/article/details/42676433【题目】Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5,转载 2015-03-13 15:46:57 · 390 阅读 · 0 评论 -
扩展欧几里德算法
欧几里德算法欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。gcd函数就是用来求(a,b)的最大公约数的。 gcd函数的基本性质:性质一:gcd(a,b)=gcd(b,a)=gcd(-a,b)=gcd(|a|,|b|)证明略。 性质二:gcd(a,b)=gcd(b,a mod b)证明:a可以表示成a = kb + r转载 2015-04-05 17:07:32 · 455 阅读 · 0 评论 -
Wildcard Matching
Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the转载 2015-03-05 15:57:08 · 361 阅读 · 0 评论 -
sort list
Sort a linked list in O(n log n) time using constant space complexity.ListNode getMiddleOfList(ListNode head) { ListNode slow = head; ListNode fast = head; while原创 2015-03-31 21:21:06 · 386 阅读 · 0 评论 -
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 dupli原创 2015-03-18 10:46:41 · 326 阅读 · 0 评论 -
动态规划
用动态规划的方法,就是要找到其转移方程式,也叫动态规划的递推式,动态规划的解法无非是维护两个变量,局部最优和全局最优。其中局部最优的意思就是到目前这个数字为止,其最好的值,整体最优就是整体的最好值。例子:Maximum Product Subarray Find the contiguous subarray within an array (containing原创 2015-03-18 11:07:09 · 346 阅读 · 0 评论 -
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 "()",转载 2015-03-23 10:19:04 · 349 阅读 · 0 评论 -
Sudoku Solver
public class Solution { public void solveSudoku(char[][] board) { solve(board); } private boolean solve(char[][] board) { for (int i = 0; i转载 2015-03-23 10:05:24 · 364 阅读 · 0 评论 -
Copy List with Random Pointer
1. 在OldList中的每个结点后,插入一个CopyNode,这个结点的Random域和Next域与OldList中的被拷贝Node的Random域和Next一致,然后让被拷贝结点的Next域指向CopyNode结点,这样先创建出OldList中结点对应的CopyNode结点。2. 由于所有的CopyNode都已经创建出来了,我们就可以调整这些CopyNode真正的Random域的值了。转载 2015-03-14 20:27:01 · 359 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
O(n2)的解法是最为直接的:1.枚举每一个子串;2.扫描子串,求出满足题意的最长长度。int max = 0;char *t;for (int i = 0; i t = &s[i]; int j = 0; while (j j++; } if (j > max) {转载 2015-03-14 09:44:49 · 336 阅读 · 0 评论 -
Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st转载 2015-03-13 19:41:26 · 309 阅读 · 0 评论 -
Palindrome Partitioning I II
Palindrome Partitioning I 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 =转载 2015-03-27 11:32:40 · 373 阅读 · 0 评论 -
LeetCode Sort List 解题报告
http://blog.youkuaiyun.com/worldwindjp/article/details/18986737Sort a linked list in O(n log n) time using constant space complexity.http://oj.leetcode.com/problems/sort-list/解题报告:就是对一个链表进行归转载 2015-03-07 15:01:32 · 365 阅读 · 0 评论 -
Combinations
Given 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], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2015-03-06 15:09:56 · 301 阅读 · 0 评论 -
Suffix Tree 后缀树
http://blog.youkuaiyun.com/g9yuayon/article/details/2574781在pongba的讨论组上看到一道Amazon的面试题:找出给定字符串里的最长回文。例子:输入XMADAMYX。则输出MADAM。这道题的流行解法是用后缀树(Suffix Tree)。这坨数据结构最酷的地方是用它能高效解决一大票复杂的字符串编程问题:在文本T里查询T是否包转载 2014-12-18 19:20:08 · 770 阅读 · 0 评论 -
Java Longest Palindromic Substring(最长回文字符串)
http://blog.youkuaiyun.com/soszou/article/details/37312317如果一个字符串从左向右写和从右向左写是一样的,这样的字符串就叫做palindromic string,如aba,或者abba。本题是这样的,给定输入一个字符串,要求输出一个子串,使得子串是最长的padromic string。下边提供3种思路1.两侧比较法以abba这转载 2014-12-18 19:16:48 · 361 阅读 · 0 评论 -
Regular Expression Matching
题解:1、当下一个不为*时,p[i]和s【j】要么相等,要么p【i】是点2、当下一个为*时,采用贪婪试探的方法,逐个往后试探public class Solution { public boolean isMatch(String s, String p) { if(p.length() == 0) return s.原创 2015-01-26 21:51:53 · 310 阅读 · 0 评论 -
【LeetCode】Min Stack 解题报告
http://blog.youkuaiyun.com/ljiabin/article/details/40982153Hints:Consider space-time tradeoff. How would you keep track of the minimums using extra space?Make sure to consider duplicate elemen转载 2014-12-03 11:10:30 · 418 阅读 · 0 评论 -
Summary for leetcode 2Sum, 3Sum, 4Sum, K Sum
http://tech-wonderland.net/blog/summary-of-ksum-problems.htmlOverviewI summarize various solutions (sort, hash, etc) for the leetcode 2Sum, 3Sum, 4Sum problems as well as how to optimize and转载 2014-12-17 09:21:37 · 869 阅读 · 0 评论 -
Insert Interval
http://my.oschina.net/jdflyfly/blog/284520Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were ini转载 2014-12-17 15:19:22 · 305 阅读 · 0 评论 -
LeetCode:Maximum Product Subarray
http://www.cnblogs.com/bakari/p/4007368.html题目:Maximum Product SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest product. For exampl转载 2014-10-24 18:44:53 · 319 阅读 · 0 评论 -
如何判断链表中是否有环
如何判断链表中是否有环分类: 数据结构与算法学习笔记2012-11-12 16:04 1606人阅读 评论(0) 收藏 举报今天面试被问住了,很惭愧啊,回来上网查了一下思路。自己写了点程序。1.如何判断是否有环?如果有两个头结点指针,一个走的快,一个走的慢,那么若干步以后,快的指针总会超过慢的指针一圈。2.如何计算环的长度?第一次相遇(超一圈)时开始计数,第转载 2014-05-28 17:09:33 · 523 阅读 · 0 评论 -
Given two binary trees, write a function to check if they are equal or not.
Given 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 same value.方法一:原创 2014-05-23 17:07:22 · 1484 阅读 · 0 评论 -
Given an array of integers, every element appears twice except for one. Find that single one.
fangimport java.util.Arrays;public class Solution { public int singleNumber(int[] A) { Arrays.sort(A); int i=0; for(i=0;i if(i+1 i=i+1转载 2014-05-23 10:57:18 · 755 阅读 · 0 评论 -
String to Integer (atoi)
原题链接:http://oj.leetcode.com/problems/string-to-integer-atoi/ 这道题还是对于Integer的chǔ理,在Reverse Integer这道题中我有提到,这种题的考察重点并不在于问题本身,而是要注意corner case的chǔ理,整数一般有两点,一个是正负符号问题,另一个是整数越界问题。思路比较简单,就是先去掉多余的空格字符,然后读符转载 2014-12-04 21:50:26 · 852 阅读 · 0 评论 -
Leetcode: Valid Number
这是一道检查字符串输入是否为合法的题目。基本规则是按照科学计数法,所以会出现的特殊字符有以下几个:符号位‘+’,‘-’,小数点‘.’,还有‘e’和‘E’,剩下的就只有数字0-9了,其他字符如果出现就是非法字符,返回false。数字字符在哪里出现都是ok的,我们主要考虑几个特殊字符的情况。对于小数点出现的时候,我们要满足一下这些条件:(1)前面不能有小数点或者‘e’和‘E’;(2)前一位是数字(转载 2014-12-05 16:58:54 · 419 阅读 · 0 评论 -
Word Break II
动态规划import java.util.*;public class Solution7 {List records = new ArrayList();public List wordBreak(String s, Set dict) {if (canBreak(s, dict)) {dfsSearch(s, dict, "");}return原创 2015-03-07 16:50:34 · 325 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV
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 k transactions.解题思路采用动转载 2015-03-26 19:53:27 · 379 阅读 · 0 评论