LeetCode-陣列與矩陣總結
阿新 • • 發佈:2018-11-27
陣列與矩陣
把陣列中的 0 移到末尾
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] 之間,其中一個數被替換為另一個數,找出重複的數和丟失的數
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;
}
}
對角元素相等的矩陣
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;
}
}
巢狀陣列
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;
}
}