Arrays
林下溪源
为学日益,为道日损
展开
-
643. Maximum Average Subarray I
Problem Description:Given an array consisting ofnintegers, find the contiguous subarray of given lengthkthat has the maximum average value. And you need to output the maximum average value.Ex...原创 2019-12-17 14:49:20 · 154 阅读 · 0 评论 -
628. Maximum Product of Three Numbers
Problem Description: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:Input: [1,2,3,4]Out...原创 2019-12-17 14:38:45 · 121 阅读 · 0 评论 -
621. Task Scheduler
Problem Description:Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original ...原创 2019-12-06 18:48:00 · 108 阅读 · 0 评论 -
611. Valid Triangle Number
Problem Description:Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a...原创 2019-12-02 15:36:29 · 91 阅读 · 0 评论 -
605. Can Place Flowers
Problem Description: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 water an...原创 2019-12-02 14:49:41 · 149 阅读 · 0 评论 -
581. Shortest Unsorted Continuous Subarray
Problem Description:Given an integer array, you need to find onecontinuous subarraythat if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, ...原创 2019-12-02 14:35:04 · 88 阅读 · 0 评论 -
566. Reshape the Matrix
Problem Description: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 given a matrix rep...原创 2019-11-29 19:22:11 · 104 阅读 · 0 评论 -
565. Array Nesting
Problem Description:A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], ... } subjected to ...原创 2019-11-29 18:55:08 · 130 阅读 · 0 评论 -
561. Array Partition I
Problem Description:Given an array of2nintegers, your task is to group these integers intonpairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from ...原创 2019-11-25 15:52:36 · 95 阅读 · 0 评论 -
560. Subarray Sum Equals K
Problem Description:Given an array of integers and an integerk, you need to find the total number of continuous subarrays whose sum equals tok.Example 1:Input:nums = [1,1,1], k = 2Output: ...原创 2019-11-24 05:40:01 · 97 阅读 · 0 评论 -
532. K-diff Pairs in an Array
Problem Description:Given an array of integers and an integerk, you need to find the number ofuniquek-diff pairs in the array. Here ak-diffpair is defined as an integer pair (i, j), whereian...原创 2019-11-24 01:52:55 · 140 阅读 · 0 评论 -
509. Fibonacci Number
Problem Description:TheFibonacci numbers, commonly denotedF(n)form a sequence, called theFibonacci sequence, such that each number is the sum of the two preceding ones, starting from0and1. T...原创 2019-11-24 01:34:56 · 138 阅读 · 0 评论 -
495. Teemo Attacking
Problem Description:In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attackingascendingtime series towards Ash...原创 2019-11-24 01:30:32 · 149 阅读 · 0 评论 -
485. Max Consecutive Ones
Problem Description: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 d...原创 2019-11-23 21:01:16 · 124 阅读 · 0 评论 -
457. Circular Array Loop
Problem Description:You are given acirculararraynumsof positive and negative integers. If a numberkat an index is positive, then move forwardksteps. Conversely, if it's negative (-k), move ...原创 2019-11-23 20:23:10 · 112 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
Problem Description: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] inclusive that do not appe...原创 2019-11-13 16:13:36 · 89 阅读 · 0 评论 -
442. Find All Duplicates in an Array
Problem Description:Given an array of integers, 1 ≤ a[i] ≤n(n= size of array), some elements appeartwiceand others appearonce.Find all the elements that appeartwicein this array.Could y...原创 2019-11-13 12:07:02 · 150 阅读 · 0 评论 -
414. Third Maximum Number
Problem Description:Given anon-emptyarray of integers, return thethirdmaximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).Exampl...原创 2019-11-12 19:49:28 · 82 阅读 · 0 评论 -
381. Insert Delete GetRandom O(1) - Duplicates allowed
Problem Decription:Design a data structure that supports all following operations inaverageO(1)time.Note: Duplicate elements are allowed.insert(val): Inserts an item val to the collection....原创 2019-11-12 16:38:22 · 123 阅读 · 0 评论 -
380. Insert Delete GetRandom O(1)
Problem Description:Design a data structure that supports all following operations inaverageO(1)time.insert(val): Inserts an item val to the set if not already present. remove(val): Remove...原创 2019-11-12 14:25:03 · 91 阅读 · 0 评论 -
289. Game of Life
Problem Description:According to theWikipedia's article: "TheGame of Life, also known simply asLife, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."Gi...原创 2019-11-11 16:09:00 · 98 阅读 · 0 评论 -
287. Find the Duplicate Number
Problem Description:Given an arraynumscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only o...原创 2019-11-11 15:11:20 · 199 阅读 · 0 评论 -
283. Move Zeroes
Problem Description:Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,...原创 2019-11-11 14:32:37 · 100 阅读 · 0 评论 -
268. Missing Number
Problem Description:Given an array containingndistinct numbers taken from0, 1, 2, ..., n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input...原创 2019-11-11 14:26:11 · 83 阅读 · 0 评论 -
238. Product of Array Except Self
Problem Description:Given an arraynumsofnintegers wheren> 1, return an arrayoutputsuch thatoutput[i]is equal to the product of all the elements ofnumsexceptnums[i].Example:Inp...原创 2019-11-11 14:14:37 · 89 阅读 · 0 评论 -
229. Majority Element II
Problem Description:Given an integer array of sizen, 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: [...原创 2019-11-09 16:33:35 · 83 阅读 · 0 评论 -
228. Summary Ranges
Problem Description:Given a sorted integer array without duplicates, return the summary of its ranges.Example 1:Input: [0,1,2,4,5,7]Output: ["0->2","4->5","7"]Explanation: 0,1,2 form ...原创 2019-11-09 13:39:04 · 101 阅读 · 0 评论 -
219. Contains Duplicate II
Problem Description:Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and theabsolutedifference between...原创 2019-11-09 11:47:51 · 107 阅读 · 0 评论 -
217. Contains Duplicate
Problem Description:Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return f...原创 2019-11-09 11:32:01 · 85 阅读 · 0 评论 -
216. Combination Sum III
Problem Description:Find all possible combinations ofknumbers that add up to a numbern, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.N...原创 2019-11-09 11:22:23 · 90 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
Problem Description:Given an array ofnpositive integers and a positive integers, find the minimal length of acontiguoussubarray of which the sum ≥s. If there isn't one, return 0 instead.Exa...原创 2019-11-08 18:11:45 · 92 阅读 · 0 评论 -
189. Rotate Array
Problem Description:Given an array, rotate the array to the right byksteps, wherekis non-negative.Example 1:Input: [1,2,3,4,5,6,7] and k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 s...原创 2019-11-07 13:56:32 · 85 阅读 · 0 评论 -
169. Majority Element
Problem Description:Given an array of sizen, find the majority element. The majority element is the element that appearsmore than⌊ n/2 ⌋times.You may assume that the array is non-empty and th...原创 2019-11-02 16:37:14 · 140 阅读 · 0 评论 -
167. Two Sum II - Input array is sorted
Problem Description:Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indi...原创 2019-11-02 16:24:45 · 87 阅读 · 0 评论 -
162. Find Peak Element
Problem Description:A peak element is an element that is greater than its neighbors.Given an input arraynums, wherenums[i] ≠ nums[i+1], find a peak element and return its index.The array may ...原创 2019-11-02 16:15:03 · 83 阅读 · 0 评论 -
154. Find Minimum in Rotated Sorted Array II
Problem Description: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]).Find the minimum element...原创 2019-11-02 15:58:09 · 90 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array
Problem Description: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]).Find the minimum element...原创 2019-11-02 15:47:29 · 101 阅读 · 0 评论 -
128. Longest Consecutive Sequence
Problem Description:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Your algorithm should run in O(n) complexity.Example:Input:[100, 4, 200...原创 2019-11-01 16:18:47 · 139 阅读 · 0 评论 -
126. Word Ladder II
Problem Description:Given two words (beginWordandendWord), and a dictionary's word list, find all shortest transformation sequence(s) frombeginWordtoendWord, such that:Only one letter can be...原创 2019-11-01 11:44:07 · 129 阅读 · 0 评论 -
152. Maximum Product Subarray
Problem Description:Given an integer arraynums, find the contiguous subarray within an array (containing at least one number) which has the largest product.Example 1:Input: [2,3,-2,4]Output:...原创 2019-10-30 19:29:26 · 82 阅读 · 0 评论