
leetcode
文章平均质量分 72
jimhust
这个作者很懒,什么都没留下…
展开
-
336. Palindrome Pairs
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome.Example 1:Give原创 2017-03-27 13:10:04 · 230 阅读 · 0 评论 -
310. 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 rooted trees, those with minimum height are called mini原创 2017-03-20 15:01:34 · 179 阅读 · 0 评论 -
Some useful tricks in bit manipulation
1. a + b = (a ^ b) + (a & b) Example: add a and b without the add operation.public int add(int a, int b){ while(b != 0){ int newA = a ^ b; int newB = (a & b) << 1; a =原创 2017-03-10 06:25:15 · 407 阅读 · 0 评论 -
223. 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 shown in the figure.Assume that the tota原创 2017-03-07 07:24:06 · 175 阅读 · 0 评论 -
220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute diffe原创 2017-03-07 06:22:30 · 349 阅读 · 0 评论 -
309. Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on原创 2017-03-20 14:14:34 · 198 阅读 · 0 评论 -
214. Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.For exam原创 2017-03-06 15:18:37 · 186 阅读 · 0 评论 -
212. Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those hor原创 2017-03-06 11:58:58 · 192 阅读 · 0 评论 -
201. Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.Solution: if m == n return mif(m != n) we can divide m and n into to partm1 | m2 n1 |原创 2017-03-05 14:41:51 · 181 阅读 · 0 评论 -
174. Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially p原创 2017-03-05 14:24:33 · 175 阅读 · 0 评论 -
179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2017-03-05 13:04:05 · 152 阅读 · 0 评论 -
233. 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, because digit 1 occurred in the follow原创 2017-03-08 16:41:33 · 344 阅读 · 0 评论 -
166. Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.原创 2017-03-04 17:18:01 · 163 阅读 · 0 评论 -
165. Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2017-03-04 16:43:21 · 207 阅读 · 0 评论 -
137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtime complexity. Coul原创 2017-03-03 11:11:43 · 187 阅读 · 0 评论 -
32. Longest Valid Parentheses
The naive thinking:i -> jif(i == '(' && j ==')')dp[i][j] = dp[i - 1][j - 1] || (dp[i][k] && dp[k][j])O(n3)Stupid Code:public class Solution { //dp[i][j] public int longestValidPare原创 2017-02-28 07:44:50 · 243 阅读 · 0 评论 -
241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.Example 1原创 2017-03-10 21:19:06 · 162 阅读 · 0 评论 -
250. 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 binary tree, 5原创 2017-03-10 21:53:05 · 262 阅读 · 0 评论 -
333. Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.Note:A subtree must include all of its descendan原创 2017-03-25 22:33:41 · 283 阅读 · 0 评论 -
316. Remove Duplicate Letters
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order a原创 2017-03-25 09:18:49 · 211 阅读 · 0 评论 -
331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.原创 2017-03-23 11:35:58 · 198 阅读 · 0 评论 -
282. Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or *between the digits so they evaluate to the target value.原创 2017-03-13 18:14:12 · 209 阅读 · 0 评论 -
356. Line Reflection
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.Example 1:Given points = [[1,1],[-1,1]], return true.Example 2:Given poi原创 2017-03-29 13:30:02 · 268 阅读 · 0 评论 -
321. Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k from digits of the two. The relative order of the digits from the same array mus原创 2017-03-22 11:58:46 · 378 阅读 · 0 评论 -
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =原创 2017-03-12 20:37:22 · 203 阅读 · 0 评论 -
277. Find the Celebrity
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1 people know him/her but原创 2017-03-12 20:15:26 · 213 阅读 · 0 评论 -
272. Closest Binary Search Tree Value II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating point.You may assume k is always valid,原创 2017-03-12 19:35:17 · 207 阅读 · 0 评论 -
318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case le原创 2017-03-21 19:26:17 · 207 阅读 · 0 评论 -
267. Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.For example:Given s = "aabb", return ["abba原创 2017-03-12 12:03:12 · 214 阅读 · 0 评论 -
261. Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.For example:Given n =原创 2017-03-12 11:40:06 · 336 阅读 · 0 评论 -
255. Verify Preorder Sequence in Binary Search Tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequence is unique.Follow up:Could you do原创 2017-03-11 21:07:14 · 219 阅读 · 0 评论 -
351. Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maxim原创 2017-03-28 11:21:49 · 539 阅读 · 0 评论 -
229. 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.Hint:How many majority elements could it po原创 2017-03-07 20:13:27 · 173 阅读 · 0 评论 -
132. Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Retu原创 2017-03-03 08:05:42 · 171 阅读 · 0 评论 -
128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2017-03-02 16:40:19 · 135 阅读 · 0 评论 -
Leetcode Everyday: 169. Majority Element
https://leetcode.com/problems/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原创 2016-05-12 12:49:57 · 182 阅读 · 0 评论 -
Leetcode Everyday: 217. Contains Duplicate
https://leetcode.com/problems/contains-duplicate/Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in th原创 2016-05-12 12:26:45 · 183 阅读 · 0 评论 -
Leetcode Everyday: 252. Meeting Rooms
https://leetcode.com/problems/meeting-rooms/Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all原创 2016-05-12 10:52:02 · 290 阅读 · 0 评论 -
Leetcode Everyday: 242. Valid Anagram
https://leetcode.com/problems/valid-anagram/Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "ra原创 2016-05-12 00:13:42 · 184 阅读 · 0 评论 -
Leetcode Everyday: 100. Same Tree
https://leetcode.com/problems/same-tree/Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical an原创 2016-05-12 00:07:10 · 182 阅读 · 0 评论