2014年3月18日

Josephus problem

摘要: http://www.geeksforgeeks.org/josephus-problem-set-1-a-on-solution/ 1 int josephus(int n, int k) 2 { 3 if (n == 1) 4 return 1; 5 else 6 /* The position returned by josephus(n - 1, k) is adjusted because the 7 recursive call josephus(n - 1, k) considers the original position 8 ... 阅读全文

posted @ 2014-03-18 03:27 longhorn 阅读(614) 评论(0) 推荐(0) 编辑

2014年3月17日

LeetCode: Validate Binary Search Tree

摘要: Given a binary tree, determine if it is a valid binary search tree (BST).我想到了用中序遍历,保存到一个数组中,再检查数组是否是有序的。但是我需要O(n)的额外空间保存这个数组。网上看到一种和这个一样的方法,但是不需要额外空间,但是没太看懂。 1 public static boolean isValidBST(TreeNode root) { 2 ArrayList list = new ArrayList(); 3 traverse(root, list); 4 5 ... 阅读全文

posted @ 2014-03-17 06:19 longhorn 阅读(175) 评论(0) 推荐(0) 编辑

LeetCode: First Missing Positive

摘要: Given an unsorted integer array, find the first missing positive integer.本想用求和的方法解决,但是因为有重复的数,所以这么解决不了。看了网上的解法,是排序。当时我以为排序肯定是O(nlogn)啊,所以没想到怎么解决。但是这里忽略了一个条件,我们需要排序的数据是从1开始的连续的数。所以可以这样排序。把每个元素放到对应的位置上。1就放到A[0], 2就放到A[1]....只要这样排好序,再找到第一个位置与数不对应的就是missing positive。这里要注意,对什么样的数据进行交换排序是有条件的。1. 它应该是一个正数 阅读全文

posted @ 2014-03-17 04:54 longhorn 阅读(178) 评论(0) 推荐(0) 编辑

LeetCode: Merge Intervals

摘要: comparator的写法。Collections.sort与Arrays.sort的区别。Arrays.sort只应用于arrays。LinkedList,ArrayList要用collections.sort。都可以使用comparator。 1 public class Solution { 2 public static ArrayList merge(ArrayList intervals) { 3 ArrayList result = new ArrayList(); 4 if (intervals.size() == 0) return r... 阅读全文

posted @ 2014-03-17 02:39 longhorn 阅读(139) 评论(0) 推荐(0) 编辑

LeetCode: Multiply Strings

摘要: 1 public class Solution { 2 public String multiply(String num1, String num2) { 3 if (num1.equals("0") || num2.equals("0")) return "0"; 4 num1 = new StringBuilder(num1).reverse().toString(); 5 num2 = new StringBuilder(num2).reverse().toString(); 6 int[] result = n... 阅读全文

posted @ 2014-03-17 02:19 longhorn 阅读(100) 评论(0) 推荐(0) 编辑

2014年3月16日

LeetCode: Jump Game II

摘要: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.最开始想像jump game I一样用动态规划,发现会超时。所以想到了用贪心算法。可以计算每一步可以跳 阅读全文

posted @ 2014-03-16 09:07 longhorn 阅读(163) 评论(0) 推荐(0) 编辑

2014年3月15日

LeetCode: Longest Palindromic Substring

摘要: Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one unique longest palindromic substring.来来回回用了好长时间。主要是O(n^2)的两种算法。最后还有一种O(n)的算法,但估计面试的时候是想不出来的,所以就只给个链接。第一种是用动态规划。O(n^2)时间,O(n^2)空间。table[i][j]标记string从i到j是不是palindrome。t 阅读全文

posted @ 2014-03-15 11:52 longhorn 阅读(166) 评论(0) 推荐(0) 编辑

2014年3月14日

LeetCode: Longest Substring Without Repeating Characters

摘要: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.我 阅读全文

posted @ 2014-03-14 09:14 longhorn 阅读(119) 评论(0) 推荐(0) 编辑

2014年3月11日

LeetCode: Trapping Rain Water

摘要: Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.这个题的思路应该是,根据当前bar的左右两侧的最高的bar的高度,可以计算出当前bar可以储存的水。所以用三个循环。第一个从左到右扫描,保存下每个bar左侧最高bar的高度。第二个从右到左,保存下每个bar右侧最高bar的高度。最后通过前两个计算出每个bar可以存水多少,加和。 1 public int 阅读全文

posted @ 2014-03-11 11:34 longhorn 阅读(173) 评论(0) 推荐(0) 编辑

LeetCode: Distinct Subsequences

摘要: Given a stringSand a stringT, count the number of distinct subsequences ofTinS.最开始,一感觉,这就是一个DFS。所以也就按dfs写了。但是超时。想到了dp,但是觉得无法解决。因为当时想到的是1维动态规划,感觉这个问题,无法用子问题得到答案。看到网上的解法,最后还是用的dp,不过是二维dp。map[i][j]表示的是S中的前i个字符,有多少种方式可以表示T中的前j个字符。if S[i] == T[j], map[i][j] = map[i-1][j] + map[i-1][j-1];if S[i] != T[j], 阅读全文

posted @ 2014-03-11 03:27 longhorn 阅读(180) 评论(0) 推荐(0) 编辑

导航