array
李白-2017
今天经历的所有惊心动魄,都不过是明天的过眼云烟。
展开
-
1. Two Sum
从数组中找到两个数字和等于目标值的一对数字,返回它们在数组中的下标。难度 【easy】Given an array of integers, returnindicesof the two numbers such that they add up to a specific target.You may assume that each input would haveexac...原创 2019-03-23 23:39:46 · 237 阅读 · 0 评论 -
238. Product of Array Except Self
除了当前元素之外的元素相乘。要求:不能用除法难度:【medium】for ex.Input: [1,2,3,4]Output: [24,12,8,6]// 24 = 2*3*4// 12 = 1*3*4// 8 = 1*2*4// 6 = 1*2*3class Solution {public: vector<int> productEx...原创 2019-04-07 16:48:51 · 132 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II 股票最佳买入和卖出时间
允许股票买入和卖出多次,并且要求只能在卖出后,才能买入。这道题和121. Best Time to Buy and Sell Stock 股票最佳买入和卖出时间不同,121这道题相对比较简单,只要求,买卖一次。而这道题可以买卖多次,得到最大的收益。例如:Input: [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price =...原创 2019-03-31 18:24:18 · 217 阅读 · 0 评论 -
268. Missing Number 查找缺少的元素
数组中的元素正常是0,1,2,n。 数组中元素有n-1个元素,查找缺少的那个元素。例如:Input: [9,6,4,2,3,5,7,0,1]Output: 8 (数组中缺少的元素是8)难度:【easy】这道题我想出了三种方法:第一种方法:对原数组进行排序,之后对比下标和该下标对应的元素值,如果不同,那么下标就是缺少的元素。class Solution {public...原创 2019-03-31 17:49:41 · 148 阅读 · 0 评论 -
217. Contains Duplicate 判断数组中是否有重复元素
例如:Input: [1,2,3,1]Output: true (有重复元素1)方法1: 对原数组进行排序,再遍历数组,比较相邻两个元素是否相等。time: O(nlogn), space:O(1)方法2:创建一个哈希表,遍历数组统计每个元素出现次数。time: O(n), space:O(n)下面是方法1的解:class Solution {public: bo...原创 2019-03-31 17:36:29 · 175 阅读 · 0 评论 -
189. Rotate Array 旋转数组元素
难度:【easy】按照指定次数k旋转数组,每次将最后一个元素挪到最前面。这里有个陷阱,比如数组的总长度是3,指定次数为7,那么需要移动的次数就是7%3=1次。如果指定次数是6,那么6%3=0,就不用挪动元素。class Solution {public: void RotateArray(vector<int> &nums, int begin, i...原创 2019-03-31 17:32:50 · 193 阅读 · 0 评论 -
169. Majority Element 求数组中满足出现次数的元素
当数组中有奇数个元素时,出现次数 大于等于 n/2+1次。当数组中有偶数个元素时,出现次数 大于等于 n/2次。难度:【easy】思路:常规的方法,第一种方法:首先先用一个hash表统计出每个元素出现的次数,再遍历一遍hasp表找到满足出现次数的元素。这样做的时间复杂度O(n),空间复杂度O(n).第二种方法:对原有数组进行排序,取下标为n/2的元素,即为所求。...原创 2019-03-31 17:26:39 · 128 阅读 · 0 评论 -
283. Move Zeroes 将数组中值为0的元素移动到后面
例如:Input: [0,1,0,3,12]Output: [1,3,12,0,0]要求:1. 就地移动,不借助额外空间。2. 最小化操作次数。难度:【easy】思路:遍历一次数组,将不为0的元素向前移动。使用双指针解决。注意事项:非0元素的相对顺序不能改变。时间复杂度:O(n)空间复杂度:O(1)方法1:class Solution {...原创 2019-03-31 16:49:03 · 407 阅读 · 0 评论 -
leetcode array数组题目整理 (持续更新中。。。)
持续更新中。。。【leetcode Top Interview Questions】, 【array】分类下面: easy 13道,medium 22道(lock 2),hard 5道 (2019.03.31)【easy】(13道题)1.Two Sum26. Remove Duplicates from Sorted Array 对排序的数组删除重复的元素53. Maxi...原创 2019-03-31 18:24:51 · 852 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock 股票最佳买入和卖出时间
Input: [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price....原创 2019-03-30 23:38:51 · 268 阅读 · 0 评论 -
118. Pascal's Triangle 打印杨辉三角
难度:【easy】class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> rows; for (int i = 0; i < numRows; ++i) { ...原创 2019-03-30 22:10:41 · 144 阅读 · 0 评论 -
88. Merge Sorted Array 合并两个排序数组
两个排序的数组nums1和nums2.。进行排序合并到nums1, nums1有足够的空间。难度:【easy】思路:对两个数组倒序比较,因为如果按照正序进行比较,nums2的元素需要往nums1里面插入时,nums1中对应位置及其后面的所有元素就需要向后面移动,所以这里选择倒序排序比较好。时间复杂度O(m+n)class Solution {public: void...原创 2019-03-30 21:55:41 · 229 阅读 · 0 评论 -
66. Plus One 把数组看成一个数值进行加1
例如:Input: [1,2,3]Output: [1,2,4]Explanation: The array represents the integer 123.难度:【easy】把[1,2,3]看着数值123,对它加1,结果就是124.需要考虑进位情况。class Solution {public: vector<int> plusOne(ve...原创 2019-03-30 21:31:54 · 166 阅读 · 0 评论 -
53. Maximum Subarray 寻找sum最大的子数组
Given an integer arraynums, find the contiguous subarray(containing at least one number) which has the largest sum and return its sum.例子:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation: [...原创 2019-03-30 18:43:30 · 277 阅读 · 0 评论 -
27. Remove Element 删除数组中指定值的元素
难度:【easy】class Solution {public: int removeElement(vector<int>& nums, int val) { int i = 0; for (int j = 0; j < nums.size(); ++j) { if (nums[j] != val)...原创 2019-03-30 17:49:41 · 463 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array 对排序的数组删除重复的元素
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this bymodifyi...原创 2019-03-30 17:13:53 · 225 阅读 · 0 评论 -
78. Subsets 求数组的子数组
难度:【medium】for ex. 输出的子数组的顺序没有限制。Input: nums = [1,2,3]Output:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]主要由两种方法:方法1:深度优先搜索 vector<vector<int>> subs...原创 2019-04-07 17:07:51 · 515 阅读 · 0 评论