leetcode-array
文章平均质量分 53
shanshanhi
这个作者很懒,什么都没留下…
展开
-
18. 4Sum leetcode (array) 总结
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. Note: The soluti...原创 2016-11-28 18:51:18 · 365 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array and 80. Remove Duplicates from Sorted Array II
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 array, you must do this in place with cons原创 2017-02-23 19:21:40 · 225 阅读 · 0 评论 -
162. 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 may contain multiple peaks, in that case原创 2017-02-23 19:27:36 · 280 阅读 · 0 评论 -
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 two lin原创 2017-02-23 19:14:56 · 213 阅读 · 0 评论 -
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] ] 方法一、vector<vector<int> > gene原创 2017-02-23 18:35:03 · 186 阅读 · 0 评论 -
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 digits are st原创 2017-02-23 18:31:50 · 384 阅读 · 0 评论 -
48. Rotate Image--重点看思路三
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?着重注意思路三【题意】给定一个n*n个2维矩阵来表示一个图。在原矩阵上旋转图形90°。原创 2017-02-23 17:47:56 · 1258 阅读 · 0 评论 -
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 constant memory.The order原创 2017-02-23 16:58:06 · 298 阅读 · 0 评论 -
380. Insert Delete GetRandom O(1)--构造新类的题目
Design a data structure that supports all following operations in average O(1) time.insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if原创 2017-02-23 16:45:32 · 314 阅读 · 0 评论 -
189. Rotate Array leetcode(array)
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 solution原创 2016-11-27 17:37:17 · 286 阅读 · 0 评论 -
75. Sort Colors--荷兰三色国旗问题
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1,原创 2017-02-23 18:55:48 · 506 阅读 · 0 评论 -
169. Majority Element--寻找数组中出现次数超过一半的数据,229. Majority Element II--注意最后的检测
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 majority element always原创 2017-02-17 10:20:38 · 394 阅读 · 0 评论 -
16. 3Sum Closest leetcode(array)
3Sum Closest For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 同15的3sum类似int threeSumClosest(vector<int>& nums,...原创 2016-11-28 18:21:08 · 275 阅读 · 0 评论 -
349. Intersection of Two Array leetcode (hash table)
Intersection of Two Arrays Add to List QuestionEditorial Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in ...原创 2016-11-27 19:48:19 · 231 阅读 · 0 评论 -
15.3sum leetcode(array)
3Sum Add to List QuestionEditorial Solution 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 zer...原创 2016-11-27 19:07:14 · 177 阅读 · 0 评论 -
1.Two Sum leetcode(array)
Two Sum Add to List QuestionEditorial Solution My 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 h...原创 2016-11-27 18:38:03 · 301 阅读 · 0 评论 -
442. Find All Duplicates in an Array (array)
Find All Duplicates in an Array Add to List QuestionEditorial Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elemen...原创 2016-11-27 14:12:10 · 229 阅读 · 0 评论 -
leetcode 448Find All Numbers Disappeared in an Array (array)
Find All Numbers Disappeared in an Array Add to List 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 ...原创 2016-11-27 11:47:44 · 880 阅读 · 0 评论 -
leetcode 119 Pascal's Triangle II(array)
Pascal’s Triangle II Add to List QuestionEditorial Solution My 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 opti...原创 2016-11-26 22:51:15 · 332 阅读 · 0 评论 -
leetcode 118 Pascal's Triangle(array)
leetcode array 118. Pascal’s Triangle Add to List QuestionEditorial Given numRows, generate the first numRows of Pascal’s triangle. For example, given numRows = 5, Return [ [1], [1,...原创 2016-11-26 22:09:13 · 325 阅读 · 0 评论 -
62. Unique Paths 和63. Unique Paths II
一、62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is try原创 2017-02-17 17:28:29 · 371 阅读 · 0 评论 -
64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any原创 2017-02-23 16:02:03 · 370 阅读 · 0 评论 -
78. Subsets ,90. Subsets II(待研究)---位运算法(重要和Combination Sum一系列的题目类似)
第一题、78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ ...原创 2017-02-23 15:35:21 · 377 阅读 · 0 评论 -
Combination Sum系列的三个题目39,40,216--重要(和78. Subsets ,90. Subsets II类似)
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 repeated number ma原创 2017-02-17 13:08:30 · 407 阅读 · 0 评论 -
73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place 方法一: 本算法的时间复杂度为O(m*n),空间复杂度为:O(m+n) 思想:首先遍历一边数组将数组中出现0的那一行以及那一列出现了的行号和列号记录在record(record初始化为0)中;再依次遍历一边re原创 2017-02-28 16:22:03 · 404 阅读 · 0 评论 -
152. Maximum Product Subarray Add to List --- 注意
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest produ转载 2017-02-18 11:08:07 · 561 阅读 · 0 评论 -
53. Maximum Subarray Add to List
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], the contiguous subarray [4,-1,2,1] has the原创 2017-02-18 10:52:06 · 542 阅读 · 0 评论 -
发现一个数组中重复的数字,448和287的总结 ---重要
区别一、448题目没有限制不能改变原来的数组,所以采用的是交换机制,不断的将错误位置上的数据不断的交换移动到本该属于其的位置上去,则重新遍历时,nums[i]!=i+1的数字即没有出现的数字,但287题目则限制了不能改变原来的数组,所以不能采用交换机制的方法,方法类似采用查找循环链表的环入口的方法,或者可以采用利用二分查找的思想; 区别二、448题目中1-n之间重复的数字只出现了两次,但是在287原创 2017-02-17 13:39:34 · 901 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array and 442 Find All Duplicates 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] inclusive that do not appear in this array.Could you原创 2017-02-13 15:33:23 · 239 阅读 · 0 评论 -
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.For example, Given nums = [0, 1, 3] return 2.Note: Your algorithm should run in line原创 2017-02-17 11:54:44 · 223 阅读 · 0 评论 -
219. Contains Duplicate II---数组中两个重复的数字的下标最多相差k
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 between i and j is at most k.原创 2017-02-17 10:46:31 · 679 阅读 · 0 评论 -
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 + n) to hold additional原创 2017-02-28 18:20:36 · 222 阅读 · 0 评论 -
1. Two Sum,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 should return indices of the two numbers suc原创 2017-02-13 18:07:01 · 306 阅读 · 0 评论 -
54. Spiral Matrix (有待进一步研究)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You sh原创 2017-02-23 13:10:01 · 297 阅读 · 0 评论 -
59. Spiral Matrix II(待进一步研究)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example, Given n = 3,You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7,原创 2017-02-23 11:15:32 · 302 阅读 · 0 评论 -
238. Product of Array Except Self (后两种方法有待进一步研究)
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For e原创 2017-02-13 18:24:17 · 264 阅读 · 0 评论 -
283. Move Zeroes
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your funct原创 2017-02-13 18:02:22 · 206 阅读 · 0 评论 -
495. Teemo Attacking
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time原创 2017-02-13 15:51:59 · 525 阅读 · 0 评论 -
485. Max Consecutive Ones leetcode array
Given a binary array, find the maximum number of consecutive 1s in this array.Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.原创 2017-02-13 15:13:50 · 241 阅读 · 0 评论 -
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转载 2017-02-28 19:05:42 · 168 阅读 · 0 评论