01 2014 档案

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 阅读(218) 评论(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 阅读(504) 评论(0) 推荐(0)

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 阅读(149) 评论(0) 推荐(0)

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 阅读(248) 评论(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 阅读(267) 评论(0) 推荐(0)

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 阅读(178) 评论(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 阅读(147) 评论(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 阅读(194) 评论(0) 推荐(0)

LeetCode: Word Search
摘要:对于这道题。。我也无话可说了。。无奈了。。。最开始就是用dfs做,写出来之后怎么测试都是time limit exceeded。。。。上网看大家的解决方法都一样啊,都是上下左右检查一遍。。。。我这个郁闷啊但是后来我检查出我的代码有个严重的问题。。用dfs在回溯的时候,我们没有把这个点的visit标记回复为false。。因此这里一定要注意如果在dfs时候将原来的状态做了改变,一定要在回溯结束的时候将状态改变回来。。。。就像做subset时候一样,用tmp保留当前的状态,如果不满足条件回溯的时候,一样要将这一轮添加进去的元素删除掉!!!!但即使发现这个问题,依然是time limit exceed 阅读全文

posted @ 2014-01-25 03:42 longhorn 阅读(283) 评论(0) 推荐(0)

LeetCode: Surrounded Regions
摘要: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 .真是无奈了。。。。。首先想到的是dfs。扫描board上的每一个点,如果发现了‘O’,就以这个点进行dfs。但是需要判断最后是否遇到了边界,如果遇到了那么需要标记出这些是应该为‘O’的,如果没有遇到需要标记这些相连的 阅读全文

posted @ 2014-01-25 01:04 longhorn 阅读(357) 评论(0) 推荐(0)

LeetCode: Subsets & Subsets II
摘要:Given a set of distinct integers,S, return all possible subsets.我想到的算法是这个样子的。我想用的是递归。n个数的subset可以由n-1个数的subset产生。就是把subset(n-1)以及其中每一个组合加上S[n]。比如[1,2,3] = [], [1], [2], [1,2], []+3, [1]+3, [2]+3, [1, 2]+3; 1 public static ArrayList> subsets(int[] S, int aa) { 2 if(aa == 0) { 3 re... 阅读全文

posted @ 2014-01-21 03:45 longhorn 阅读(260) 评论(0) 推荐(0)

LeetCode: Convert Sorted Array & List to Binary Search Tree
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.递归。找到数组的中点作为root,左边递归构造左子树,右边递归构造右子树。条件判断有点写重复了。。。 1 public TreeNode sortedArrayToBST(int[] num) { 2 if (num.length == 0) return null; 3 else if (num.length == 1) return new TreeNode(num[... 阅读全文

posted @ 2014-01-20 21:11 longhorn 阅读(152) 评论(0) 推荐(0)

LeetCode: Single Number I & II
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.这个方法重来没见过,以后估计也不会再见了。。1 public static int singleNumber(int[] A) {2 int sum = 0;3 for (int a : A) {4 sum ^= a;5 }6 return sum;7 }本以为不会再见了,不过看到第二个,觉得位操作还是有些应用的。。。。... 阅读全文

posted @ 2014-01-19 11:31 longhorn

LeetCode: Best Time to Buy and Sell Stock I & II & III
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.最开始nc,只想到了brute force算法。想着最大减最小就是结果,但是如果最大值出现在最小值之前就不能算了。所以 阅读全文

posted @ 2014-01-18 23:58 longhorn 阅读(218) 评论(0) 推荐(0)

LeetCode: Merge Sorted Array
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.最开始的想法是,设两个指针,从0开始,分别标记数组A,B当前进行到的位置。通过不断比较和移动指针,实现数组的融合。但是发现,数组B中的值会覆盖掉A中还未比较的值,所以插入的时候就要进行数组的移动。显然非常不理想。后来学习到,可以从数组的后面开始比较。两个指针分别从数组的最后面开始,这样就不会有覆盖的情况发生。 1 public static void merge(int A[], int m, int B[], int n) { 2 ... 阅读全文

posted @ 2014-01-17 19:11 longhorn

LeetCode: NQueens
摘要:n皇后问题。用的是back tracking以及递归的方法。这个算法的时间复杂度为O(n^2).首先确定第一行的皇后放在什么位置。(需要遍历这个皇后所有可以放的位置)。就是说第一个皇后可以在第一行的任一位置。然后遍历第二行的皇后放在哪个位置,如果满足条件就接着放第三个,否则移到下一位置。。。以此类推当放置完最后一个时,返回结果。注意的问题是ArrayList是引用传递,所以可以把所有值都加在里面。 1 public ArrayList solveNQueens(int n) { 2 int[] result = new int[n]; 3 ArrayList a... 阅读全文

posted @ 2014-01-14 13:40 longhorn

LeetCode: Word Break & Word Break II
摘要:Word BreakGiven a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"lee 阅读全文

posted @ 2014-01-14 13:24 longhorn 阅读(189) 评论(0) 推荐(0)

LeetCode: 3Sum & 4Sum
摘要:就是在2Sum的基础上进行计算的。 阅读全文

posted @ 2014-01-13 10:48 longhorn 阅读(115) 评论(0) 推荐(0)

LeetCode: Generate Parentheses
摘要:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"最开始想的是,先产生n-1的情况,然后在n-1中的每个string的前面,后面加上()以及用()扩住整个string。 阅读全文

posted @ 2014-01-13 10:10 longhorn 阅读(193) 评论(0) 推荐(0)

导航