Leetcode题目解析
包含Leetcode题目源代码、解析,欢迎大家一起讨论
灵魂画手-编程如画
大工小本,计算所小硕一枚,奋斗ing,相信技术改变世界!
展开
-
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 only O(k) extra spac原创 2017-06-03 13:37:16 · 255 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
26. 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 another a原创 2017-06-02 20:49:23 · 225 阅读 · 0 评论 -
27. Remove Element
27. 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 place with const原创 2017-06-02 20:50:20 · 221 阅读 · 0 评论 -
35. Search Insert Position
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 d原创 2017-06-02 20:54:00 · 1422 阅读 · 0 评论 -
66. Plus One
66. 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 number 0 itself.The原创 2017-06-02 17:25:13 · 343 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
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 (ie, buy one原创 2017-06-02 17:20:23 · 285 阅读 · 0 评论 -
219. Contains Duplicate II
219 contains duplicate ii题目Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference betw原创 2017-05-28 13:28:37 · 288 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array题目Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclu原创 2017-06-02 20:51:43 · 213 阅读 · 0 评论 -
485. Max Consecutive Ones
485. Max Consecutive Ones题目Given a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three dig原创 2017-06-02 20:53:03 · 205 阅读 · 0 评论 -
532. K-diff Pairs in an Array
532. K-diff Pairs in an Array题目Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), whe原创 2017-06-02 17:30:21 · 387 阅读 · 0 评论 -
118. Pascal's Triangle
118. 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]]翻译给定numRows,生原创 2017-06-03 11:10:27 · 248 阅读 · 0 评论 -
561. Array Partition I
561. Array Partition I题目Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1原创 2017-06-05 20:32:30 · 380 阅读 · 0 评论 -
88. Merge Sorted Array
88. 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 equal to m原创 2017-06-06 15:18:15 · 280 阅读 · 0 评论 -
566. Reshape the Matrix
122. Best Time to Buy and Sell Stock II题目In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data.You’re giv原创 2017-06-02 20:57:02 · 569 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
167. 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 twoSum sho原创 2017-06-06 14:38:25 · 201 阅读 · 0 评论 -
169. Majority Element
169. 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 that the array is non-empty and the m原创 2017-06-09 08:59:27 · 218 阅读 · 0 评论 -
189. Rotate Array
189. Rotate Array题目Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note: Try to come up as many solutio原创 2017-06-09 09:04:35 · 339 阅读 · 0 评论 -
551. Student Attendance Record I
551. Student Attendance Record I题目You are given a string representing an attendance record for a student. The record only contains the following three characters:‘A’ : Absent. ‘L’ : Late. ‘P’ : Prese原创 2017-06-09 10:43:14 · 708 阅读 · 0 评论 -
537. Complex Number Multiplication
537. Complex Number Multiplication1、题目Given two strings representing two complex numbers.原创 2017-06-09 11:14:23 · 373 阅读 · 0 评论 -
609. Find Duplicate File in System
609. Find Duplicate File in System题目Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in原创 2017-06-11 11:33:20 · 930 阅读 · 0 评论 -
556. Next Greater Element III
556. Next Greater Element III1 题目Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than原创 2017-06-11 14:15:21 · 419 阅读 · 0 评论 -
609. Find Duplicate File in System
609. Find Duplicate File in System1 题目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 em原创 2017-06-13 16:15:37 · 336 阅读 · 0 评论 -
575. Distribute Candies
575. Distribute Candies1 题目Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. Yo原创 2017-06-20 13:25:09 · 408 阅读 · 0 评论 -
389. Find the Difference
389. Find the Difference1 题目Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find t原创 2017-06-20 14:18:56 · 236 阅读 · 0 评论 -
454. 4Sum II
454. 4Sum II1 题目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 ha原创 2017-06-22 14:03:10 · 308 阅读 · 0 评论 -
347. Top K Frequent Elements
347. Top K Frequent Elements1 题目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 val原创 2017-06-23 14:39:32 · 218 阅读 · 0 评论 -
566. Reshape the Matrix
转载自:http://algorithm.books.mafengshe.com/answer/566.html566. Reshape the Matrix题目描述:In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different转载 2017-09-24 18:16:38 · 261 阅读 · 0 评论 -
560. Subarray Sum Equals K
转载自 http://algorithm.books.mafengshe.com/answer/560.html560. Subarray Sum Equals K题目描述:Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum e转载 2017-09-24 18:17:34 · 188 阅读 · 0 评论 -
128. Longest Consecutive Sequence
转载自 http://algorithm.books.mafengshe.com/answer/128.html128. Longest Consecutive Sequence题目描述:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For examp转载 2017-09-24 18:18:20 · 210 阅读 · 0 评论 -
1. Two Sum
转载自 http://algorithm.books.mafengshe.com/answer/1.html1. 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 in转载 2017-09-29 10:02:59 · 165 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
转载自 http://algorithm.books.mafengshe.com/answer/26.html26. Remove Duplicates from Sorted Array题目描述:Given a sorted array, remove the duplicates in place such that each element appear only once and retur转载 2017-09-29 10:04:21 · 145 阅读 · 0 评论 -
11. Container With Most Water
转载自 http://algorithm.books.mafengshe.com/answer/11.html11. Container With Most Water题目描述:Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lin转载 2017-09-29 10:07:06 · 205 阅读 · 0 评论 -
15. 3Sum
转载自 http://algorithm.books.mafengshe.com/answer/3.html15. 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 g转载 2017-09-29 10:08:15 · 175 阅读 · 0 评论 -
20. Valid Parentheses
同步发表于 http://algorithm.books.mafengshe.com/answer/20.html题目描述:Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.The brackets must clo原创 2017-10-11 10:40:45 · 249 阅读 · 0 评论 -
693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.Example 1:Input: 5Output: TrueExplanation:The binary representation o原创 2017-10-13 21:18:38 · 203 阅读 · 3 评论 -
[java] 36. Valid Sudoku
同步发表于 http://algorithm.books.mafengshe.com/answer/36.html36. Valid Sudoku题目描述:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where原创 2017-10-18 16:21:40 · 645 阅读 · 0 评论 -
518. Coin Change 2
518. Coin Change 2题目You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you原创 2017-12-27 16:17:42 · 275 阅读 · 0 评论 -
205. Isomorphic Strings
205. Isomorphic Strings题目Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be re原创 2017-12-27 17:34:22 · 183 阅读 · 0 评论 -
396. Rotate Function
396. Rotate Function题目Given an array of integers A and let n to be its length.Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a “rotation function”F on Aas f原创 2017-12-27 19:11:40 · 299 阅读 · 0 评论 -
66. Plus One
66. 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 number 0 itself.The原创 2017-12-28 11:54:49 · 249 阅读 · 0 评论