
Leetcode题解
文章平均质量分 76
沙丁鱼鱼鱼
这个作者很懒,什么都没留下…
展开
-
leetcode题解1-Two Sum
题目:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same...原创 2018-04-14 20:49:46 · 191 阅读 · 0 评论 -
Leetcode题解55- Jump Game
题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you...原创 2018-04-27 15:43:45 · 216 阅读 · 0 评论 -
Leetcode题解70-Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive ...原创 2018-04-27 18:10:14 · 130 阅读 · 0 评论 -
LeetCode题解121- Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock)...原创 2018-05-21 16:28:44 · 164 阅读 · 0 评论 -
Leetcode题解141-Linked List Cycle(C++)
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:设立快慢两个指针,快指针一次移动两步,慢指针一次移动一步。若存在换,那么慢指针会赶上快指针。可以想象成操场上有两个人在跑步,一个快一个慢,那么经过几圈之后,两个人肯定会相遇...原创 2018-05-23 08:58:26 · 158 阅读 · 0 评论 -
Leetcode题解142- Linked List Cycle II(C++)
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?思路:快慢双指针,快指针移...原创 2018-05-23 18:54:07 · 526 阅读 · 0 评论 -
LeetCode题解160-Intersection of Two Linked Lists(C++)
题目:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘ ...原创 2018-05-28 16:36:08 · 191 阅读 · 0 评论 -
LeetCode题解287- Find the Duplicate Number(C++)
题目:Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,...原创 2018-06-05 09:27:19 · 291 阅读 · 0 评论 -
LeetCode题解240-Search a 2D Matrix II(C++)
题目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 c...原创 2018-05-30 16:18:31 · 252 阅读 · 0 评论 -
LeetCode题解215-Kth Largest Element in an Array(C++)
题目: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: 5Examp...原创 2018-05-30 17:03:14 · 347 阅读 · 0 评论 -
LeetCode题解226-Invert Binary Tree(C++)
题目:nvert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this original tweet by M...原创 2018-05-30 21:44:36 · 291 阅读 · 0 评论 -
LeetCode题解347. Top K Frequent Elements
题目:Given a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of unique ele...原创 2018-06-07 14:18:32 · 175 阅读 · 0 评论 -
LeetCode题解221— Maximal Square(C++)
题目:Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.Example:Input: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Output: 4思路:借鉴了LeetCode大神...原创 2018-05-31 14:45:57 · 453 阅读 · 0 评论 -
LeetCode题解-144. Binary Tree Preorder Traversal(二叉树的非递归前序遍历)
题目:Given a binary tree, return the preorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [1,2,3]思路:根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根...原创 2018-06-09 16:07:19 · 254 阅读 · 0 评论 -
Leetcode题解84- Largest Rectangle in Histogram
题目Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each b...原创 2018-05-11 20:41:33 · 175 阅读 · 0 评论 -
leetcode题解32- Longest Valid Parentheses
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid p...原创 2018-04-27 11:50:10 · 134 阅读 · 0 评论 -
leetcode题解7- Reverse Integer(C++和python)
题目:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3:Input: 120Output: 21Note:Assume we are dealing with an en...原创 2018-04-15 10:31:59 · 330 阅读 · 0 评论 -
leetcode题解20—Valid Parentheses
题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of br...原创 2018-04-15 21:50:37 · 147 阅读 · 0 评论 -
leetcode题解3-Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with th...原创 2018-04-16 11:39:11 · 129 阅读 · 0 评论 -
leetcode题解5-Longest Palindromic Substring
题目:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example 1:Input: "babad"Output: "bab"Note: "aba" is also a valid answer.Example...原创 2018-04-17 21:35:40 · 149 阅读 · 0 评论 -
leecode题解22- Generate Parentheses
题目:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())",...原创 2018-04-22 10:50:22 · 169 阅读 · 0 评论 -
leetcode题解11-Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find t...原创 2018-04-18 11:51:06 · 160 阅读 · 0 评论 -
leetcode题解9-Palindrome Number
题目:Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1:Input: 121Output: trueExample 2:Input: -121Output: falseExplanation...原创 2018-04-18 15:16:06 · 139 阅读 · 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.The above elevation map is represented by array...原创 2018-04-30 09:02:27 · 130 阅读 · 0 评论 -
Leetcode题解198- House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent ho...原创 2018-04-30 11:48:53 · 189 阅读 · 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 dire...原创 2018-05-01 14:44:14 · 154 阅读 · 0 评论 -
leetcoe题解17—Letter Combinations of a Phone Number
题目:Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is gi...原创 2018-04-20 19:59:29 · 579 阅读 · 0 评论 -
leetcode题解34- Search for a Range
题目:Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the ...原创 2018-04-27 09:30:17 · 281 阅读 · 0 评论 -
leetcode题解35- Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.E...原创 2018-04-27 10:06:11 · 122 阅读 · 0 评论 -
二叉树重建(C++/Python)
问题:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 思路分析 根据二叉树前序遍历的特点(根-左-右),每次读取的第一个值一定是根节点,这样我们可以在中序遍历的序列中找到当前的根节点的位置。 根...转载 2018-09-09 16:45:42 · 238 阅读 · 0 评论