LeetCode 1329. Sort the Matrix Diagonally

1. 算法描述

Given a m * n matrix mat of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array.
给定 m * n 的整型矩阵mat, 按照左上到右下的顺序,升序排列对接线元素。

举例:
在这里插入图片描述
Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]

2. 思想分析

(1)遍历矩阵***第一行***元素,取出该对角线上的所有元素并且记录每个元素所对应的数组行列坐标[row, col];
(2)每一个元素row index + 1, col index + 1, 就可以得到下一个对角线上元素的[row, col];
(3)将[row, col]作为key, 对应的数值为value,存在一个map中;
(4)同时保存这些对应的数值,然后进行排序;
(5)遍历map, 取值,并且更新mat对应的[row, col]的元素;
(6)处理***第一列***剩余元素。

3. 参考代码

这里不讨论算法复杂度和代码风格,只是一份提交成功并且容易理解的参考代码

class Solution {
public:
    vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
        map<pair<int, int>, int> idxValMap;
        const size_t row = mat.size();
        const size_t col = mat[0].size();  
  
        // handle first row
        for(int i = 0; i < col; i++)
        {
            int firstRow = 0;
            int tmpCol = i;
            vector<int> diagonalData;
            idxValMap.clear();
            
            while(firstRow < row || tmpCol < col)
            {
                pair<int, int> pairIdx = make_pair(firstRow, tmpCol);
                idxValMap[pairIdx] = mat[firstRow][tmpCol];
                diagonalData.push_back(mat[firstRow][tmpCol]);
                firstRow++;
                tmpCol++;
                
                if(firstRow == row || tmpCol == col) break;
            }
           
            sort(diagonalData.begin(), diagonalData.end());
            
            int start = 0;
            for(auto & m : idxValMap)
            {
                m.second = diagonalData[start];
                mat[m.first.first][m.first.second] = m.second;
                start++;
            }
        }
        
        // handle first col, excludes the first one in 1st column
        for(int j = 1; j < row; j++)
        {
            int firstCol = 0;
            int tmpRow = j;
            vector<int> diagonalData;
            idxValMap.clear();
            
            while(firstCol < col || j < row)
            {
                pair<int, int> pairIdx = make_pair(tmpRow, firstCol);
                idxValMap[pairIdx] = mat[tmpRow][firstCol];
                diagonalData.push_back(mat[tmpRow][firstCol]);
                firstCol++;
                tmpRow++;
                
                if(firstCol == col || tmpRow == row) break;
            }
            
            sort(diagonalData.begin(), diagonalData.end());
            
            int start = 0;
            for(auto & m : idxValMap)
            {
                m.second = diagonalData[start];
                mat[m.first.first][m.first.second] = m.second;
                start++;
            }
        }
        
        return mat;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值