06 2014 档案
Leetcode: Permutations
摘要: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]...
阅读全文
Leetcode: First Missing Positive
摘要:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm sho...
阅读全文
Summary: Binary Search
摘要:Iterative ways: 1 int binarySearch (int[] a, int x) { 2 int low = 0; 3 int high = a.length - 1; 4 int mid; 5 6 while (low x) {12 ...
阅读全文
Leetcode: Find First and Last Position of Element in Sorted Array
摘要:Analysis: 这道题是二分查找Search Insert Position的变体,思路并不复杂,就是先用二分查找找到其中一个target,然后再往左右找到target的边缘。找边缘的方法跟二分查找仍然是一样的,只是相等的情况依然要往左找(找左边缘)或往右找(找右边缘)。这样下来总共进行了三次二
阅读全文
Leetcode: Unique Binary Search Trees
摘要:Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's...
阅读全文
Leetcode: Group Anagrams
摘要:if want lexicographic order,然后要用Collections.sort() 来保证字典序 for (List<String> each : map.values()) { Collections.sort(each); res.add(new ArrayList<Strin
阅读全文
Leetcode: Reverse Linked List II
摘要:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5...
阅读全文
Leetcode: Longest Valid Parentheses
摘要:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the lon...
阅读全文
Leetcode: Partition List
摘要:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the o...
阅读全文
Leetcode: Rotate List
摘要:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->...
阅读全文
Leetcode: Divide Two Integers
摘要:Best method(跟discuss vote最高相似):只有一种情况整数除以整数会overflow,那就是Integer.MIN_VALUE除以-1,这种情况特殊分析。 之所以要用long a, b代替dividend和divisor是因为:比如Integer.MAX_VALUE除以1,第21
阅读全文
Leetcode: Linked List Cycle II
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?一次...
阅读全文
Leetcode: Single Number II
摘要:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime co...
阅读全文
Leetcode: Linked List Cycle
摘要:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Analysis: typical Runner Technique. 一次过 1 ...
阅读全文
Leetcode: Binary Tree Preorder Traversal
摘要:Analysis: 第一反应肯定是recursion(递归), 非常直接,但是题目要求不能用递归。如果要使用迭代的方法来写preorder traversal,最大的问题是要如何确定遍历节点的顺序,因为树的pre-order traversal其实很类似图的DFS,DFS可以用Stack来写,所以这
阅读全文
Leetcode: Single Number
摘要:Analysis: 需求里面要求O(N)时间以及无额外空间,这就排除了使用boolean array, hashmap这些个方法,只能在原数组上进行查找。O(N)基本上就相当于遍历数组. 最好的方法: 第二遍做法: 17行 & 运算符优先等级低于 == 所以一定要打括号 一样的思路另一个做法: 另外
阅读全文
Leetcode: Combination Sum
摘要:典型的recursion方法,找符合要求的path,存入result的ArrayList中。所以方法还是建立一个ArrayList<ArrayList<Integer>> result, 建立一个ArrayList<Integer> path,用recursion当找到符合条件的path时,存入re
阅读全文
Leetcode: Rotate Image
摘要:抠细节的题,题目思想如下:to implement the rotation in layers, if size is n, so there will be (int)(n / 2) layers that should be rotated. 需要注意的是:在下面这段code里面,需要注意的是
阅读全文
Leetcode: Longest Palindromic Substring && Summary: Palindrome
摘要:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Example 2: Analysis: 想到
阅读全文
Leetcode: Reverse Nodes in k-Group
摘要:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then le...
阅读全文
Leetcode: Substring with Concatenation of All Words
摘要:You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatena...
阅读全文
Leetcode: Merge k Sorted List
摘要:参看别人的思路,类似MergeSort的思路,思路是先分成两个子任务,然后递归求子任务,最后回溯回来。这个题目也是这样,先把k个list分成两半,然后继续划分,直到剩下两个list就合并起来,合并时会用到Merge Two Sorted Lists这道题。 MergeSort的方法:我们来分析一下上
阅读全文
Summary: Java中函数参数的传递
摘要:函数调用参数传递类型(java)的用法介绍.java方法中传值和传引用的问题是个基本问题,但是也有很多人一时弄不清。(一)基本数据类型:传值,方法不会改变实参的值。 1 public class TestFun { 2 3 public static void testInt(int ...
阅读全文
Leetcode: Generate Parentheses
摘要:又是把所有满足条件的结果存到一个ArrayList里面, 之前也有类似的如Letter combination of a Phone Number这道题。这种类型的题其实形成了一个套路,套路就是,recursion参数包括最终结果的集合(ArrayList),input(String),递归层次le
阅读全文
Leetcode: ZigZag Conversion
摘要:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font ...
阅读全文
Leetcode: Letter Combinations of a Phone Number
摘要:像这种DFS的题目,常见的写法无外乎两种,使用recursion, 或者用Stack。本文采用了Recursion的方式。做完后积累的经验有:像这种在一个ArrayList里面罗列可能的path的题目,recursion的参数一般包括:包含最终结果的集合(ArrayList),input(Strin
阅读全文
Summary: Depth-first Search(DFS)
摘要:There are generally two methods to write DFS algorithm, one is using recursion, another one is using stack. (reference from Wiki Pedia)Pseudocode for ...
阅读全文
Leetcode: Integer to Roman
摘要:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.一次过 1 public class Solution { 2 public S...
阅读全文
Leetcode: Container With Most Water
摘要:这道题我考虑过类似Trapping most water的做法,不大一样。那道题寻找左右最大元素,我可以确定那就是最优解,但是在这里,即使寻到了左右最大元素,我也不敢确定那是最优解。 然后我有考虑了DP全局最优&局部最优的做法,发现局部最优的递推式很难写。所以没有法了,网上看到说这道题用夹逼方法 T
阅读全文
Leetcode: Add Two Numbers
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single ...
阅读全文
Leetcode: Word Ladder II
摘要:Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be ch...
阅读全文