2014年2月3日

LeetCode: Permutation I & II

摘要: Given 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,1,2], and[3,2,1].思路有两个,一个是dfs,一个是递归。dfs的关键是,有一个visited数组来标记哪些元素访问过。这个非常重要,因为我第一次面试就卡在了这个上。。。 1 public class Solution { 2 public static ArrayList> . 阅读全文

posted @ 2014-02-03 06:38 longhorn 阅读(424) 评论(0) 推荐(0) 编辑

2014年2月2日

Context free grammar and Context sensitive grammar

摘要: Informal language theory, acontext-free grammar(CFG) is agrammarin which everyproductionrule is of the formV→wwhereVis a singlenonterminalsymbol, andwis a string ofterminalsand/or nonterminals (possibly empty). The term "context-free" expresses the fact that nonterminals can be rewritten w 阅读全文

posted @ 2014-02-02 03:41 longhorn 阅读(450) 评论(0) 推荐(0) 编辑

2014年1月31日

LeetCode: Symmetric Tree

摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).用递归的方法很好做。先比较对应的两个节点数值是否相同,如果相同就看第一个节点的左子和第二个节点的右子是否对称,以及第一个节点的右子和第二个节点的左子。 1 public static boolean isSymmetric(TreeNode root) { 2 if (root == null) return true; 3 else return check(root.lef... 阅读全文

posted @ 2014-01-31 01:18 longhorn 阅读(209) 评论(0) 推荐(0) 编辑

OODesign: Modelling an elevator using Object-Oriented Analysis and Design

摘要: The problem is as follows: design a basic set of objects/methods to be used to simulate an elevator bank. What are the objects and their attributes/methods?First there is an elevator class. It has a direction (up, down, stand, maintenance), a current floor and a list of floor requests sorted in the 阅读全文

posted @ 2014-01-31 00:39 longhorn 阅读(492) 评论(0) 推荐(0) 编辑

2014年1月29日

LeetCode: Reverse Linked List II

摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.这么一道小题做了一下午。。。真是太令我伤心了。我的想法是 先找到开始反转的第一个元素的前面的那个元素,标记start。然后一边遍历 一边反转,最后前中后三段连在一起。想法很简单,但是实现的很痛苦。。。有一个特殊情况,如果要求反转的第一个元素是head,需要注意。因为在这种情况下,head是要改变的,其他时候head不需要改变。所以可以在m==1的情况单独考虑,也可以给head之前加一个头结点,这样的话就不会有之前的问题。我觉得第二种方法比较好。n是最后 阅读全文

posted @ 2014-01-29 07:52 longhorn 阅读(137) 评论(0) 推荐(0) 编辑

2014年1月28日

LeetCode: Sort Color

摘要: 这道题断断续续想了好长时间。今天看说这个要用two points做。所以尝试做了一下。我的想法是先把0都移到数组的前面。然后再把1都移到2的前面。首先用一个指针first,我们要做到的是first之前的元素都是0. 还有一个扫描指针end,用来扫描数组并交换元素。在first到end之间不会有0出现,因为在end扫描的时候已经把0元素交换到first之前了。。。 1 public static void sortColors(int[] A) { 2 int first = 0, end = 0; 3 for (; end first the element b... 阅读全文

posted @ 2014-01-28 13:39 longhorn 阅读(241) 评论(0) 推荐(0) 编辑

LeetCode: Rotate Image

摘要: 最开始还用了一种递归的方法。。。也不知道是聪明还是傻。先将最外圈旋转,然后向里一圈,旋转。。。。最后到达矩阵中心。这里比较麻烦的是矩阵的下标,调了好久才写对。 1 public static void rotate(int[][] matrix) { 2 rotate(matrix, 0); 3 } 4 public static void rotate(int[][] matrix, int start) { 5 if(start == matrix.length/2) return; 6 int first=0, second... 阅读全文

posted @ 2014-01-28 04:45 longhorn 阅读(259) 评论(0) 推荐(0) 编辑

2014年1月26日

LeetCode: Largest Rectangle in Histogram

摘要: Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.本来的想法就是最naive的想法。对应每一个bar,在它的左右两侧延伸,直到遇到一个比这个值小的bar,计算面积。可以通过,就是时间比较长。后来在网上看到一种O(n)的算法。。我发现很多我做出来的题都不是最优的算法,都是一些naive的算法。Actually, we can decr 阅读全文

posted @ 2014-01-26 23:47 longhorn 阅读(169) 评论(0) 推荐(0) 编辑

LeetCode: Container With Most Water

摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.最开始只能想到bru 阅读全文

posted @ 2014-01-26 07:47 longhorn 阅读(141) 评论(0) 推荐(0) 编辑

LeetCode: strStr

摘要: Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.我以为不就是找个字符串吗,扫描一遍不就行了。。后来才发现,原来面试的时候不让用这种方法,都考出花了。所以学习了一下KMP算法。具体内容有链接。 1 public static String kmp(String haystack, String needle) { 2 if (needle.length() == 0) return haystack; 3 ... 阅读全文

posted @ 2014-01-26 01:57 longhorn 阅读(189) 评论(0) 推荐(0) 编辑

导航