06 2015 档案
摘要:key points:1 conversion between different types2 overflow 会有比较多corner cases需要考虑3 iterationrelated questions:Median of Two Sorted ArraysReverse Integer...
阅读全文
摘要:今天完整刷完leetcode除了自己看不懂follow不下来的 199道题。。。纪念一下Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find ...
阅读全文
摘要:ref http://blog.csdn.net/u013325815/article/details/41892135The API:int read4(char *buf)reads 4 characters at a time from a file.The return value is t...
阅读全文
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete a...
阅读全文
摘要:Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain lead...
阅读全文
摘要:Given a list of non negative integers, arrange them such that they form the largest number.For example, given[3, 30, 34, 5, 9], the largest formed num...
阅读全文
摘要:The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a...
阅读全文
摘要:完全没有想法实际上是in order traversal而已public class BSTIterator { public TreeNode crt; public Stack st = new Stack(); public BSTIterator(TreeNode ...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the valu...
阅读全文
摘要:Given an integern, return the number of trailing zeroes inn!.ref 抄自达达http://www.danielbit.com/blog/puzzle/leetcode/leetcode-factorial-trailing-zeroes首...
阅读全文
摘要:Related to questionExcel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A ...
阅读全文
摘要:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 ->...
阅读全文
摘要:Compare two version numbersversion1andversion2.Ifversion1>version2return 1, ifversion1v2) return 1; if(v1<v2) return -1; ...
阅读全文
摘要:Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 ...
阅读全文
摘要:Given two strings S and T, determine if they are both one edit distance apart.Hint:1. If | n – m | is greater than 1, we know immediately both are not...
阅读全文
摘要:网上抄的http://blog.csdn.net/whuwangyi/article/details/43186045Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
阅读全文
摘要:Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes...
阅读全文
摘要:话说我们昨晚去看了侏罗纪公园,原来xd就是我最爱的巨幕,来美国这么多年了,怀念和小星星一路成长,一路看电影的经历,avatar是我们在美国一起看的第一部电影。老美看完好片子,还喜欢鼓个掌。这题one pass in place就是麻烦,要多做个string来判断单词要用到sb的insert,不过可以...
阅读全文
摘要:很简单的stack,注意operation orderpublic class Solution { public int evalRPN(String[] tokens) { if(tokens ==null|| tokens.length==0) return 0; ...
阅读全文
摘要:http://bookshadow.com/weblog/2015/01/19/leetcode-binary-tree-postorder-traversal/http://bookshadow.com/weblog/2015/01/19/binary-tree-post-order-traver...
阅读全文
摘要:ref linkhttp://blog.sina.com.cn/s/blog_eb52001d0102v1si.html摘抄感谢以下文章的作者:BIN大神的大总结:http://blog.csdn.net/fightforyourdream/article/details/16843303 面试...
阅读全文
摘要:树的遍历当初觉得难死,现在还好public class Solution { public ArrayList preorderTraversal(TreeNode root) { ArrayList res = new ArrayList(); if(root=...
阅读全文
摘要:Given an array of integers, find out whether there are two distinct indicesiandjin the array such that the difference betweennums[i]andnums[j]is at mo...
阅读全文
摘要:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired bythis...
阅读全文
摘要:还有2个queue做,没写class MyStack { public Queue q = new LinkedList(); //cc150的问题,好像 // single queue public void push(int x) { q.add((int)...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For exampl...
阅读全文
摘要:排排坐分果果,这道题还是蛮难想的,答案忘了从哪里看的了 public int candy(int[] ratings) { if(ratings == null) return 0; int len = ratings.length; int res ...
阅读全文
摘要:我觉得这道题是dp比较有趣的应用吧,但是老用不熟sigh一下public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int total=0, curRem=0, curSum=0...
阅读全文
摘要:典型的dfs,做个hashmap判断是否存过(oldnode,newnode)最后怎么把neighbor加入到hm对应node的neighbor list中比较关键public class Solution { public UndirectedGraphNode cloneGraph(Und...
阅读全文
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs...
阅读全文
摘要:正常dfspublic class Solution { public ArrayList> partition(String s) { ArrayList> res = new ArrayList>(); if(s==null||s.length()==0) re...
阅读全文
摘要: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...
阅读全文
摘要:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which rep...
阅读全文
摘要:乍一看还真以为和1 一样,呵呵当然不是啦Given two words (startandend), and a dictionary, find all shortest transformation sequence(s) fromstarttoend, such that:Only one l...
阅读全文
摘要:还是参考爱做饭同学http://www.cnblogs.com/springfor/p/3893499.htmlGiven two words (beginWordandendWord), and a dictionary, find the length of shortest transform...
阅读全文
摘要:基础但是要掌握 int a = 'A'这种写法,以及s.trim(); s = s.toUpperCase();public class Solution { public boolean isPalindrome(String s) { if(s==null ||...
阅读全文
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 ...
阅读全文
摘要:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution stil...
阅读全文
摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the orig...
阅读全文
摘要:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t...
阅读全文
摘要:就recursive做,简单题public class Solution { public int minDepth(TreeNode root) { if(root==null) return 0; if (root.left==null && root.righ...
阅读全文
摘要:简单题IGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given su...
阅读全文
摘要:超简洁的代码本来考虑是不是真的要每个点求一个maxDepth,看来是的哟public class Solution { public boolean isBalanced(TreeNode root) { if (root==null) return true; i...
阅读全文
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题解完全抄自ref的说明,感谢!“题解:先复习下什么是二叉搜索树(引自Wikipedia):二叉查找树(B...
阅读全文
摘要:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest le...
阅读全文
摘要:跟之前的解法一模一样Binary Tree Level Order Traversal I,II 不赘述public class Solution { public ArrayList> zigzagLevelOrder(TreeNode root) { ArrayList> r...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2...
阅读全文
摘要:有个问题,就是不能和same tree一样光看root 做recursive而是必须比较左右节点,因为对称是指整个树否则就会出现 1 2 2 # 3 3public class Solution { public boolean isSymmetric(TreeNode root) { ...
阅读全文
摘要:简单题ref“使用的是先序遍历,算法的复杂度跟遍历是一致的,如果使用递归,时间复杂度是O(n),空间复杂度是O(logn)。” by codegankerhttp://blog.csdn.net/linhuanmars/article/details/22839819•Time to search ...
阅读全文
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is ...
阅读全文
摘要:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only n...
阅读全文
摘要:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="...
阅读全文
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1...
阅读全文
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.2...
阅读全文
摘要:message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message contain...
阅读全文
摘要:题目:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representat...
阅读全文
摘要:complete tree,except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.from wiki本来在考虑最底层到底满不满,发现是通过...
阅读全文
摘要:来说说看问题急躁,一开始就理所当然的想问的是overlapped area,实际上是total。脑子里一堆边界条件,看了ref觉得很简洁http://blog.csdn.net/foreverling/article/details/46411069public class Solution { ...
阅读全文
摘要:典型的dfs题,建议在其他dfs之前做public class Solution { public ArrayList> combine(int n, int k) { ArrayList> res = new ArrayList>(); if(nn) return...
阅读全文
摘要:public class Solution { public int minDistance(String word1, String word2) { if(word1==null||word1.length()==0) return word2.length(); ...
阅读全文
摘要:几个corner cases要清楚refhttp://www.cnblogs.com/springfor/p/3869666.html引用一个用法注意“判断字符串相等与否要用.equals(),因为是引用类型。要注意split函数是可以split出空字符的,例如://b/ 会被split结果为[""...
阅读全文
摘要:public class Solution { public int climbStairs(int n) { int[] res =new int[n+3]; // 因为如果n<3,至少希望把前三个位置做出来 res[0] = 0; res[1]...
阅读全文
摘要: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 matr...
阅读全文
摘要:http://www.quora.com/Whats-the-best-way-to-delete-duplicates-from-a-sorted-array-in-math-O-log-N-math-time-and-math-O-1-math-spacehttps://mtatar.wordp...
阅读全文
摘要:如果NetworkX不好用,pip uninstall 然后install1) 看每个node的social情况In [1]: import networkx as nxIn [2]: nx.read 比如nx.read_adjlist nx.read_dot nx.read_edgelist这个时...
阅读全文
摘要:ref: Learning Ipython for interactive computing and data visualizationcode website:http://ipython.rossant.net.启动:基础,可以用powershell在window下实现近似linux操作no...
阅读全文
摘要:看cast type比较重要,public class Solution { public int sqrt(int x) { // 巧妙使用二分法 // ref http://www.cnblogs.com/springfor/p/3857772.html ...
阅读全文
摘要:还没刷,感觉好复杂,先把看到的做法记录一下http://blog.csdn.net/kenden23/article/details/18696083
阅读全文
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie...
阅读全文
摘要:简单题public class Solution { public int lengthOfLastWord(String s) { s = s.trim(); if(s==null||s.length()==0) return 0; int len ...
阅读全文
摘要:数据处理题目摘抄自爱做饭 http://www.cnblogs.com/springfor/p/3870929.html“题解:pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。当然n是可...
阅读全文
摘要:IGiven a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3...
阅读全文
摘要:题解抄自“http://simpleandstupid.com/2014/10/26/wildcard-matching-leetcode-%E8%A7%A3%E9%A2%98%E7%AC%94%E8%AE%B0/”“Implement wildcard pattern matching with ...
阅读全文
摘要:依然觉得蛮无聊的,注意返回的是第n个(从1开始算起)stringThe count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off a...
阅读全文
摘要:这道题最大的问题是位置容易想不清楚public class Solution { public int longestValidParentheses(String s) { // http://blog.csdn.net/linhuanmars/article/details/...
阅读全文
摘要:不甚知道意义的题目,不过数组reverse值得复习一下哟public class Solution { public void nextPermutation(int[] num) { //这个题目意义何在啊唉 if (num==null || nu...
阅读全文
摘要:Divide two integers without using multiplication, division and mod operator.比有趣在于1 提速过程 2怎么判断符号正负public class Solution { public int divide(int divi...
阅读全文
摘要:https://www.youtube.com/watch?v=izMKq3epJ-QBoyer-Moore algrt 关于skip的部分很重要Implement strStr().Returns the index of the first occurrence of needle in hay...
阅读全文
摘要:dfs 的一个关键是termination conditionpublic class Solution { public ArrayList generateParenthesis(int n) { ArrayList res = new ArrayList(); ...
阅读全文
摘要:http://blog.kaggle.com/author/kevin-markham/
阅读全文