• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅
上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 21 下一页
2015年12月22日
Leetcode: Binary Tree Paths
摘要: Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->... 阅读全文
posted @ 2015-12-22 12:44 neverlandly 阅读(275) 评论(0) 推荐(0)
Leetcode: Verify Preorder Sequence in Binary Search Tree
摘要: 4 / \ 2 6 / \ / \ 1 3 5 7 如图所示BST, preorder结果是 4 2 1 3 6 5 7 4是root, 213是左子树,657是右子树 idea就是右子树不能再出现比root小的数,一旦出现,return false. 这样一层一层recursion, 直到子树只剩 阅读全文
posted @ 2015-12-22 11:16 neverlandly 阅读(293) 评论(0) 推荐(0)
Leetcode: Meeting Rooms II
摘要: Like the previous one Meeting Room, still need to sort the intervals using a comparator. We need to simulate the process to know the maximum number of 阅读全文
posted @ 2015-12-22 09:19 neverlandly 阅读(949) 评论(0) 推荐(0)
Leetcode: Meeting Rooms
摘要: Interval as an array: Previous interval as an object: Implement a Comparator<Interval> Syntax: don't forget the public sign when defining a function 阅读全文
posted @ 2015-12-22 08:32 neverlandly 阅读(302) 评论(0) 推荐(0)
Leetcode: Flatten 2D Vector
摘要: Implement an iterator to flatten a 2d vector.For example,Given 2d vector =[ [1,2], [3], [4,5,6]]By calling next repeatedly until hasNext returns fa... 阅读全文
posted @ 2015-12-22 08:13 neverlandly 阅读(281) 评论(0) 推荐(0)
Leetcode: Count Univalue Subtrees
摘要: Given a binary tree, count the number of uni-value subtrees.A Uni-value subtree means all nodes of the subtree have the same value.For example:Given b... 阅读全文
posted @ 2015-12-22 07:22 neverlandly 阅读(1746) 评论(0) 推荐(0)
Leetcode: Group Shifted Strings
摘要: Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequen... 阅读全文
posted @ 2015-12-22 02:04 neverlandly 阅读(443) 评论(0) 推荐(0)
2015年12月21日
Leetcode: Paint House
摘要: The same with F面经 Better Solution: O(N) time, O(1) space 阅读全文
posted @ 2015-12-21 13:34 neverlandly 阅读(336) 评论(0) 推荐(0)
Leetcode: Strobogrammatic Number III
摘要: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Write a function to count the total strobogr... 阅读全文
posted @ 2015-12-21 12:53 neverlandly 阅读(340) 评论(0) 推荐(0)
Leetcode: Strobogrammatic Number II
摘要: Watch out for cornor case: when n = 2, there shouldn't be "00" returned. but when n = 4, which uses n =2 as recursion base, "1001" is an answer 阅读全文
posted @ 2015-12-21 09:30 neverlandly 阅读(310) 评论(0) 推荐(0)
Leetcode: Strobogrammatic Number
摘要: Related to confusing number O(N) time and O(N) space O(N) time and O(1) space, use two pointers 阅读全文
posted @ 2015-12-21 07:11 neverlandly 阅读(292) 评论(0) 推荐(0)
Leetcode: Shortest Word Distance III
摘要: This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.Given a list of words and two words word1 a... 阅读全文
posted @ 2015-12-21 06:40 neverlandly 阅读(281) 评论(0) 推荐(0)
Leetcode: Shortest Word Distance II
摘要: This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly ma... 阅读全文
posted @ 2015-12-21 04:34 neverlandly 阅读(322) 评论(0) 推荐(0)
Leetcode: Shortest Word Distance
摘要: 双指针法:time O(N), space O(1) 一个指针指向word1上次出现的位置,一个指针指向word2上次出现的位置。因为两个单词如果比较接近的话,肯定是相邻的word1和word2的位置之差,所以我们只要每次得到一个新位置和另一个单词的位置比较一下就行了。 阅读全文
posted @ 2015-12-21 00:57 neverlandly 阅读(305) 评论(0) 推荐(0)
2015年12月20日
Leetcode: Valid Anagram
摘要: 还不知道Unicode 该怎么做 只有lowercase alphabets的做法无外乎sort, hashmap, array, bitmap之类的 Better: Unicode Follow up In Java, a Unicode could be represented by a sin 阅读全文
posted @ 2015-12-20 13:24 neverlandly 阅读(331) 评论(0) 推荐(0)
Leetcode: Search a 2D Matrix II
摘要: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted i 阅读全文
posted @ 2015-12-20 12:42 neverlandly 阅读(247) 评论(0) 推荐(0)
Leetcode: Sliding Window Maximum
摘要: 方法一:Heap时间 O(NlogK) 空间 O(K) maintain a maximum heap, notice PriorityQueue.remove(object o) can remove certain object from our heap. When writting Maxi 阅读全文
posted @ 2015-12-20 12:25 neverlandly 阅读(422) 评论(0) 推荐(0)
Leetcode: Product of Array Except Self
摘要: Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except ... 阅读全文
posted @ 2015-12-20 11:38 neverlandly 阅读(280) 评论(0) 推荐(0)
Leetcode: Delete Node in a Linked List
摘要: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 - 阅读全文
posted @ 2015-12-20 08:16 neverlandly 阅读(227) 评论(0) 推荐(0)
Leetcode: Lowest Common Ancestor of a Binary Tree
摘要: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest... 阅读全文
posted @ 2015-12-20 08:08 neverlandly 阅读(268) 评论(0) 推荐(0)
Leetcode: Lowest Common Ancestor of a Binary Search Tree
摘要: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia 阅读全文
posted @ 2015-12-20 08:06 neverlandly 阅读(285) 评论(0) 推荐(0)
Leetcode: Palindrome Linked List
摘要: Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?先分成大小相同(可能长度差1) 两部分, reverse一个list. ... 阅读全文
posted @ 2015-12-20 07:12 neverlandly 阅读(286) 评论(0) 推荐(0)
Leetcode: Number of Digit One
摘要: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6... 阅读全文
posted @ 2015-12-20 05:21 neverlandly 阅读(259) 评论(0) 推荐(0)
Leetcode: Power of Two
摘要: Best Solution 还是有一个地方不小心:Integer.MIN_VALUE, 应该是false,但是因为只有一个1,我曾经判断为true。事实上,所有negative value都应该是false 一旦符号位为1,就return false, 检查其他位只有1个1 做的时候遇到很多语法错误 阅读全文
posted @ 2015-12-20 04:28 neverlandly 阅读(217) 评论(0) 推荐(0)
Leetcode: Implement Queue using Stacks
摘要: Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front o... 阅读全文
posted @ 2015-12-20 02:20 neverlandly 阅读(275) 评论(0) 推荐(0)
Leetcode: Majority Element II
摘要: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Hin... 阅读全文
posted @ 2015-12-20 02:02 neverlandly 阅读(316) 评论(0) 推荐(0)
2015年12月19日
Leetcode: Summary Ranges
摘要: 这种题就是要写的clean and tidy,好方法: 维护两个指针的方法: 阅读全文
posted @ 2015-12-19 13:33 neverlandly 阅读(267) 评论(0) 推荐(0)
Leetcode: Kth Smallest Element in a BST
摘要: Java Solution 1 - Inorder Traversal We can inorder traverse the tree and get the kth smallest element. Time is O(n). Recursion method: Java Solution 2 阅读全文
posted @ 2015-12-19 13:06 neverlandly 阅读(412) 评论(0) 推荐(0)
Leetcode: Basic Calculator II
摘要: 用Stack来做:时间 O(N) 空间 O(N) 因为乘法和除法不仅要知道下一个数,也要知道上一个数。所以我们用一个栈把上次的数存起来,遇到加减法就直接将数字压入栈中,遇到乘除法就把栈顶拿出来乘或除一下新数,再压回去。最后我们把栈里所有数加起来就行了。 上面这段code可以先用String.repl 阅读全文
posted @ 2015-12-19 12:31 neverlandly 阅读(311) 评论(0) 推荐(0)
Leetcode: Basic Calculator
摘要: https://discuss.leetcode.com/topic/15816/iterative-java-solution-with-stack 第二遍做法:遇到digit直接走到末尾,并且直接利用sign进行计算,这样比较方便 第一遍做法: 阅读全文
posted @ 2015-12-19 06:12 neverlandly 阅读(371) 评论(0) 推荐(0)
Leetcode: Count Complete Tree Nodes
摘要: 递归树高法 复杂度 时间 O(N) 空间 O(1) 思路 完全二叉树的一个性质是,如果左子树最左边的深度,等于右子树最右边的深度,说明这个二叉树是满的,即最后一层也是满的,则以该节点为根的树其节点一共有2^h-1个。如果不等于,则是左子树的节点数,加上右子树的节点数,加上自身这一个。 注意 这里在左 阅读全文
posted @ 2015-12-19 02:30 neverlandly 阅读(332) 评论(0) 推荐(0)
Leetcode: Implement Stack using Queues
摘要: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. to 阅读全文
posted @ 2015-12-19 01:31 neverlandly 阅读(268) 评论(0) 推荐(0)
Leetcode: Maximal Square
摘要: dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长 if (matrix[i][j] == '0') dp[i][j] = 0; else dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1; 阅读全文
posted @ 2015-12-19 00:47 neverlandly 阅读(346) 评论(0) 推荐(0)
2015年12月18日
Leetcode: Contains Duplicate III
摘要: 暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型! 时间 O(NlogK) 空间 O(K) 思路 要求判断之前是否存在差值小于t的数字,我们需要知道在当前数字x两边 阅读全文
posted @ 2015-12-18 23:31 neverlandly 阅读(269) 评论(0) 推荐(0)
Leetcode: Invert Binary Tree
摘要: nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this... 阅读全文
posted @ 2015-12-18 13:14 neverlandly 阅读(287) 评论(0) 推荐(0)
Leetcode: The Skyline Problem
摘要: 参考了http://www.cnblogs.com/tonyluis/p/4564617.html 复杂度 Time Complexity is O(n^2) because removal in a PQ is O(n), 空间 O(N) I think if replace PQ with Tr 阅读全文
posted @ 2015-12-18 12:27 neverlandly 阅读(268) 评论(0) 推荐(0)
Leetcode: Word Search II
摘要: 如果跟Word Search做法一样,还按照DFS回溯的方法,逐个检查每个word是否在board里,显然效率是比较低的。我们可以利用Trie数据结构,也就是前缀树。然后dfs时,如果当前形成的单词不在Trie里,就没必要继续dfs下去了。如果当前字符串在trie里,就说明board可以形成这个wo 阅读全文
posted @ 2015-12-18 03:22 neverlandly 阅读(533) 评论(0) 推荐(0)
Leetcode: Rectangle Area
摘要: Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as sh... 阅读全文
posted @ 2015-12-18 00:48 neverlandly 阅读(324) 评论(0) 推荐(0)
2015年12月17日
Leetcode: Shortest Palindrome
摘要: 本题最简单的思路是从后往前判断子串是否是回文串,然后把后面的弄到前面即可,不过这样仅仅能擦边通过测试 Brute Force: O(N^2), space O(1) DP: O(N^2), space O(N^2) KMP介绍:http://kb.cnblogs.com/page/176818/ 实 阅读全文
posted @ 2015-12-17 14:43 neverlandly 阅读(300) 评论(0) 推荐(0)
Leetcode: Combination Sum III
摘要: ind all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a... 阅读全文
posted @ 2015-12-17 12:56 neverlandly 阅读(262) 评论(0) 推荐(0)
上一页 1 ··· 9 10 11 12 13 14 15 16 17 ··· 21 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3