leetcode题解大全
文章平均质量分 60
这个专栏主要用于记录自己在leetcode上的刷题之路
ghscarecrow
这个作者很懒,什么都没留下…
展开
-
【leetcode】25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of ...原创 2019-02-24 22:05:54 · 117 阅读 · 0 评论 -
【leetcode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list's nodes, only nodes itself may be changed. Example:Given 1->2->3->4, ...原创 2019-02-24 22:04:31 · 108 阅读 · 0 评论 -
【leetcode】383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot...原创 2019-02-18 23:48:48 · 124 阅读 · 0 评论 -
【leetcode】240. 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 in ascending from left to right. Integers in each...原创 2019-02-14 22:21:08 · 99 阅读 · 0 评论 -
【leetcode】263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.Example 1:Input: 6Output: trueExplanation: 6 = 2 ×...原创 2019-02-14 20:23:09 · 104 阅读 · 0 评论 -
【leetcode】204. Count Primes
Count the number of prime numbers less than a non-negative number, n.Example:Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.题解1:利用素数检测class Solu...原创 2019-02-14 20:14:13 · 102 阅读 · 0 评论 -
【leetcode】513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1 Example 2: Input: 1 / \ 2 3 / / \...原创 2019-02-13 10:25:54 · 109 阅读 · 0 评论 -
【leetcode】894. All Possible Full Binary Trees
A full binary tree is a binary tree where each node has exactly 0 or 2 children.Return a list of all possible full binary trees with N nodes. Each element of the answer is the root node of one poss...原创 2019-02-12 20:50:23 · 127 阅读 · 0 评论 -
229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.Note: The algorithm should run in linear time and in O(1) space.Example 1:Input: [3,2,3]Output: [3]Exa...原创 2019-02-11 14:17:06 · 103 阅读 · 0 评论 -
【leetcode】48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix d...原创 2019-02-11 11:57:52 · 152 阅读 · 0 评论 -
【leetcode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Example 1:Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:...原创 2019-02-11 11:08:25 · 118 阅读 · 0 评论 -
【leetcode】784. Letter Case Permutation
Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.Examples:Input: S = "a1b2...原创 2019-02-10 19:44:20 · 117 阅读 · 0 评论 -
【leetcode】77. Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.Example:Input: n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]题解如下:cla...原创 2019-02-10 19:26:31 · 118 阅读 · 0 评论 -
【leetcode】216. Combination Sum III
Find 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 unique set of numbers.Note:All numbers will ...原创 2019-02-10 12:14:12 · 129 阅读 · 0 评论 -
【leetcode】40. Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in candidates may...原创 2019-02-10 11:53:04 · 104 阅读 · 0 评论 -
【leetcode】101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3...原创 2019-02-10 11:17:44 · 121 阅读 · 0 评论 -
【leetcode】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.A sudoku solution must satisfy all of the following rules:Each of the digits 1-9 must occur exactly once in each row. Each of t...原创 2019-02-09 21:52:43 · 107 阅读 · 0 评论 -
【leetcode】203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5key:需要移除所有等于val的结点!题解如下:...原创 2019-02-09 19:25:06 · 106 阅读 · 0 评论 -
【leetcode】445. Add Two Numbers II
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return i...原创 2019-02-09 18:48:17 · 94 阅读 · 0 评论 -
【leetcode】2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2019-02-08 16:42:42 · 74 阅读 · 0 评论 -
【leetcode】329. Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside o...原创 2019-02-08 16:29:43 · 237 阅读 · 0 评论 -
【leetcode】145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [3,2,1]Follow up: Recursive solution is trivial, coul...原创 2019-02-07 23:51:22 · 100 阅读 · 0 评论 -
【leetcode】617. Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree...原创 2019-02-07 23:19:05 · 76 阅读 · 0 评论 -
【leetcode】547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then ...原创 2019-02-07 22:51:57 · 82 阅读 · 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.Example 1:Input: [3,2,1,5,6,4] and k = 2Output: 5E...原创 2019-02-07 11:03:28 · 93 阅读 · 0 评论 -
【leetcode】414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Example 1:Input: [3, 2, 1]...原创 2019-02-07 10:47:13 · 131 阅读 · 0 评论 -
【leetcode】496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2....原创 2019-02-03 20:46:17 · 112 阅读 · 0 评论 -
【leetcode】739. Daily Temperatures
Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which...原创 2019-02-03 20:20:39 · 183 阅读 · 0 评论 -
【leetcode】173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST. Example:...原创 2019-02-03 11:10:44 · 82 阅读 · 0 评论 -
【leetcode】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...原创 2019-02-03 10:45:59 · 166 阅读 · 0 评论 -
【leetcode】637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanati...原创 2019-02-02 23:39:21 · 108 阅读 · 0 评论 -
【leetcode】107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15,7...原创 2019-02-02 23:31:19 · 91 阅读 · 0 评论 -
【leetcode】102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 ...原创 2019-02-02 23:18:09 · 83 阅读 · 0 评论 -
【leetcode】111. Minimum Depth of Binary Tree
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.Note: A leaf is a node with no childre...原创 2019-02-02 23:03:28 · 99 阅读 · 0 评论 -
【leetcode】649. Dota2 Senate
In the world of Dota2, there are two parties: the Radiant and the Dire.The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Do...原创 2019-02-02 13:57:08 · 126 阅读 · 0 评论 -
【leetcode】881. Boats to Save People
The i-th person has weight people[i], and each boat can carry a maximum weight of limit.Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most...原创 2019-02-02 11:59:29 · 146 阅读 · 0 评论 -
【leetcode】80. Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.Do not allocate extra space for another array, you must do this by mod...原创 2019-02-02 11:40:07 · 76 阅读 · 0 评论 -
【leetcode】832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.To flip an image horizontally means that each row of the image is reversed. For examp...原创 2019-02-02 10:59:45 · 107 阅读 · 0 评论 -
【leetcode】59. Spiral Matrix II
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.Example:Input: 3Output:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 题解如下:class Sol...原创 2019-02-02 10:48:08 · 92 阅读 · 0 评论 -
【leetcode】54. Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Output: [1,2,3,6,9,8,7,4,5]...原创 2019-02-02 10:29:45 · 94 阅读 · 0 评论