LeetCode—数组与矩阵
T448 找到所有数组中小时的数字
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
for(int num : nums){
int x = (num - 1) % n;
nums[x] += n;
}
List<Integer> out = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
if(nums[i] <= n){
out.add(i+1);
}
}
return out;
}
}
T48 旋转图像
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < (n + 1) / 2; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
matrix[j][n - i - 1] = temp;
}
}
}
}
T240 搜索二维矩阵
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int m = matrix.length;
int n = matrix[0].length;
int i = 0;
int j = n-1;
while(i < m && j >= 0){
if(target > matrix[i][j]){
i++;
}
else if(target < matrix[i][j]){
j--;
}
else{
return true;
}
}
return false;
}
}
T769 最多能完成排序的块
class Solution {
public int maxChunksToSorted(int[] arr) {
int ans = 0;
int max = 0;
for(int i = 0; i < arr.length; i++){
max = Math.max(max, arr[i]);
if(max == i){
ans++;
}
}
return ans;
}
}
1、把数组中的 0 移到末尾
T283 Move Zeroes (Easy)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
for(int i=0,j=0; i<nums.size(); ++i){
if(nums[i])
swap(nums[i],nums[j++]);
}
}
};
2、改变矩阵维度
T566 Reshape the Matrix (Easy)
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m=nums.size();
int n=nums[0].size();
if(m*n != r*c) return nums;
vector<vector<int>> res(r, vector<int>(c));
for(int i=0; i<r; ++i){
for(int j=0; j<c; ++j){
int k=i*c+j;
res[i][j] = nums[k/n][k%n];
}
}
return res;
}
};
3、找出数组中最长的连续 1
T485 Max Consecutive Ones (Easy)
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count = 0;
int res = 0;
for(int num :nums){
if(num == 0){
count = 0;
}
else count = count + 1;
res = max(res, count);
}
return res;
}
};
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = 0;
int count = 0;
for(int i = 0; i < nums.size(); ++i)
{
if(nums[i] == 1)
{
res++;
}
else if(nums[i] == 0)
{
res = 0;
}
count = max(count, res);
}
return count;
}
};
5、有序矩阵的 Kth Element
T378. Kth Smallest Element in a Sorted Matrix ((Medium))
class Solution {
public:
int kthSmallest(vector<vector<int>>& matrix, int k) {
priority_queue<int> q;
for(int i=0; i<matrix.size(); ++i){
for(int j=0; j<matrix[i].size(); ++j){
q.emplace(matrix[i][j]);
if(q.size() > k) q.pop();
}
}
return q.top();
}
};
6、一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数
T645. Set Mismatch (Easy)
class Solution {
public:
vector<int> findErrorNums(vector<int>& nums) {
vector<int> res(2,0);
vector<int> cnt(nums.size(),0);
for(int num : nums) ++cnt[num-1];
for(int i=0; i<cnt.size(); ++i){
if(res[0]!=0 && res[1]!=0) return res;
if(cnt[i]==2) res[0]=i+1;
else if(cnt[i]==0) res[1]=i+1;
}
return res;
}
};
7、找出数组中重复的数,数组值在 [1, n] 之间
T287. Find the Duplicate Number (Medium)
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int left = 1;
int right = nums.size();
while(left<right){
int mid=left+(right-left)/2;
int cnt=0;
for(int num : nums){
if(num<=mid) ++cnt;
}
if(cnt<=mid) left=mid+1;
else right=mid;
}
return right;
}
};
8、数组相邻差值的个数
T667. Beautiful Arrangement II (Medium)
class Solution {
public:
vector<int> constructArray(int n, int k) {
vector<int> res;
int i=1;
int j=n;
while(i<=j){
if(k>1) res.push_back(k--%2 ? i++ : j--);
else res.push_back(i++);
}
return res;
}
};
9、数组的度
T697. Degree of an Array (Easy)
class Solution {
public:
int findShortestSubArray(vector<int>& nums) {
int n = nums.size();
int res = INT_MAX;
int degree = 0;
unordered_map<int, int> m;
unordered_map<int, pair<int, int>> pos;
for(int i=0; i<n; ++i){
if(++m[nums[i]] == 1){
pos[nums[i]] = {i, i};
}
else{
pos[nums[i]].second = i;
}
degree = max(degree, m[nums[i]]);
}
for(auto a : m){
if(degree == a.second){
res = min(res, pos[a.first].second - pos[a.first].first+1);
}
}
return res;
}
};
10、对角元素相等的矩阵
T766. Toeplitz Matrix (Easy)
class Solution {
public:
bool isToeplitzMatrix(vector<vector<int>>& matrix) {
for(int i=0; i<matrix.size()-1; ++i){
for(int j=0; j<matrix[i].size()-1; ++j){
if(matrix[i][j] != matrix[i+1][j+1]) return false;
}
}
return true;
}
};
11、嵌套数组
T565. Array Nesting (Medium)
class Solution {
public:
int arrayNesting(vector<int>& nums) {
int n=nums.size();
int res=INT_MIN;
vector<bool> visited(n,false);
for(int i=0; i<n; ++i){
if(visited[nums[i]]) continue;
res = max(res, helper(nums, i, visited));
}
return res;
}
int helper(vector<int>& nums, int start, vector<bool>& visited){
int i=start;
int cnt=0;
while(cnt==0 || i!=start){
visited[i] = true;
i=nums[i];
++cnt;
}
return cnt;
}
};