需要好好想一下的题目
JackZhangNJU
未来的路还很长
展开
-
leetcode 44. Wildcard Matching (需要好好想一下)
Implement wildcard pattern matching with support for ‘?’ and ‘*’.‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence).The matching should cover ...原创 2017-09-03 16:23:27 · 416 阅读 · 0 评论 -
leetcode 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.提议很简单,就是归并排序。首先想到的即使逐个归并得到最终的结果,但是会超时,这是因为这种会造成数组的size大小不一样,导致归并排序的时间变长;最好的做法是两两合并,然后在两两合并,这样不会超时, 需要注原创 2017-08-31 17:19:41 · 362 阅读 · 0 评论 -
leetcode 215. Kth Largest Element in an Array 堆排序的简单应用 + 快速排序
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example, Given [3,2,1,5,6,4] and k = 2, return 5.这...原创 2017-09-22 09:53:48 · 700 阅读 · 0 评论 -
leetcode 53. Maximum Subarray 动态规划DP + 最大子串和
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has ...原创 2017-09-04 10:00:41 · 643 阅读 · 0 评论 -
leetcode 56. Merge Intervals 区间合并
Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].这道题考察的是区间合并,解决方法很简单,按照区间的起始值从小到大排序,然后逐个合并即可。需要注意的地方是合并之后一个区原创 2017-09-04 14:55:05 · 437 阅读 · 0 评论 -
leetcode 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.For exam原创 2017-09-19 10:21:47 · 815 阅读 · 0 评论 -
leetcode 172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.这里我们要求n!末尾有多少个0,因为我们知道0是2和5相乘得到的,而在1到n这个范围内,2的个数要远多于5的个数,所以这里只需计算从1到n这个范围内有多少原创 2017-09-19 10:44:11 · 357 阅读 · 0 评论 -
leetcode 65. Valid Number
Validate if a given string is numeric.Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => true Note: It is intended for the problem statement to be ambiguous. You s原创 2017-09-05 11:03:28 · 225 阅读 · 0 评论 -
leetcode 174. Dungeon Game 一个逆着推导计算的DP动态规划问题
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 posi原创 2017-09-20 09:11:02 · 378 阅读 · 0 评论 -
leetcode 72. Edit Distance DP动态规划 + 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2。 (each operation is counted as 1 step。)You have the following 3 operations permitted on a word:a) In原创 2017-09-10 21:13:23 · 386 阅读 · 1 评论 -
leetcode 74. Search a 2D Matrix 右上角搜索
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 from left to right. The first integer of each row is原创 2017-09-10 21:40:03 · 229 阅读 · 0 评论 -
leetcode 75. Sort Colors 很不错的3种元素排序方法 + O(n)
Given an array with n objects colored red。 white or blue。 sort them so that objects of the same color are adjacent。 with the colors in the order red。 white and blue。Here。 we will use the integers 0。 1。原创 2017-09-10 21:51:04 · 632 阅读 · 0 评论 -
leetcode 204. Count Primes 这道题很有趣 + 搜索空间递减
Description:Count the number of prime numbers less than a non-negative number, n.这道题就是考察小于n的所有的质数的数量,直接遍历肯定超时,所以使用布尔数组来筛掉不符合条件的数字,该数字就不再进入下一层循环进行判断了。代码如下:import java.util.Arrays;public class Solution原创 2017-09-21 09:18:03 · 224 阅读 · 0 评论 -
leetcode 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2原创 2017-09-11 22:06:16 · 267 阅读 · 0 评论 -
leetcode 109. Convert Sorted List to Binary Search Tree 链表构造平衡二叉搜索树 + DFS
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.根据一个排序好的链表构造二叉平衡树,这个问题的重点是找到中间元素,使用双指针即可。建议和 leetcode 108. Convert Sorted Array to Binary Se原创 2017-09-14 11:13:48 · 458 阅读 · 0 评论 -
leetcode 111. Minimum Depth of Binary Tree DFS深度优先遍历 + 添加对叶子节点判断
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.本题考查的就是平衡二叉树的最小深度,其实跟Maximum Depth of ...原创 2017-09-13 09:46:32 · 219 阅读 · 0 评论 -
leetcode 237. 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 -> 4 and you are given the third node with value 3, t原创 2017-09-26 09:21:10 · 249 阅读 · 0 评论 -
leetcode 42. Trapping Rain Water 正反循环遍历求解
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], ret原创 2017-09-03 15:30:01 · 366 阅读 · 0 评论 -
leetcode 454. 4Sum II 四个数求和的总次数+Map降低运算复杂度
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.To make problem a bit easier, all A, B, C, D have same lengt...原创 2017-12-09 17:17:12 · 322 阅读 · 0 评论 -
leetcode 452. Minimum Number of Arrows to Burst Balloons 消除覆盖区间
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordina原创 2017-12-09 18:06:35 · 309 阅读 · 0 评论 -
leetcode 127. Word Ladder BFS广度优先遍历
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a ti...原创 2017-09-16 12:03:15 · 715 阅读 · 0 评论 -
leetcode 126. Word Ladder II BFS + 反向链表 + DFS
Given two words (beginWord and endWord), and a dictionary’s word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:Only one letter can be changed at a time Eac...原创 2017-09-16 12:20:42 · 584 阅读 · 0 评论 -
leetcode 76. Minimum Window Substring 包含目标串的最小子串+典型移动窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example, S = “ADOBECODEBANC” T = “ABC” Minimum window is “BANC”.N...原创 2017-09-11 21:08:47 · 559 阅读 · 0 评论 -
leetcode 600. Non-negative Integers without Consecutive Ones 非负整数不包括连续的1 + DP动态规划
Given a positive integer n, find the number of non-negative integers less than or equal to n, whose binary representations do NOT contain consecutive ones.Example 1: Input: 5 Output: 5 Explanation:原创 2017-12-21 09:06:27 · 654 阅读 · 0 评论 -
leetcode 606. Construct String from Binary Tree 前序遍历 + 深度优先遍历DFS
You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. And you nee原创 2017-12-21 09:10:14 · 169 阅读 · 0 评论 -
leetcode 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, 1...原创 2017-10-02 10:15:30 · 557 阅读 · 0 评论 -
leetcode 630. Course Schedule III 课程调度 + 选择最多的课 + 任务安排 + 贪心算法
There are n different online courses numbered from 1 to n. Each course has some duration(course length) t and closed on dth day. A course should be taken continuously for t days and must be finished b...原创 2017-12-20 11:30:11 · 1394 阅读 · 0 评论 -
leetcode 392. Is Subsequence 子序列判断 深度优先遍历DFS + 一个很简单的循环
Given a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, an原创 2017-12-05 16:53:55 · 600 阅读 · 0 评论 -
leetcode 482. License Key Formatting
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.Given a number K, we would want to refor原创 2017-12-14 09:42:31 · 182 阅读 · 0 评论 -
leetcode 498. Diagonal Traverse 矩阵对角遍历 + 控制方向即可
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ...原创 2017-12-14 09:44:33 · 591 阅读 · 0 评论 -
leetcode 459. Repeated Substring Pattern 暴力拆分即可
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase Englis原创 2017-12-14 11:14:01 · 174 阅读 · 0 评论 -
leetcode 481. Magical String 神奇字符串+有点绕的题
A magical string S consists of only ‘1’ and ‘2’ and obeys the following rules:The string S is magical because concatenating the number of contiguous occurrences of characters ‘1’ and ‘2’ generates t...原创 2017-12-14 14:56:31 · 651 阅读 · 0 评论 -
leetcode 124. Binary Tree Maximum Path Sum 最大路径和 + DFS深度优先搜索
Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path原创 2017-09-15 10:49:00 · 448 阅读 · 0 评论 -
leetcode 664. Strange Printer 奇怪的打印机+最小打印次数 + 动态规划DP
There is a strange printer with the following two special requirements:The printer can only print a sequence of the same character each time. At each turn, the printer can print new characters star...原创 2017-12-22 13:26:47 · 891 阅读 · 0 评论 -
leetcode 295. Find Median from Data Stream 双优先级队列 + 中位数查找
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples: [2,3,4] , the median is 3原创 2017-09-28 11:24:24 · 692 阅读 · 0 评论 -
leetcode 516. Longest Palindromic Subsequence 最长回文子序列 + DP动态规划
Given a string s, find the longest palindromic subsequence’s length in s. You may assume that the maximum length of s is 1000.Example 1: Input:“bbbab” Output: 4 One possible longest palindromic sub原创 2017-12-16 15:10:47 · 734 阅读 · 0 评论 -
leetcode 520. Detect Capital 检查大小写 + 统计大写字母即可
Given a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be right when one of the following cases holds:All letters in this ...原创 2017-12-16 16:46:37 · 270 阅读 · 0 评论 -
leetcode 406. Queue Reconstruction by Height 人群高度排序
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this p原创 2017-12-07 19:53:35 · 174 阅读 · 0 评论 -
leetcode 751. IP to CIDR
Given a start IP address ip and a number of ips we need to cover n, return a representation of the range as a list (of smallest possible length) of CIDR blocks.A CIDR block is a string consisting of an原创 2017-12-26 10:34:06 · 749 阅读 · 0 评论 -
leetcode 753. Cracking the Safe 全排列获取保险箱密码+深度优先遍历DFS + 贪心策略
There is a box protected by a password. The password is n digits, where each letter can be one of the first k digits 0, 1, …, k-1.You can keep inputting the password, the password will automatically be原创 2017-12-26 10:53:35 · 2066 阅读 · 0 评论