上一页 1 ··· 3 4 5 6 7 8 下一页

2014年1月25日

LeetCode: Word Search

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

posted @ 2014-01-25 03:42 longhorn 阅读(275) 评论(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 阅读(351) 评论(0) 推荐(0) 编辑

2014年1月21日

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

2014年1月20日

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

2014年1月19日

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

2014年1月18日

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

2014年1月17日

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

2014年1月14日

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

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

2014年1月13日

LeetCode: 3Sum & 4Sum

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

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

上一页 1 ··· 3 4 5 6 7 8 下一页

导航