leetcode
eric--lin
这个作者很懒,什么都没留下…
展开
-
338. Counting Bits-medium
Counting Bits-medium 难在要在O(n)时间范围内求解。public int[] countBits(int num) { int[] f = new int[num + 1]; for (int i=1; i<=num; i++) f[i] = f[i >> 1] + (i & 1); return f; }原创 2017-08-14 21:53:09 · 152 阅读 · 0 评论 -
114. Flatten Binary Tree to Linked List
解法一:递归解法,非in placepublic class Solution { //return the head and tail of flatten tree public TreeNode flattenTree(TreeNode root){ TreeNode left = null, right = null; TreeNode tai原创 2016-09-29 11:34:37 · 133 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
难点:Z字形,奇数行从左向右输出,偶数行从右向左输出(但是注意这个偶数行的下一行又要以从左向右输出)。 解决思路:用栈和队列来解决顺序和逆序的问题。 1.用phase来表示奇数行还是偶数行; 2.对于奇数行用队列nodeQueue来记录所有点,对于偶数行,用nodeStack来记录所有点(从左到右push进去,则出来时从右到左),偶数行的输出用nodeStack弹出的值来完成;与nodeSta原创 2016-09-28 20:50:42 · 133 阅读 · 0 评论 -
98. Validate Binary Search Tree
验证二叉搜索树的正确性:法一:二叉搜索树的中序遍历是递增数列public class Solution { public List<Integer> inorderTraversal(TreeNode root){ List<Integer> result = new ArrayList<Integer>(); Stack<TreeNode> nodeStac原创 2016-09-27 23:57:15 · 207 阅读 · 0 评论 -
96. Unique Binary Search Trees
解法一:DPpublic class Solution { public int numTrees(int n) { int[] f = new int[n+1]; f[0] = 1; f[1] = 1; for (int len = 2; len <= n; len ++) for (int subLen = 0; s原创 2016-09-27 22:30:35 · 138 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
很简单。自己的解法:public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null){ return null; } ListN原创 2016-09-27 17:24:39 · 125 阅读 · 0 评论 -
148. Sort List
解法一:public class Solution { public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; ListNode fast = head, slow = head; while原创 2016-09-27 17:05:02 · 127 阅读 · 0 评论 -
142. Linked List Cycle II-巧
难点在于不使用额外空间解法思路如下:(参考https://discuss.leetcode.com/topic/58134/o-n-time-and-o-1-space-java-solution-with-chinese-explanation) 步骤一:通过Linked List Cycle的方式,则快慢指针(快指针一次两步,慢指针一次一步)相遇时,则表示存在环,且相遇点在环上。 步骤二:如原创 2016-09-27 10:08:57 · 131 阅读 · 0 评论 -
141. Linked List Cycle
注意:这个cycle不一定是包含链表所有node的cycle难点在于in place 思路:遍历所有元素,把遍历过的元素的next都改为head,如此的话,只要找到一个元素,他的next是head,就说明有cyclepublic class Solution { public boolean hasCycle(ListNode head) { if (head == nul原创 2016-09-27 09:15:37 · 185 阅读 · 0 评论 -
61. Rotate List-链表做法归纳-巧妙
注意先获取链表长度len, k = k % len解法一:最简单,一次移一位public class Solution { public ListNode rotateRightOnce(ListNode head){ ListNode runner = head; while(runner.next.next != null){ r原创 2016-09-26 14:02:59 · 149 阅读 · 0 评论 -
117. Populating Next Right Pointers in Each Node II
解题思路: 和Populating Next Right Pointers in Each Node(https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)一样采用dfs,但是注意在dfs的时候需要多做一些next节点是否存在的判断。并且需要注意的是,DFS时要先遍历right son, 再遍历left原创 2016-09-30 15:35:33 · 130 阅读 · 0 评论 -
127. Word Ladder-未完待续
最初的思路(TLE,最大数据集wordList约2800个词): 维护两个集合,分别代表beginWord的n级关系和endWord的n级关系; 若beginSet和endSet在第n级关系的交集为空,则beginSet和endSet分别去wordList中扩展下一级关系。 若beginSet和endSet在第n级关系的交集不为空,则返回关系级数。 代码如下:public class Sol原创 2016-10-26 22:16:45 · 189 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
寻找最低共同父节点(LCA)代码如下:public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root;原创 2016-10-04 14:41:16 · 136 阅读 · 0 评论 -
221. Maximal Square
https://leetcode.com/problems/maximal-square/description/最开始想的是:用f[i][j][side_len_i][side_len_j]来表示以[i, j]为左上顶点,高为side_len_i, 宽为side_len_j的矩形。 最后解法如下(TLE): ··· public class Solution { public in原创 2017-08-02 10:47:11 · 165 阅读 · 0 评论 -
179. Largest Number-medium
最开始的思路:把所有数,按照位数最长的数的位数来补齐(补上的数即是该数的首位) 例如: 最长位数为5,则9补齐后是“99999”, 32补齐后是“32333”按照补齐后的数的大小来排序,做组合以上解法是找规律得出的解决的特殊情况: “121”和”12”,补足后,都是121,此例中应选12+121 0, 0 8308, 830,此例中应选8308+830问题是:上述思路,无法解决”121”和原创 2017-05-21 17:00:55 · 227 阅读 · 0 评论 -
127. Word Ladder
一开始想用Dijkstra求出beginWord到wordList里面所有词的最短路径,然后再看endWord是否和wordList里面某个词相同或者相差1个字母。 结果TLE:public class Solution { public int diffCharNum(String word1, String word2){ if (word1.length() != w原创 2016-12-18 00:21:55 · 283 阅读 · 0 评论 -
47. Permutations II
一开始使用DFS,加使用集合来去重,结果TLE。改进,参考别人的代码: 1. 先对nums数组做一个排序; 2. 在dfs的时候,判断一下if (k > 0 && nums[k] == nums[k-1] && flags[k-1]==false) continue;原因如下:假设有如下序列[x, x, x, a1, a2, x, x, x], 这里是X代表任意数,a1=a2 这段代原创 2016-12-17 22:52:03 · 202 阅读 · 0 评论 -
23. Merge k Sorted Lists
难度主要在时间复杂度上,按正常的归并排序,k=10000时,TLE。普通归并排序(k=10000, TLE): public ListNode mergeKLists(ListNode[] lists) { ListNode dummyHead = new ListNode(-1); ListNode cur = dummyHead; Array原创 2016-10-08 17:31:50 · 379 阅读 · 0 评论 -
214. Shortest Palindrome
一个简单明了的解法: 找一个最小的end,使得s.substring(0, end+1)是一个回文串,然后对余下部分补齐。复杂度O(N^2) public String shortestPalindrome(String s) { int i = 0, end = s.length() - 1, j = end; char chs[] = s.toCharArray();原创 2016-10-07 11:00:15 · 159 阅读 · 0 评论 -
258. Add Digits
这个题有个O(1)的解法。数学trick:https://en.wikipedia.org/wiki/Digital_root原理解析: 以138为例 138 = 1+3+8=12=1+2=3 138 = 1 * (99+1) + 3 * (9+1) + 8 = 1 + 3 + 8 (这步相当于138 mod 9) 1 + 3 + 8 = 12 = 1 * (9+1)+2 = 3 (这步也原创 2016-10-28 23:23:58 · 148 阅读 · 0 评论 -
43. Multiply Strings
大数乘法的主要解法如下:解法: 0、最简单的模拟手算的方法(O(N^2)) 1、分治乘法(最简单的是Karatsuba乘法(http://blog.csdn.net/sunnyyoona/article/details/43234889),一般化以后有Toom-Cook乘法); 2、快速傅里叶变换FFT(为了避免精度问题,可以改用快速数论变换FNTT),时间复杂度O(NlgNlglgN)。具体原创 2016-10-06 15:39:35 · 228 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
s[j,i]表示以字母i结尾的最长的无字母重复的substring;public class Solution { public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> charPosition = new HashMap<Character, Integer>();原创 2016-10-04 21:40:58 · 141 阅读 · 0 评论 -
289. Game of Life
leetcode原创 2016-09-25 16:57:05 · 131 阅读 · 0 评论 -
152. Maximum Product Subarray
最大连续子序列乘积 参考这个题解: 求最大连续子序列乘积与最大连续子序列和问题有所不同,因为其中有正有负还有可能有0。 假设数组为a[],直接利用动归来求解,考虑到可能存在负数的情况,我们用Max[i]来表示以a[i]结尾的最大连续子序列的乘积值,用Min[i]表示以a[i]结尾的最小的连续子序列的乘积值,那么状态转移方程为: Max[i]=max{a[i], Max[i-1]原创 2016-09-25 13:09:21 · 131 阅读 · 0 评论 -
313. Super Ugly Number
Write a program to find the nth super ugly number.Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16,原创 2016-08-02 23:29:58 · 228 阅读 · 0 评论 -
220. Contains Duplicate III—medium
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] andnums[j] is at most t and the difference between i and j is a原创 2016-07-26 23:20:03 · 182 阅读 · 0 评论 -
169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always原创 2016-03-15 17:12:52 · 158 阅读 · 0 评论 -
10. Regular Expression Matching-hard
本来采用递归的方法来做,结果超时——网上找的其他人的题解没有不超时的C++代码,都是C或者Java:class Solution {public: //to verify whether s and p=='x*' is matched bool match(string s, string p){ if(s.size() == 0 || p[0] == '.')原创 2016-02-28 22:42:49 · 213 阅读 · 0 评论 -
310. Minimum Height Trees-Difficulty: Medium
https://leetcode.com/problems/minimum-height-trees/For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible roote原创 2016-02-28 20:33:36 · 196 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1,2,4,原创 2016-03-23 14:26:05 · 126 阅读 · 0 评论 -
123. Best Time to Buy and Sell Stock III
以下解体方案转自:http://blog.csdn.net/linhuanmars/article/details/23236995原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这道题是Best Time to Buy and Sell Stock的扩展,现在我们最多可以进行两次交易。我们仍然转载 2016-03-09 10:16:45 · 160 阅读 · 0 评论 -
213. House Robber II
原想按照 House Robber那样: f.resize(nums.size(), vector(2)); //f[i][0] indicates the max value got when the i-th house not broken. //f[i][1] indicates the max value got when the i-th house broken. 再加一个g数原创 2016-03-20 22:46:06 · 155 阅读 · 0 评论 -
335. Self Crossing
解题思路:模拟。 重点在于模拟第4条线的终点相对于第1条线的位置,有3种情况: 1.第4条线的终点超出了第1条线 2.第4条线的终点刚好在第1条线那里 3.第4条线的终点在第1条线以内(如下图)-----------| || |------public class Solution { public boolean isSelfCrossing(原创 2016-08-06 16:08:01 · 273 阅读 · 0 评论 -
59. Spiral Matrix II
思路:找规律 第一次向右走n步,第二次向下走n-1, 第3次向左走n-1,第4次向上走n-2,第5次向右走n-2……public class Solution { public int[][] generateMatrix(int n) { int num[][] = new int[n][n]; if (n == 0) return原创 2016-08-06 16:50:31 · 118 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
解题思路: 1. 先找到最小值所在位置; 2. 以循环交界处(最小值前一位)为分界,分两段做二分查找public class Solution { public int binarySearch(int[] nums, int beg, int end, int target){ if (beg == end){ if (nums[beg] ==原创 2016-08-18 13:33:45 · 119 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
参考:http://blog.csdn.net/nomasp/article/details/50829772 想复杂了举例子: // 4 7 8 2 8 最大利润很明显是 (8 - 4) + (8 - 2) = 10 就因为这个式子让我想复杂了:首先要找到一个极小值4,然后找到极大值8;然后找到极小值2,然后找到极大值8;balabala……其实换一种思路,(7 - 4) + (8 - 7原创 2016-09-24 23:19:41 · 118 阅读 · 0 评论 -
120. Triangle
优解:使用O(n) space规律:从top往bottom递归的话,会发现不能只用O(n) space,因为同一层中 前面的值更新会影响到后面 从bottom往top递归的话可以避免这个问题。public class Solution { public int minimumTotal(List<List<Integer>> triangle) { int sum[] =原创 2016-09-24 21:33:45 · 148 阅读 · 0 评论 -
81. Search in Rotated Sorted Array II
参考 33. Search in Rotated Sorted Array的题解: http://blog.csdn.net/linwutao0810/article/details/52240194 原想把33的题解改改就行: 1. 33源代码不改的话,对[2,2,2,0,2,2]这种数据会找错最小值; 2. 把33的找最小值算法改一改,改成O(n)遍历的,对[1,3,1,1,1]这种多最原创 2016-09-24 20:00:34 · 149 阅读 · 0 评论 -
55. Jump Game
参照:http://www.cnblogs.com/zichi/p/4808025.html 题意:给你一个数组,数组的每个元素表示你能前进的最大步数,最开始时你在第一个元素所在的位置,之后你可以前进,问能不能到达最后一个元素位置(index n-1)。BFS解法,超时//bfs public boolean canJump(int[] nums) { if (nums原创 2016-09-23 00:37:08 · 219 阅读 · 0 评论 -
31. Next Permutation
初始解法: 直接调用一次next(nums, 0, nums.length); 例如1, 4, 7, 8, 6 从右往左找第一个比6小的数,与6对调(1,6,7,8,4),然后对6以后的数做sort( 1, 6, 4, 7, 8)这样可能会出现: input: [4,2,0,2,3,2,0] output: [4,2,2,0,0,2,3] expected: [4,2,0,3,0,2,2原创 2016-08-11 22:44:50 · 176 阅读 · 0 评论