随笔分类 - Leetcode
Leetcode: N-Queens
摘要:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integern, return all di...
阅读全文
Leetcode: Sudoku Solver
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be o...
阅读全文
Leetcode: Binary Tree Maximum Path Sum
摘要:难度:75. 参见了他人的思路,这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可以从根到某一个结点,而且路径可以从左子树某一个结点,然后到达右子树的结点,就像题目中所说的可以起始和终结于任何结点。函数的返回值定义为以自己为根的一条从根到叶子结点的最长路径,这个返回值是为了提供给它的父结
阅读全文
Leetcode: Gas Station
摘要:难度:60.这道题用Brute Force的方法解比较好想,就是从每一个站开始,一直走一圈,累加过程中的净余的油量,看它是不是有出现负的,如果有则失败,从下一个站开始重新再走一圈;如果没有负的出现,则这个站可以作为起始点,成功。可以看出每次需要扫描一圈,对每个站都要做一次扫描,所以时间复杂度是O(n
阅读全文
Leetcode: Convert Sorted List to Binary Search Tree
摘要:目前做法: 第一遍做法:难度70,与Convert Sorted Array to Binary Search Tree问题相似,这道题的做法就是,我们需要中点来做每一层树的根,问题在于,对于一个链表我们是不能常量时间访问它的中间元素的。于是我的想法是花O(N)的时间把链表里面的元素先存到一个数组或
阅读全文
Leetcode: Permutations II
摘要:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique per...
阅读全文
Leetcode: Reorder List && Summary: Reverse a LinkedList
摘要:这是一道比较综合的链表操作的题目,要按照题目要求给链表重新连接成要求的结果。其实理清思路也比较简单,分三步完成:(1)将链表切成两半,也就是找到中点,然后截成两条链表;(2)将后面一条链表进行reverse操作,就是反转过来;(3)将两条链表按顺序依次merge起来。 这几个操作都是我们曾经接触过的
阅读全文
Leetcode: Unique Binary Search Trees II
摘要:难度:98。没有做出来的一道题,很难,参考了其他人的想法,发现基本上只有一个版本,在纸上按照它的步骤推了几步终于看懂了。分析如下: 第一次遇到这种让你construct a BST的问题,而且结果并不是建立一个ArrayList<ArrayList<Integer>>型的,而是建立一个ArrayLi
阅读全文
Leetcode: Subsets II
摘要:第二遍Better做法: 第11行 i>start && nums[i]==nums[i-1] 就skip很不错 第一遍做法:需要已访问数组,当前后元素一样且前面元素并未访问,这个时候就是重复的case, continue, 注意这里的判重复条件跟3Sum那种是不一样的, 3SUM防止重复方法是 i
阅读全文
Leetcode: Unique Paths II
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space i...
阅读全文
Leetcode: Minimum Path Sum
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
阅读全文
Leetcode: Multiply Strings
摘要:大整数的字符串乘法,我本来对字符串表示的大整数就不是太熟悉,至于溢出该怎么处理也是不大清楚,所以参考了别人的做法,然后自己凭着印象重做了一次 直接乘会溢出,所以每次都要两个single digit相乘,最大81,不会溢出。 比如385 * 97, 就是个位=5 * 7,十位=8 * 7 + 5 *
阅读全文
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...
阅读全文
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...
阅读全文
浙公网安备 33010602011771号