自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(45)
  • 资源 (6)
  • 收藏
  • 关注

原创 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 145

原创 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 161

原创 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 222

原创 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 278

原创 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 198

原创 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 144

原创 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 187

原创 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 377

原创 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 157

原创 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 225

原创 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 138

原创 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 134

原创 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 126

原创 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 131

原创 103. Binary Tree Zigzag Level Order Traversal

难点:Z字形,奇数行从左向右输出,偶数行从右向左输出(但是注意这个偶数行的下一行又要以从左向右输出)。 解决思路:用栈和队列来解决顺序和逆序的问题。 1.用phase来表示奇数行还是偶数行; 2.对于奇数行用队列nodeQueue来记录所有点,对于偶数行,用nodeStack来记录所有点(从左到右push进去,则出来时从右到左),偶数行的输出用nodeStack弹出的值来完成;与nodeSta

2016-09-28 20:50:42 130

原创 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 203

原创 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 135

原创 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 121

原创 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 125

原创 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 130

原创 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 181

原创 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 148

原创 289. Game of Life

leetcode

2016-09-25 16:57:05 126

原创 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 129

原创 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 115

原创 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 144

原创 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 147

原创 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 215

原创 53. Maximum Subarray

最长子序列和问题:解法分析详见http://blog.csdn.net/sgbfblog/article/details/8032464public class Solution { public int maxSubArray(int[] nums) { int sum[] = new int[nums.length]; sum[0] = nums[0

2016-09-12 23:21:57 111

原创 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 117

原创 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 171

原创 16 - 3Sum Closest

参考题解:http://blog.csdn.net/lisonglisonglisong/article/details/45849965 思路: 先整体排一次序 然后遍历数组,固定一个元素,对其后的元素采用头尾指针。 若距离更小,更新距离与三个数之和。 否则,移动头指针或尾指针。public class Solution { public int threeSumClosest(

2016-08-08 21:31:37 113

原创 11. Container With Most Water

参考题解:http://blog.csdn.net/wzy_1988/article/details/17248209 解说: 假定初始的盛水面积为ans=0,设定两个指针pl和pr,分别指向左边的index和右边的index,如果height[pl] < height[pr], 则pl向右运动,寻找第一个比当前height[pl]大的左节点。同理,如果height[pl] > height[p

2016-08-08 20:54:45 122

原创 4. Median of Two Sorted Arrays

思路:分治法 计算中位数的左边需要剔除多少个数,然后剔除,并取出中位数。 例如: [1,4] [2,3,5,7] 可以知道中位数需要取出2个作平均,在这两个中位数左边需要排除掉2个数。先找出nums1和nums2的中位数,如上为4和5(本算法实现如此),把4放到nums2中看看,发现,比4小的有2和3,所以,总的比4小的有1,2,3。 超出了总共需要排除的总数(2), 所以,需要排除的2个

2016-08-07 17:55:54 169 4

原创 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 116

原创 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 269

原创 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 223

原创 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 178

原创 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 124

原创 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 153

微软等IT名企经典笔试100题-答案参考

微软等IT名企经典笔试100题-答案参考 题目: 微软等IT名企经典笔试100题(答案另外上传).docx

2014-02-05

微软等IT名企经典笔试100题

微软等IT名企经典笔试100题

2014-02-05

POJ题目分类

POJ 题目 分类

2014-02-05

C++命名规范

C++命名规范 例如doSomething,do_something等等

2014-02-05

Qt_Creator简明教程

Qt_Creator简明教程,强力推荐喔,简单易懂

2013-04-21

算法导论.pdf

算法导论.pdf

2013-04-20

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除