12 2015 档案
摘要:12.7去西雅图微软on-site,等了两周终于等来了offer。看了地里很多面经,因为微软并没有签保密协议,今天来贡献一下。先说一下微软onsite体验真的很好,宾馆机票吃饭都安排的很好。一天给75刀饭补,本来以为都这样,后来谷歌onsite才知道谷歌只有30.我面的组是hololens,一共五轮...
阅读全文
摘要:经典动态规划public class Solution { public int coinChange(int[] coins, int amount) { int[] record = new int[amount + 1]; record[0] = 1; ...
阅读全文
摘要:MS面试最后一轮就跪在number of island 1 了,痛心啊。bfs虽然能过lc,但是面试的时候得写出union find来才可以啊。public class Solution { private int[] array; private int[][] grid; pr...
阅读全文
摘要:对每个房子bfs,找出每个房子到所有空地的距离。用一个record二维数组来记录每个空地上所有房子到该空地的距离和。同时用另一个二维数组记录每个空地能被几个房子访问到,来保证所有房子都可以到达的了该空地。public class Solution { public int shortestDi...
阅读全文
摘要:思路借鉴了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 ...
阅读全文
摘要:public class Solution { public List summaryRanges(int[] nums) { List result = new ArrayList(); int length = nums.length; int l...
阅读全文
摘要:public class Solution { public int[] plusOne(int[] digits) { int carry = 1; int length = digits.length; List list = new ArrayL...
阅读全文
摘要:刚从微软onsite回来,感觉要跪了。不过doesnt matter 继续刷题面谷歌很有意思的一道小题,很容易出错public class Solution { public int compareVersion(String version1, String version2) { ...
阅读全文
摘要:二分搜索,边界条件要注意public class Solution { public List countSmaller(int[] nums) { List sorted = new ArrayList(); int length = nums.length; ...
阅读全文
摘要:class TrieNode { // Initialize your data structure here. TrieNode[] child; boolean isWord; public TrieNode() { child = new TrieNode...
阅读全文
摘要:public class Solution { public List wordBreak(String s, Set wordDict) { return helper(s, wordDict, new HashMap>()); } public List ...
阅读全文
摘要:public class Solution { public String longestPalindrome(String s) { int length = s.length(); String result = ""; for (int i = ...
阅读全文
摘要:public class Solution { public String longestPalindrome(String s) { int length = s.length(); String result = ""; for (int i = ...
阅读全文
摘要:public class Solution { public String simplifyPath(String path) { String[] strs = path.split("/"); Stack stack = new Stack(); ...
阅读全文
摘要:public class Solution { public int calculate(String s) { Stack stack = new Stack(); s = s.replace(" ", ""); int length = s.len...
阅读全文
摘要:在网上看了一个超级精妙的解法public class Solution { public String multiply(String num1, String num2) { int length1 = num1.length(); int length2 = n...
阅读全文
摘要:public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { int length = preorder.length; if (length == 0) { ...
阅读全文
摘要:用了treemap来维护左右关系,其实也可以不用,记录一个min的index就好。public class Solution { public List> verticalOrder(TreeNode root) { List> result = new ArrayList>()...
阅读全文
摘要:这个题之前做过,实在没想出什么好方法,今天突然发现这个完全就是个有环链表找开始进入环的题目,真是相当精巧public class Solution { public int findDuplicate(int[] nums) { int slow = 0; int ...
阅读全文
摘要:class MyStack { // Push element x onto stack. Queue queue = new LinkedList(); public void push(int x) { Queue q = new LinkedList(); ...
阅读全文
摘要:public class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) { return root; } TreeNode tmp...
阅读全文
摘要: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 -...
阅读全文
摘要:public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return helper(inorder, postorder, 0, inorder.length - 1,...
阅读全文
摘要:public class Solution { public String largestNumber(int[] nums) { int length = nums.length; Queue queue = new PriorityQueue(length, n...
阅读全文
摘要:主要是一下步骤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 overflowpubl...
阅读全文
摘要:第一个是普通二叉树,第二个是bstpublic class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) { ...
阅读全文
摘要:public class Solution { public int[][] generateMatrix(int n) { int level = (n + 1) / 2; int[][] result = new int[n][n]; int tm...
阅读全文
摘要:上面是lc的单链表题目,下面我又自己加了个双向链表的情况public class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { ...
阅读全文
摘要:在无重复元素时,中间元素与首元素相等,表示一共只有两个元素,low与high各指向一个。由于while循环中限制的大小关系,因此返回nums[high]即为最小值。然而当存在重复元素时,该条件并不能表示一共只有low和high指向的两个元素,而是说明low指向的元素重复了,因此删除其一,low ++...
阅读全文
摘要:public class Solution { public int[] productExceptSelf(int[] nums) { int length = nums.length; int[] result = new int[length]; ...
阅读全文
摘要:public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int row = obstacleGrid.length; if (row == 0) { ...
阅读全文
摘要:public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; int count = (n + 1) / 2; for (int i = 0; i...
阅读全文
摘要:判断四个数是否可能通过加减乘得到二十四, 可以使用括号,思路极其像Expression Add Operators, 区别是在处理乘号的时候,一种是没有括号的,和Expression Add Operators一样,一种是带有括号的,其实就是把乘号当作加号来计算public class Soluti...
阅读全文
摘要:dp的方法比较简单就不写了。这里用分治法,对与一个数组,最大的子区间可以在left 到 mid这一段, 也可能划过mid, 也可能在mid 到 right, 所以分别求这三段,取最大的结果。求左右段最大的时候才用分治的想法。算法复杂度为o(nlogn)public class Solution { ...
阅读全文
摘要:两种解法,第一个是利用了前序遍历递增的特点public class Solution { long count = Long.MIN_VALUE; public boolean isValidBST(TreeNode root) { if (root == null) { ...
阅读全文
摘要:1 public class Solution { 2 public List> zigzagLevelOrder(TreeNode root) { 3 List> result = new ArrayList>(); 4 if (root == null)...
阅读全文
摘要:第三次写了,一遍过/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(i...
阅读全文
摘要:public class Solution { public int[] twoSum(int[] numbers, int target) { int left = 0; int right = numbers.length - 1; while (...
阅读全文
摘要:public class Solution { private int result = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { helper(root); return result...
阅读全文
摘要:public class Solution { public int minSubArrayLen(int s, int[] nums) { int p1 = 0; int p2 = 0; int length = nums.length; ...
阅读全文
摘要:第一个是递归方法,第二个是非递归方法public class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } ...
阅读全文
摘要:public class Solution { public int findPeakElement(int[] nums) { int left = 0; int right = nums.length - 1; while (left + 1 n...
阅读全文
摘要:public class ZigzagIterator { private List v1; private List v2; private int p1; private int p2; private boolean flg = true; publ...
阅读全文
摘要:public class Solution { List result = new ArrayList(); public List letterCombinations(String digits) { if (digits.length() == 0) { ...
阅读全文
摘要:public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode result = new ListNode(0); ...
阅读全文
摘要:思路和ugly2一样,不过是变成了一组factor,用一个priority求出每次最小的,同时维护一个conut数组,记录每个factor走的次数有一个500000的过不了,超限了,无耻的写了一行作弊的代码public class Solution { public int nthSuperU...
阅读全文
摘要:public class Solution { public int removeElement(int[] nums, int val) { if (nums.length == 0) { return 0; } int lef...
阅读全文
摘要:public class Solution { public String convert(String s, int numRows) { if (numRows == 1) { return s; } int flg = 0;...
阅读全文
摘要:public class LRUCache { HashMap map = new HashMap(); ListNode start; ListNode end; int capacity; int cur_size; public LRUCache(int c...
阅读全文
摘要:第一个是用了hashmap, 第二个是在每个结点后面加一个新的结点public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if (head == null) { ...
阅读全文
摘要:public class Solution { public List getSkyline(int[][] buildings) { List height = new ArrayList(); List result = new ArrayList(); ...
阅读全文
摘要:单纯的dfs,感觉应该有更好的办法public class Solution { public void solveSudoku(char[][] board) { helper(board); } public boolean helper(char[][] boa...
阅读全文
摘要:public class Solution { public void connect(TreeLinkNode root) { if (root == null) { return; } TreeLinkNode pre = r...
阅读全文
摘要:之前写了个很复杂的办法,后来参考网上思路,觉得这个是最简便的写法了public class Solution { public String numberToWords(int num) { String[] first = "Zero One Two Three Four Fi...
阅读全文
摘要:public class Solution { public void rotate(int[] nums, int k) { int length = nums.length; k = k % length; reverse(nums, 0, len...
阅读全文
摘要:public class Solution { public List spiralOrder(int[][] matrix) { List result = new ArrayList(); int row = matrix.length; if (...
阅读全文
摘要:public class Solution { private char[][] board; private List result = new ArrayList(); public List findWords(char[][] board, String[]...
阅读全文
摘要:动规public class Solution { public boolean isInterleave(String s1, String s2, String s3) { int length1 = s1.length(); int length2 = s2....
阅读全文
摘要:这个题目主要是需要处理两个数长度不一样的情况,有两个解决办法。我是采用递归,把长的那一段与carray再加。也可以把短的前面补0.public class Solution { public String addBinary(String a, String b) { int p...
阅读全文
摘要:public class Solution { public String countAndSay(int n) { if (n == 1) { return "1"; } String str = "1"; for...
阅读全文
摘要:public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length == 0) { return ""; } int...
阅读全文
摘要:public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { HashMap map = new HashMap(); for (int i = 0; i < ...
阅读全文
摘要:public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode dummy = new ListNode(0); dummy.next = head;...
阅读全文
摘要:public class Solution { boolean result = true; public boolean isBalanced(TreeNode root) { helper(root); return result; } pub...
阅读全文
摘要:public class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0; } Queue queue = new Link...
阅读全文
摘要:public class Solution { public boolean canAttendMeetings(Interval[] intervals) { Arrays.sort(intervals, new Comparator() { public...
阅读全文
摘要:public class Solution { public boolean isAnagram(String s, String t) { char[] first = s.toCharArray(); Arrays.sort(first); cha...
阅读全文
摘要:public class Solution { public int titleToNumber(String s) { int result = 0; int tmp = 1; for (int i = s.length() - 1; i >= 0;...
阅读全文
摘要:public class Solution { public int longestConsecutive(int[] nums) { HashSet set = new HashSet(); for (int num : nums) { se...
阅读全文
摘要:public class Solution { private int result = 0; public int longestConsecutive(TreeNode root) { if (root == null) { return 0; ...
阅读全文
摘要:public class Solution { public int lengthOfLIS(int[] nums) { int length = nums.length; List record = new ArrayList(); for (int...
阅读全文
摘要:public class Solution { public boolean wordPatternMatch(String pattern, String str) { return helper(pattern, str, new HashMap(), new HashMap...
阅读全文
摘要:用的dfs,但是感觉这个方法应该不是最优的,否则这个题目不应该是hardpublic class Solution { int up = Integer.MAX_VALUE; int low = Integer.MIN_VALUE; int left = Integer.MAX_V...
阅读全文
摘要:相当tricky的一道题目,我怀疑在面试的时候有人能在45分钟想出来,我参考了一下https://leetcode.com/discuss/72215/java-dp-solution-with-detailed-explanation-o-n-3这个人的思路。public class Soluti...
阅读全文
摘要:public class Solution { public int minCostII(int[][] costs) { int n = costs.length; if (n == 0) { return 0; } ...
阅读全文
摘要:public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head;...
阅读全文
摘要:public class Solution { public void connect(TreeLinkNode root) { if (root == null) { return; } Queue queue = new Li...
阅读全文
摘要://DFS,没有剪枝public class Solution { List> result = new ArrayList>(); public List> combinationSum3(int k, int n) { helper(k, n, 0, new Array...
阅读全文

浙公网安备 33010602011771号