数组常见面试题
- 数组中重复的数字
- 寻找重复数
- 删除有序数组中的重复项I(元素只出现一次)
- 删除有序数组中的重复项 II(元素最多出现两次)
- 连续子数组的最大和
- 乘积最大子数组(最大连续数子数组的积)
- 构建乘积数组
- 把数组排成最小的数
- 把数组排成最大的数(最大数)
- 最小的k个数
- 数组中第K大的数字
- 两数之和
- 三数之和
- 最长无重复子数组
- 最长重复子数组
- 合并两个有序数组(合并排序数组)
- 合并区间
- 顺时针打印矩阵
- 螺旋矩阵
- 螺旋矩阵II
- 菲波那切数列
- 最长斐波那契数列
- 跳台阶
- 跳台阶扩展问题
- 调整数组顺序使奇数位于偶数前面(顺序可以改变)
- 调整数组顺序使奇数位于偶数前面(顺序不可以改变)
- 数组的奇偶排序
- 最小矩阵之和(矩阵的最小路径和)
- 礼物的最大价值(矩阵的最大值)
- 重建二叉树
- 在两个长度相等的排序数组中找到上中位数
- 数组中的逆序对
- 旋转数组(向后移动k个元素)
- 顺时针旋转矩阵(旋转90°)
- 0~n-1中缺失的数字(二分)
- 缺失的第一个正整数(未排序数组)
- 数组中数字出现的次数I
- 数组中数字出现的次数 II
- 数组的奇偶排序(当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数)
- 数组中出现次数超过一半的数字
- 数组中有些元素出现两次而其他元素出现一次。找到所有出现两次的元素。
- 数组中两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字
- 在一个数组 nums 中除一个数字只出现一次之外,其他数字都出现了三次。请找出那个只出现一次的数字
- 下一个排列
- 最长连续序列
数组中重复的数字
https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
空间复杂度O(n)
class Solution {
public int findRepeatNumber(int[] nums) {
if(nums == null || nums.length == 0) {
return -1;
}
HashSet<Integer> set = new HashSet<>();
for(int num : nums){
if(set.contains(num)) {
return num;
}
set.add(num);
}
return -1;
}
}
空间复杂度O(1)
class Solution {
public int findRepeatNumber(int[] nums) {
if(nums == null || nums.length == 0) {
return -1;
}
for(int i = 0; i < nums.length; i++) {
for(int j = i + 1; j < nums.length; j++ ) {
if(nums[i] == nums[j]) {
return nums[i];
}
}
}
return -1;
}
}
寻找重复数
https://leetcode-cn.com/problems/find-the-duplicate-number/
二分
class Solution {
public int findDuplicate(int[] nums) {
if(nums == null || nums.length == 0) return -1;
int n = nums.length - 1;
int l = 0, r = n;
while (l < r) {
int mid = l + r >> 1;
int cnt = 0;
for (int num : nums) {
if (num <= mid) {
cnt += 1;
}
}
if (cnt > mid) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
}
快慢指针
class Solution {
public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
slow = nums[slow];
fast = nums[nums[fast]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
int pre1 = 0;
int pre2 = slow;
while(pre1 != pre2){
pre1 = nums[pre1];
pre2 = nums[pre2];
}
return pre1;
}
}
删除有序数组中的重复项I(元素只出现一次)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0) {
return -1;
}
int i = 0;
int j = 1;
while(j < nums.length) {
if(nums[j] != nums[i]) {
nums[i + 1] = nums[j];
i++;
}
j++;
}
return i + 1;
}
}
删除有序数组中的重复项 II(元素最多出现两次)
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <= 2) return nums.length;
int index = 2;
for(int i = 2; i < nums.length; i++){
if(nums[i] != nums[index-2])
nums[index++] = nums[i];
}
return index;
}
}
连续子数组的最大和
https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/
class Solution {
public int maxSubArray(int[] nums) {
if(nums == null || nums.length == 0) {
return -1;
}
int ret = nums[0];
for(int i = 1; i < nums.length; i++) {
nums[i] += Math.max(nums[i - 1], 0);
ret = Math.max(ret, nums[i]);
}
return ret;
}
}
乘积最大子数组(最大连续数子数组的积)
https://leetcode-cn.com/problems/maximum-product-subarray/
class Solution {
public int maxProduct(int[] nums) {
int ret= Integer.MIN_VALUE, imax = 1, imin = 1; //一个保存最大的,一个保存最小的。
for(int i = 0; i < nums.length; i++){
if(nums[i] < 0){
int tmp = imax;
imax = imin;
imin = tmp;
}
//如果数组的数是负数,那么会导致最大的变最小的,最小的变最大的。因此交换两个的值。
imax = Math.max(imax*nums[i], nums[i]);
imin = Math.min(imin*nums[i], nums[i]);
ret= Math.max(ret, imax);
}
return ret;
}
}
构建乘积数组
https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/
class Solution {
public int[] constructArr(int[]nums) {
if(nums == null || nums.length == 0) {
return new int[0];
}
int[] ret = new int[nums.length];
for(int i = 0, j = 1; i < nums.length; i++) {
// 先乘左边的数(不包括自己)
ret[i] = j;
j *= nums[i];
}
for(int i = nums.length - 1, j = 1; i >= 0; i--) {
// 再乘右边的数(不包括自己)
ret[i] *= j;
j *= nums[i];
}
return ret;
}
}
把数组排成最小的数
https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
class Solution {
public String minNumber(int[] nums) {
String[] strs = new String[nums.length];
for(int i = 0; i < nums.length; i++) {
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, (x, y)-> (x + y).compareTo(y + x));
StringBuffer ret = new StringBuffer();
for(String s : strs) {
ret.append(s);
}
return ret.toString();
}
}
把数组排成最大的数(最大数)
import java.util.*;
public class Solution {
public String solve (int[] nums) {
String[] strs = new String[nums.length];
for(int i = 0; i < nums.length; i++){
strs[i] = String.valueOf(nums[i]);
}
Arrays.sort(strs, (x, y)-> (y + x).compareTo(x + y));
// Arrays.sort(strs, new Comparator<String>(){
// public int compare(String a, String b){
// return (b + a).compareTo(a + b);
// }
// });
if(strs[0].equals("0")) {
return "0";
}
StringBuilder ret = new StringBuilder();
for(int i = 0; i < nums.length; i++){
ret.append(strs[i]);
}
return ret.toString();
}
}
最小的k个数
https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if(arr == null || arr.length == 0 || k == 0) {
return new int[0];
}
PriorityQueue<Integer> bigHeap = new PriorityQueue<>((o1, o2)->(o2 - o1));
for(int num : arr) {
if(bigHeap.size() < k) {
bigHeap.offer(num);
}else if(bigHeap.peek() > num) {
bigHeap.poll();
bigHeap.offer(num);
}
}
int[] ret = new int[k];
int index = 0;
while(!bigHeap.isEmpty()) {
ret[index++] = bigHeap.poll();
}
return ret;
}
}
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if (k >= arr.length) return arr;
return quickSort(arr, k, 0, arr.length - 1);
}
private int[] quickSort(int[] arr, int k, int l, int r) {
int i = l, j = r;
while (i < j) {
while (i < j && arr[j] >= arr[l]) j--;
while (i < j && arr[i] <= arr[l]) i++;
swap(arr, i, j);
}
swap(arr, i, l);
if (i > k) return quickSort(arr, k, l, i - 1);
if (i < k) return quickSort(arr, k, i + 1, r);
return Arrays.copyOf(arr, k);
}
private void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
数组中第K大的数字
https://leetcode-cn.com/problems/xx4gT2/
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<