- 博客(129)
- 收藏
- 关注
转载 [LeetCode]Wildcard Matching
public class Solution { public boolean isMatch(String s, String p) { int length1 = s.length(); int length2 = p.length(); if (length1 == 0) { return ...
2016-01-10 15:52:00 113
转载 [LeetCode]Regular Expression Matching
public class Solution { public boolean isMatch(String s, String p) { if (p.length() == 0) { return s.length() == 0; } if (p.length() == 1 || p.charA...
2016-01-10 15:52:00 114
转载 [LeetCode]Maximum Size Subarray Sum Equals k
O(n)时间空间复杂度public class Solution { public int maxSubArrayLen(int[] nums, int k) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int sum =...
2016-01-05 15:31:00 182
转载 [LeetCode]Generalized Abbreviation
感觉我的方法逻辑不够清晰public class Solution { private List<String> result; public List<String> generateAbbreviations(String word) { result = new ArrayList<String>()...
2016-01-03 23:08:00 128
转载 微软onsite面经
12.7去西雅图微软on-site,等了两周终于等来了offer。看了地里很多面经,因为微软并没有签保密协议,今天来贡献一下。先说一下微软onsite体验真的很好,宾馆机票吃饭都安排的很好。一天给75刀饭补,本来以为都这样,后来谷歌onsite才知道谷歌只有30.我面的组是hololens,一共五轮,全都是白人面试官,没有遇到三哥很幸运。第一面是要实现一个数据压缩编码,是一个微软十几年前...
2015-12-29 15:05:00 192
转载 [LeetCode]Coin Change
经典动态规划public class Solution { public int coinChange(int[] coins, int amount) { int[] record = new int[amount + 1]; record[0] = 1; for (int i = 1; i <= amoun...
2015-12-29 14:21:00 65
转载 [LeetCode]Number of Islands II
MS面试最后一轮就跪在number of island 1 了,痛心啊。bfs虽然能过lc,但是面试的时候得写出union find来才可以啊。public class Solution { private int[] array; private int[][] grid; private int m, n; private int co...
2015-12-15 07:04:00 91
转载 [LeetCode]Shortest Distance from All Buildings
对每个房子bfs,找出每个房子到所有空地的距离。用一个record二维数组来记录每个空地上所有房子到该空地的距离和。同时用另一个二维数组记录每个空地能被几个房子访问到,来保证所有房子都可以到达的了该空地。public class Solution { public int shortestDistance(int[][] grid) { int row...
2015-12-14 14:02:00 109
转载 [LeetCode]Remove Duplicate Letters
思路借鉴了https://leetcode.com/discuss/73806/15-ms-java-solutionfor "cbacdcbc", we counts each letter's index:a----2b----1,6c----0,3,5,7d----4we go from a to d, to find the first letter ...
2015-12-14 06:38:00 62
转载 [LeetCode]Summary Ranges
public class Solution { public List<String> summaryRanges(int[] nums) { List<String> result = new ArrayList<String>(); int length = nums.length; ...
2015-12-14 05:37:00 79
转载 [LeetCode]Plus One
public class Solution { public int[] plusOne(int[] digits) { int carry = 1; int length = digits.length; List<Integer> list = new ArrayList<Integer>()...
2015-12-11 12:02:00 92
转载 [LeetCode]Compare Version Numbers
刚从微软onsite回来,感觉要跪了。不过doesnt matter 继续刷题面谷歌很有意思的一道小题,很容易出错public class Solution { public int compareVersion(String version1, String version2) { String[] v1 = version1.split("\\...
2015-12-11 11:59:00 102
转载 [LeetCode]Count of Smaller Numbers After Self
二分搜索,边界条件要注意public class Solution { public List<Integer> countSmaller(int[] nums) { List<Integer> sorted = new ArrayList<Integer>(); int length = nums...
2015-12-07 08:55:00 128
转载 [LeetCode]Implement Trie (Prefix Tree)
class TrieNode { // Initialize your data structure here. TrieNode[] child; boolean isWord; public TrieNode() { child = new TrieNode[26]; isWord = false; ...
2015-12-06 12:36:00 90
转载 [LeetCode]Word Break II
public class Solution { public List<String> wordBreak(String s, Set<String> wordDict) { return helper(s, wordDict, new HashMap<String, List<String>>(...
2015-12-06 12:19:00 66
转载 [LeetCode]Word Break
public class Solution { public String longestPalindrome(String s) { int length = s.length(); String result = ""; for (int i = 0; i < length; i++) { ...
2015-12-06 10:22:00 57
转载 [LeetCode]Longest Palindromic Substring
public class Solution { public String longestPalindrome(String s) { int length = s.length(); String result = ""; for (int i = 0; i < length; i++) { ...
2015-12-06 09:47:00 59
转载 [LeetCode]Simplify Path
public class Solution { public String simplifyPath(String path) { String[] strs = path.split("/"); Stack<String> stack = new Stack<String>(); for (in...
2015-12-06 08:05:00 76
转载 [LeetCode]Basic Calculator II
public class Solution { public int calculate(String s) { Stack<String> stack = new Stack<String>(); s = s.replace(" ", ""); int length = s.length();...
2015-12-06 07:35:00 78
转载 [LeetCode]Multiply Strings
在网上看了一个超级精妙的解法public class Solution { public String multiply(String num1, String num2) { int length1 = num1.length(); int length2 = num2.length(); int[] reco...
2015-12-06 06:31:00 58
转载 [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { int length = preorder.length; if (length == 0) { return null; }...
2015-12-06 05:19:00 60
转载 [LeetCode]Binary Tree Vertical Order Traversal
用了treemap来维护左右关系,其实也可以不用,记录一个min的index就好。public class Solution { public List<List<Integer>> verticalOrder(TreeNode root) { List<List<Integer>> result = n...
2015-12-06 03:04:00 100
转载 [LeetCode]Find the Duplicate Number
这个题之前做过,实在没想出什么好方法,今天突然发现这个完全就是个有环链表找开始进入环的题目,真是相当精巧public class Solution { public int findDuplicate(int[] nums) { int slow = 0; int fast = 0; do { ...
2015-12-05 14:31:00 71
转载 [LeetCode]Implement Stack using Queues
class MyStack { // Push element x onto stack. Queue<Integer> queue = new LinkedList<Integer>(); public void push(int x) { Queue<Integer> q = new Linked...
2015-12-05 13:34:00 150
转载 [LeetCode]Invert Binary Tree
public class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) { return root; } TreeNode tmp1 = root.left; TreeNode ...
2015-12-05 13:29:00 66
转载 [LeetCode]Rectangle Area
public class Solution { public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { int area = (C - A) * (D - B) + (G - E) * (H - F); if (C <= E |...
2015-12-05 13:25:00 80
转载 [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return helper(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1); } ...
2015-12-05 09:49:00 65
转载 [LeetCode]Largest Number
public class Solution { public String largestNumber(int[] nums) { int length = nums.length; Queue<String> queue = new PriorityQueue<String>(length, new Compar...
2015-12-05 09:19:00 66
转载 [LeetCode]String to Integer (atoi)
主要是一下步骤1.delete space in front of str2.check if str startsWith other characters3.check if str is positive4.check the end of str5.check if overflowpublic class Solution { public...
2015-12-05 08:39:00 54
转载 [LeetCode]Lowest Common Ancestor of a Binary Tree
第一个是普通二叉树,第二个是bstpublic class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) { return null; }...
2015-12-05 08:00:00 53
转载 [LeetCode]Spiral Matrix II
public class Solution { public int[][] generateMatrix(int n) { int level = (n + 1) / 2; int[][] result = new int[n][n]; int tmp = 1; for (int i = 0; i &...
2015-12-05 06:44:00 64
转载 [LeetCode]Swap Nodes in Pairs
上面是lc的单链表题目,下面我又自己加了个双向链表的情况public class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { return head; }...
2015-12-05 03:54:00 54
转载 [LeetCode]Find Minimum in Rotated Sorted Array II
在无重复元素时,中间元素与首元素相等,表示一共只有两个元素,low与high各指向一个。由于while循环中限制的大小关系,因此返回nums[high]即为最小值。然而当存在重复元素时,该条件并不能表示一共只有low和high指向的两个元素,而是说明low指向的元素重复了,因此删除其一,low ++即可。public class Solution { pub...
2015-12-05 03:16:00 63
转载 [LeetCode]Product of Array Except Self
public class Solution { public int[] productExceptSelf(int[] nums) { int length = nums.length; int[] result = new int[length]; int tmp = 1; for (int i =...
2015-12-05 02:28:00 61
转载 [LeetCode]Unique Paths II
public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int row = obstacleGrid.length; if (row == 0) { return 0; } ...
2015-12-04 16:42:00 56
转载 [LeetCode]Rotate Image
public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; int count = (n + 1) / 2; for (int i = 0; i < count; i++) { fo...
2015-12-04 14:30:00 52
转载 二十四点问题
判断四个数是否可能通过加减乘得到二十四, 可以使用括号,思路极其像Expression Add Operators, 区别是在处理乘号的时候,一种是没有括号的,和Expression Add Operators一样,一种是带有括号的,其实就是把乘号当作加号来计算public class Solution { public boolean solution(List<...
2015-12-04 12:45:00 148
转载 [LeetCode]Maximum Subarray
dp的方法比较简单就不写了。这里用分治法,对与一个数组,最大的子区间可以在left 到 mid这一段, 也可能划过mid, 也可能在mid 到 right, 所以分别求这三段,取最大的结果。求左右段最大的时候才用分治的想法。算法复杂度为o(nlogn)public class Solution { public int maxSubArray(int[] nums) {...
2015-12-04 11:53:00 49
转载 [LeetCode]Validate Binary Search Tree
两种解法,第一个是利用了前序遍历递增的特点public class Solution { long count = Long.MIN_VALUE; public boolean isValidBST(TreeNode root) { if (root == null) { return true; ...
2015-12-04 09:42:00 68
转载 [LeetCode]Binary Tree Zigzag Level Order Traversal
1 public class Solution { 2 public List<List<Integer>> zigzagLevelOrder(TreeNode root) { 3 List<List<Integer>> result = new ArrayList<List<Integer...
2015-12-04 09:25:00 49
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人