随笔分类 -  leetcode

摘要:Problem: Implement a function to check if a singly linked list is a palindrome.思路:最简单的方法是 Reverse and compare.另外一种非常经典的办法是用 Recursive 的思路,把一个list看成这种形... 阅读全文
posted @ 2014-07-16 00:02 门对夕阳 阅读(441) 评论(0) 推荐(0)
摘要:Given a sorted array that has been rotated serveral times. Write code to find an element in this array. You may assume that the array was originally s... 阅读全文
posted @ 2014-07-09 09:31 门对夕阳 阅读(265) 评论(0) 推荐(0)
摘要:给一堆盒子,知道每个盒子的三围(长宽高),盒子正面朝你,不能旋转摆放,按照大的放在小的下面的原则堆起来,必须是 strictly larger,同样大小的盒子不行,问怎么样堆到最大的高度?思路:动态规划最优解一定是 max( {box_1 be the bottom}, {box_2 be the ... 阅读全文
posted @ 2014-06-14 11:14 门对夕阳 阅读(695) 评论(0) 推荐(0)
摘要:Write an algorithm to print all ways of arranging eight queens on an 8*8 chess board so that none of them share the same row, column or diagonal.思路:本质... 阅读全文
posted @ 2014-06-10 03:11 门对夕阳 阅读(244) 评论(0) 推荐(0)
摘要:Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), find how many ways to represent n cents.思路:... 阅读全文
posted @ 2014-06-09 05:11 门对夕阳 阅读(497) 评论(0) 推荐(0)
摘要:Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of parentheses.思路:比如 n = 3, ((())) 就是一个valid combinati... 阅读全文
posted @ 2014-06-06 13:30 门对夕阳 阅读(269) 评论(0) 推荐(0)
摘要:Problem: Compute all permutations of a string of unique characters.此题用循环的方式不好做,下面是一种递归的思路:把给的字符串看成一个字符集合,每次从这个集合里拿出一个字符来填到下面的空格中。填的顺序从左到右。把a1填到第一个空格里是... 阅读全文
posted @ 2014-06-06 09:34 门对夕阳 阅读(377) 评论(0) 推荐(0)
摘要:Problem: Given a two-dimensional graph with points on it, find a line which passes the most number of points.此题是Cracking the code 5th edition 第七章第六题,思... 阅读全文
posted @ 2014-05-28 12:22 门对夕阳 阅读(612) 评论(1) 推荐(0)
摘要:给一个Binary Tree,检查是不是Binary Search Tree. 即是否满足对每个节点,左子树的中的所有节点的值 = root.val){ // only 'Less than' is valid return false; } ... 阅读全文
posted @ 2014-05-04 07:26 门对夕阳 阅读(394) 评论(0) 推荐(0)
摘要:问题:给一个二叉树,写一个算法判断这个树是不是balanced。Solution #1.第一次遇到这个问题时我的解法,如下:public class Solution { public boolean isBalanced(TreeNode root) { if(root == ... 阅读全文
posted @ 2014-05-04 00:58 门对夕阳 阅读(6718) 评论(2) 推荐(1)
摘要:Problem:此题的做法和 “检查单链表回文” 那道题的骨架是一样的,用 Recursive 的方法,简洁而优雅。参考回文那道题:http://www.cnblogs.com/Antech/p/3847752.htmlCode in Java:public class Solution { ... 阅读全文
posted @ 2014-04-12 03:08 门对夕阳 阅读(367) 评论(0) 推荐(0)
摘要:Problem statement:Thought: Since TreeNode has a self-recursive structure, it's very natural and easy to come up with a recursive implement. But the tricky part is how to implement this using a iterative method. The three binary tree traversal methods: pre-order, in-order and post-order can all b 阅读全文
posted @ 2014-04-10 04:41 门对夕阳 阅读(957) 评论(0) 推荐(0)
摘要:Problem:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key 阅读全文
posted @ 2014-04-10 02:58 门对夕阳 阅读(366) 评论(0) 推荐(0)