摘要:
原题链接:https://leetcode.com/problems/number-of-islands/题意描述:Given a 2d grid map of'1's (land) and'0's (water), count the number of islands. An island is... 阅读全文
随笔分类 - Solving Report
[LeetCode]Binary Tree Right Side View
2015-04-06 17:36 by 庸男勿扰, 187 阅读, 收藏, 编辑
摘要:
原题链接:https://leetcode.com/problems/binary-tree-right-side-view/题意描述:Given a binary tree, imagine yourself standing on therightside of it, return the v... 阅读全文
[LeetCode]House Robber
2015-04-06 17:31 by 庸男勿扰, 251 阅读, 收藏, 编辑
摘要:
原题链接:https://leetcode.com/problems/house-robber/题意描述:You are a professional robber planning to rob houses along a street. Each house has a certain amo... 阅读全文
[微软实习生2014]K-th string
2014-05-17 18:32 by 庸男勿扰, 273 阅读, 收藏, 编辑
摘要:
很久之前的事情了,微软2014实习生的在线测试题,记录下来以备后用。题目描述:DescriptionConsider a string set that each of them consists of {0, 1} only. All strings in the set have the sam... 阅读全文
[LeetCode]Subsets II
2014-03-24 19:49 by 庸男勿扰, 237 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/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 duplicate subsets.For example,IfS=[1,2,2], a solution is:[ [2]. 阅读全文
[LeetCode]Subsets
2014-03-23 23:34 by 庸男勿扰, 258 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/subsets/题意描述:Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3],... 阅读全文
[LeetCode]Pascal's Triangle
2014-03-15 23:23 by 庸男勿扰, 124 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/pascals-triangle/题意描述:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]题解: 两端为1,中间为v[i][j] = v[i-1][j-1]+v[i-1][j]; 1 class Solution { 2 public: 3 vector > ... 阅读全文
[LeetCode]Reverse Integer
2014-03-14 23:53 by 庸男勿扰, 166 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/reverse-integer/题意描述:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought thro 阅读全文
[LeetCode]Linked List Cycle II
2014-03-14 17:12 by 庸男勿扰, 177 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/linked-list-cycle-ii/题意描述:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?题解: 这道题的v1版本是判断一个单链表是否有环,当时我们采用的是快慢指针的方法。传送门:http://www.cnblogs.com/codershell/p/3600100.. 阅读全文
[LeetCode]Binary Tree Postorder Traversal
2014-03-14 16:14 by 庸男勿扰, 118 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/题意描述:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?题解: 二叉树的后序遍历,... 阅读全文
[LeetCode]Minimum Path Sum
2014-03-14 16:06 by 庸男勿扰, 134 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/minimum-path-sum/题意描述:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.题解: 这是提到基本的动态规划题,转移方程为:dp[i][j] . 阅读全文
[LeetCode]Search Insert Position
2014-03-14 11:13 by 庸男勿扰, 127 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/search-insert-position/题意描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[ 阅读全文
[LeetCode]Linked List Cycle
2014-03-14 11:07 by 庸男勿扰, 253 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/linked-list-cycle/题意描述:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?题解: 判断一个单链表中是否有环,这是一道典型的快慢指针的题,具体概念参见百度:http://baike.baidu.com/link?url=6vrn7yMJjWonSHrzGNLZA16JoJZxrLkFuRtJH3oP3PHCBakSKKM2UQH-5xt. 阅读全文
[LeetCode]Binary Tree Preorder Traversal
2014-03-14 00:20 by 庸男勿扰, 182 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/题意描述:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?题解: 裸的二叉树遍历,没什么... 阅读全文
[LeetCode]Longest Common Prefix
2014-03-11 20:04 by 庸男勿扰, 126 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/longest-common-prefix/题目描述:Write a function to find the longest common prefix string amongst an array of strings.题解: 依然是一道分治法解的题,类似的还有http://www.cnblogs.com/codershell/p/3592992.html 1 class Solution { 2 public: 3 string lcp(string str1,string str2){ 4 ... 阅读全文
[LeetCode]Merge k Sorted Lists
2014-03-10 23:42 by 庸男勿扰, 348 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/merge-k-sorted-lists/题目描述:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.题解: 这是个典型的分治题,用递归,不断平分,解决merge两个有序链表的小问题即可。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ... 阅读全文
[LeetCode]Reverse Words in a String
2014-03-09 19:02 by 庸男勿扰, 308 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/reverse-words-in-a-string/题目描述:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes a word?A sequence of non-space ch 阅读全文
[LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
2013-12-21 00:21 by 庸男勿扰, 331 阅读, 收藏, 编辑
摘要:
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/题目描述:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3&qu 阅读全文
[创新工场2014] 2014创新工场校园招聘笔试题
2013-09-26 16:42 by 庸男勿扰, 1851 阅读, 收藏, 编辑
摘要:
继上一篇博文《[创新工厂2014]回文修复》后,继续推出第二道创新工厂的笔试算法题~ 对于非负数列a1、a2、......、an,在数轴上做垂线连接点(i,0)和(i,ai)。选择这样的两条线和x轴可以形成一个容器,我们以面积代表所装的水,求以这种方式构成的容器能装的最大面积。比如选择a2=3、a5=6,则所装的面积为9。分析: 这道题实际上的意思是:对于一个给定的序列,求abs(i-j)*min(a[i],a[j])的最大值! 先对序列按照高度排序,然后按照排序后的顺序进行枚举,枚举过的就做标记,表示已经访问过了,保证了当前的节点,是序列里的最小高度的板,那么以这个板为边界能装的最... 阅读全文
[创新工场2014] 回文修复
2013-09-26 11:10 by 庸男勿扰, 1040 阅读, 收藏, 编辑
摘要:
新鲜出炉~,创新工厂2013-9-25晚在交大的现场笔试题~1、回文修复 所谓回文,就是正序和倒序遍历结果一样的字符串,比如“aba”,“abcdedcba”。实现一个方法pal(),输入一个字符串,打印出以这个字符串为前缀的一个回文。比如输入“abc”,pal()方法打印出“abcdcba”或“abcba”;输入“abcb”,可以输出“abcbcba”或“abcba”。如果可能,输出尽量短的结果。以Java语言为例: void pal(String in);语言任选。分析: 这道题说白了就是求以给定字符串为前缀的最短回文串。首先我们要明确的是,假设字符串的长度为len,那么最短长度... 阅读全文