LeetCode题解
LeetCode题目思路解析
i逆天耗子丶
这个作者很懒,什么都没留下…
展开
-
LeetCode 287. Find the Duplicate Number
Find the Duplicate Number题目描述: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原创 2017-09-16 19:35:11 · 619 阅读 · 0 评论 -
LeetCode 203. Remove Linked List Elements
Remove Linked List Elements题目描述:Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 -->原创 2017-09-17 20:50:06 · 361 阅读 · 0 评论 -
LeetCode 237. Delete Node in a Linked List
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 an原创 2017-09-17 21:02:44 · 412 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List
Reverse Linked List题目思路:将题目给出的链表反转。使用中间变量将每个链表中元素的指向下一个元素的指针指向前一个元素即可。最后返回新的头指针节点。题目代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *nex原创 2017-09-18 15:21:26 · 499 阅读 · 0 评论 -
LeetCode 83. Remove Duplicates from Sorted List
Remove Duplicates from Sorted List题目描述:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3原创 2017-09-18 15:26:59 · 457 阅读 · 0 评论 -
LeetCode 21. Merge Two Sorted Lists
Merge Two Sorted Lists题目思路:题目要将两个排好序的链表合并。我们新声明一个指针,表示合并后链表的头节点元素的指针,最后返回头节点指针即可。我们再次声明一个指针,用来合并两个数组,我们可以依次将两个链表中较小的元素加到指针的后面,知道用完全部的元素。题目代码:/** * Definition for singly-linked list.原创 2017-09-18 15:30:30 · 459 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle
Linked List Cycle题目思路:判断给定的链表中是否存在环。参考这篇文章题目代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next原创 2017-09-18 15:36:01 · 474 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II
Linked List Cycle II题目思路判断给定链表中是否存在环,如果存在环返回环的起点。参考这篇文章。题目代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x原创 2017-09-18 15:39:07 · 490 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List
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?题目思路:给定一个链表给定头指针,判断这个链表是否是“回文”链表。我们原创 2017-09-19 11:42:51 · 435 阅读 · 0 评论 -
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal题目描述:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in原创 2017-09-13 14:33:17 · 510 阅读 · 0 评论 -
LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal题目描述:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist原创 2017-09-13 14:39:58 · 668 阅读 · 0 评论 -
LeetCode 153. Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array题目描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).原创 2017-09-13 14:44:14 · 468 阅读 · 0 评论 -
LeetCode 162. Find Peak Element
Find Peak Element题目描述:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array原创 2017-09-13 14:50:02 · 531 阅读 · 0 评论 -
LeetCode 667. Beautiful Arrangement II
Beautiful Arrangement II 题目描述:Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requiremen原创 2017-09-12 09:27:23 · 622 阅读 · 0 评论 -
LeetCode 39. Combination Sum
Combination Sum题目描述:Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same rep原创 2017-09-12 09:32:21 · 359 阅读 · 0 评论 -
LeetCode 40. Combination Sum II
Combination Sum II题目描述:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may onl原创 2017-09-12 09:39:04 · 332 阅读 · 0 评论 -
LeetCode 216. Combination Sum III
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原创 2017-09-12 09:44:16 · 315 阅读 · 0 评论 -
LeetCode 11. Container With Most Water
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原创 2017-09-12 09:51:36 · 383 阅读 · 0 评论 -
LeetCode 674. Longest Continuous Increasing Subsequence
Longest Continuous Increasing Subsequence题目描述:Given an unsorted array of integers, find the length of longest continuous increasing subsequence.Example 1:Input: [1,3,5,4,7]Output: 3原创 2017-09-10 20:58:26 · 367 阅读 · 0 评论 -
LeetCode 15. 3Sum
3Sum题目描述:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set m原创 2017-09-11 15:11:06 · 416 阅读 · 0 评论 -
LeetCode 16. 3Sum Closest
3Sum Closest题目描述:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that ea原创 2017-09-11 15:18:51 · 421 阅读 · 0 评论 -
LeetCode 18. 4Sum
4Sum题目描述:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Not原创 2017-09-11 15:27:32 · 408 阅读 · 0 评论 -
LeetCode 565. Array Nesting
Array Nesting题目描述:A zero-indexed array A consisting of N different integers is given. The array contains all integers in the range [0, N - 1]. Sets S[K] for 0 S[K] = { A[K], A[A[K]], A[A[A原创 2017-09-11 15:50:20 · 1506 阅读 · 0 评论 -
LeetCode 605. Can Place Flowers
Can Place Flowers题目描述:Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for w原创 2017-09-09 10:20:00 · 709 阅读 · 0 评论 -
LeetCode 628. Maximum Product of Three Numbers
Maximum Product of Three Numbers题目描述:Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:原创 2017-09-09 10:24:49 · 589 阅读 · 0 评论 -
LeetCode 643. Maximum Average Subarray I
Maximum Average Subarray I题目描述:Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the maximu原创 2017-09-09 10:30:53 · 674 阅读 · 0 评论 -
LeetCode 661. Image Smoother
Image Smoother题目描述:Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (roundi原创 2017-09-09 10:35:39 · 1546 阅读 · 0 评论 -
LeetCode 665. Non-decreasing Array
Non-decreasing Array题目描述:Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if ar原创 2017-09-09 10:49:15 · 2936 阅读 · 0 评论 -
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原创 2017-09-04 20:01:13 · 355 阅读 · 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 an原创 2017-09-04 20:19:10 · 270 阅读 · 0 评论 -
LeetCode 26. Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array题目描述:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for原创 2017-09-05 11:26:06 · 275 阅读 · 0 评论 -
LeetCode 27. Remove Element
Remove Element题目描述:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in plac原创 2017-09-05 12:35:59 · 261 阅读 · 0 评论 -
LeetCode 35. Search Insert Position
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原创 2017-09-05 12:46:09 · 256 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
Maximum Subarray题目描述: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],th原创 2017-09-05 12:53:09 · 300 阅读 · 0 评论 -
LeetCode 66. Plus One
Plus One题目描述:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the num原创 2017-09-05 12:59:00 · 332 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
Merge Sorted Array题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or原创 2017-09-06 15:34:35 · 365 阅读 · 0 评论 -
LeetCode 118. Pascal's Triangle
Pascal's Triangle题目描述:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]原创 2017-09-06 15:51:55 · 246 阅读 · 0 评论 -
LeetCode 119. Pascal's Triangle II
119. Pascal's Triangle II题目描述:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use原创 2017-09-06 16:03:30 · 260 阅读 · 0 评论 -
LeetCode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II题目描述: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 c原创 2017-09-06 16:18:10 · 217 阅读 · 0 评论 -
LeetCode 167. Two Sum II - Input array is sorted
Two Sum II - Input array is sorted题目描述:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function原创 2017-09-06 16:23:59 · 269 阅读 · 0 评论