您的位置:首页 > 产品设计 > UI/UE

LeetCode Top Interview Questions Easy Collections 总结一(Array)

2018-03-14 16:33 609 查看

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 another array, you must do this by modifying the input array in-place with O(1) extra memory.

要求: 不建新数组, O(1) 额外空间, 给一个有序数组, 要求我们将不重复的数从头依次放置, 并返回去重后的数组长度

分析: 既然有序, 就让1开始的每一位和前面一位的数比较就可以,重复的再做处理

可以设置一个索引, 表示新数组(在原数组之上的索引), 将上面对比的不重复的数取出放到索引出, 让索引加一即可完成新数组, 这样破坏了原有数组的数据, 但是很好的完成题目要求

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 complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

要求: 给一个数组, 保存一个商品每天的价格, 要求我们倒买倒卖, 看最多能赚多少钱

分析: 直接遍历一躺, 用sum 累加 后面只要比前面大的值即可

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].

要求: 长度为n的数组右移k部, 另外还有一个hint 这个可以inplace, O(1)extra space,

分析: 这个应该很经典了, 我一开始的想法是 反转整个数组, 然后反转 1-k, 再反转k+1到n-1, 显然不够好

个人觉得最好的方法下面两种

1. 利用状态方程 i → (i + k) % n, 一步到位, 个人觉得这个方法和约瑟夫环类似

2. 将设f (k, n) 的意义为 将 后k位 与 前 k 位 交换位置(即0 - k, 1- k-1….),

那么题目要求就可以 f(k, n) 再 f(n-k, k) 如此反复, 直到n <= 1即可

Contains Duplicate

Given an array of in
4000
tegers, 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 false if every element is distinct.

要求: 有重复就返回false, 没有就返回true

分析: hashmap, 或者排序后遍历一遍

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

要求: 给一个数组, 里面只有1个数只出现了一次

分析: 不太常用位运算以至于一开始还排序然后找重, 最简单方法用 ^ ;

Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example:

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:

Each element in the result should appear as many times as it shows in both arrays.

The result can be in any order.

要求: 找两个数组的公共数据

分析: hashmap存入一个, 检索另一个…

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 stored such that the most significant digit is at the head of the list.

要求: 给一个数组代表一个大数字, 给他加1

分析: 不要死脑筋给他加1就行…是9就置0, 其余数+1, 然后直接返回, 若全是9就做一个新数组

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 function, nums should be [1, 3, 12, 0, 0].

Note:

You must do this in-place without making a copy of the array.

Minimize the total number of operations.

要求: 给一个数组, 移动0 到它的末尾, in-place , 不新建数组

分析: 和第一题Remove Duplicates from Sorted Array 类似, 设置一个索引, 从头遍历, 不为0 的数和索引交换, 索引加一

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 the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,

return [0, 1].

要求: 在数组中找所有 2个数加起来等于 target 的组合并返回, 同一个元素只能使用一个, 有且只有一个解

分析: 都限制了有一个解, hashmap 存target- nums[i], 遍历一次就找到了

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.

A partially filled sudoku which is valid.

Note:

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

要求: 给一个二位数组代表数独, 判断这个数独谜题是否成立

分析: 仔细读题是关键, 看看下面那个Note, 不需要判断这个数度有没有解!!!! 那就简单了, 空间还时间还是时间换空间的问题, 多建几个hashmap 还是多几次for 循环的选择

Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

要求; 给一个二维数组代表一个矩阵, 将他旋转90°, 返回新数组

分析: 我的想法是, 将其对称后, 上下翻转 即可, 应为对应关系简单, AC了之后, 看了看别人的思路

将这个矩阵分成4个等边三角形, 从对角线开始每边分一个小的等边三角形, 这样可以完成映射



temp = matrix[i][j];
matrix[i][j] = matrix[len-j-1][i];
matrix[len-j-1][i] = matrix[len-i-1][len-j-1];
matrix[len-i-1][len-j-1] = matrix[j][len-i-1];
matrix[j][len-i-1] = temp;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode Array