LeetCode-数组与矩阵总结

数组与矩阵

把数组中的 0 移到末尾

283. Move Zeroes (Easy)

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
class Solution {
    public void moveZeroes(int[] nums) {
        if (null == nums || 0 == nums.length) {
            return ;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                int index = i;
                while (index - 1 >= 0 && nums[index - 1] == 0) {
                    index--;
                }
                if (index != i) {
                    nums[index] = nums[i];
                    nums[i] = 0;
                }
            }
        }
    }
}

有序矩阵查找

240. Search a 2D Matrix II (Medium)

[
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
]
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (null == matrix || 0 == matrix.length) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        int row = 0;
        int col = n - 1;
        while (row < m && col >= 0) {
            if (target == matrix[row][col]) {
                return true;
            } else if (target < matrix[row][col]) {
                col--;
            } else {
                row++;
            }
        }
        return false;
    }
}

有序矩阵的 Kth Element

378. Kth Smallest Element in a Sorted Matrix ((Medium))

matrix = [
  [ 1,  5,  9],
  [10, 11, 13],
  [12, 13, 15]
],
k = 8,

return 13.
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        if (null == matrix || 0 == matrix.length) {
            return 0;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                queue.offer(matrix[i][j]);
            }
        }
        int count = 0;
        int cur = 0;
        int pre = Integer.MAX_VALUE;
        while (!queue.isEmpty() && count < k) {
            cur = queue.poll();
            count++;
        }
        return cur;
    }
}

一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数

645. Set Mismatch (Easy)

Input: nums = [1,2,2,4]
Output: [2,3]
Input: nums = [1,2,2,4]
Output: [2,3]

最直接的方法是先对数组进行排序,这种方法时间复杂度为 O(NlogN)。本题可以以 O(N) 的时间复杂度、O(1) 空间复杂度来求解。

主要思想是通过交换数组元素,使得数组上的元素在正确的位置上。

class Solution {
    public int[] findErrorNums(int[] nums) {
        if (null == nums) {
            return new int[2];
        }
        int[] flag = new int[nums.length + 1];
        int twice = 0, miss = 0;
        for (int i = 0; i < nums.length; i++) {
            flag[nums[i]]++;
            if (flag[nums[i]] > 1) {
                twice = nums[i];
            }
        }
        for (int i = 1; i < nums.length + 1; i++) {
            if (flag[i] == 0) {
                miss = i;
            }
        }
        return new int[]{twice, miss};
    }
}

找出数组中重复的数,数组值在 [1, n] 之间

287. Find the Duplicate Number (Medium)

要求不能修改数组,也不能使用额外的空间。

二分查找解法:

class Solution {
    public int findDuplicate(int[] nums) {
        if (null == nums || 0 == nums.length) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]);
            if (nums[index - 1] > 0) {
                nums[index - 1] = - nums[index - 1];
            } else {
                result = index;
                break;
            }
        }
        return result;
    }
}

数组的度

697. Degree of an Array (Easy)

Input: [1,2,2,3,1,4,2]
Output: 6

题目描述:数组的度定义为元素出现的最高频率,例如上面的数组度为 3。要求找到一个最小的子数组,这个子数组的度和原数组一样。

class Solution {
    public int findShortestSubArray(int[] nums) {
        if (null == nums || 0 == nums.length) {
            return 0;
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                int value = map.get(nums[i]);
                map.put(nums[i], value + 1);
            } else {
                map.put(nums[i], 1);
            }
        }
        int max = Integer.MIN_VALUE;
        for (int i : map.keySet()) {
            max = Math.max(max, map.get(i));
        }
        int min = Integer.MAX_VALUE;
        for (int i : map.keySet()) {
            if (map.get(i) == max) {
                int number = i;
                int left = 0, right = nums.length - 1;
                while (left < right && nums[left] != number) left++;
                while (left < right && nums[right] != number) right--;
                min = Math.min(min, right - left + 1);
            }
        }
        return min;
    }
}
class Number{
    int number = 0;
    int count = 0;
    public Number(int number, int count) {
        this.number = number;
        this.count = count;
    }
}

对角元素相等的矩阵

766. Toeplitz Matrix (Easy)

1 2 3 4
5 1 2 3
9 5 1 2

In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]", and in each diagonal all elements are the same, so the answer is True.
class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        if (matrix == null) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;
        for (int i = 0; i < m; i++) {
            if (i == m - 1) {
                break;
            }
            for (int j = 0; j < n ; j++) {
                int gap = i - j;
                int ii = i;
                while (ii < m && ii - gap < n) {
                    if (matrix[ii][ii - gap] != matrix[i][j]) {
                        return false;
                    }
                    ii++;
                }
            }
        }
        return true;
    }
}

嵌套数组

565. Array Nesting (Medium)

Input: A = [5,4,0,3,1,6,2]
Output: 4
Explanation:
A[0] = 5, A[1] = 4, A[2] = 0, A[3] = 3, A[4] = 1, A[5] = 6, A[6] = 2.

One of the longest S[K]:
S[0] = {A[0], A[5], A[6], A[2]} = {5, 6, 2, 0}

题目描述:S[i] 表示一个集合,集合的第一个元素是 A[i],第二个元素是 A[A[i]],如此嵌套下去。求最大的 S[i]。

class Solution {
    public int arrayNesting(int[] nums) {
        if (null == nums || 0 == nums.length) {
            return 0;
        }
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            int count = 0;
            int index = i;
            while (nums[index] != -1) {
                count++;
                int temp = nums[index];
                nums[index] = -1;
                index = temp;
            }
            max = Math.max(max, count);
        }
        return max;
    }
}

分隔数组

769. Max Chunks To Make Sorted (Medium)

Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

题目描述:分隔数组,使得对每个子部分排序后整个数组就为有序。

class Solution {
    public int maxChunksToSorted(int[] arr) {
        if (null == arr || 0 == arr.length) {
            return 0;
        }
        int num = 0;
        int index = 0;
        for (int i = 0; i < arr.length; i++) {
            index = Math.max(index, arr[i]);
            if (index == i) {
                num++;
            }
        }
        return num;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值