carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode766:托普利茨矩阵(toeplitz_matrix_766) #130

Open carloscn opened 1 year ago

carloscn commented 1 year ago

问题分析

给你一个 m x n 的矩阵 matrix 。如果这个矩阵是托普利茨矩阵,返回 true ;否则,返回 false 。

如果矩阵上每一条由左上到右下的对角线上的元素都相同,那么这个矩阵是 托普利茨矩阵 。

示例 1: image

输入:matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] 输出:true 解释: 在上述矩阵中, 其对角线为: "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。 各条对角线上的所有元素均相同, 因此答案是 True 。

示例 2:

image

输入:matrix = [[1,2],[2,2]] 输出:false 解释: 对角线 "[1, 2]" 上的元素不同。  

提示:

m == matrix.length n == matrix[i].length 1 <= m, n <= 20 0 <= matrix[i][j] <= 99   进阶:

如果矩阵存储在磁盘上,并且内存有限,以至于一次最多只能将矩阵的一行加载到内存中,该怎么办? 如果矩阵太大,以至于一次只能将不完整的一行加载到内存中,该怎么办?

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/toeplitz-matrix 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

carloscn commented 1 year ago

问题分析

C语言版本

static int32_t toeplitz_martrix(const int64_t *array, size_t width, size_t depth, bool *result)
{
    int32_t ret = 0;
    size_t i = 0, j = 0;
    int64_t e = 0;
    size_t len = width * depth;

    UTILS_CHECK_PTR(array);
    UTILS_CHECK_PTR(result);
    UTILS_CHECK_LEN(len);

    // hor
    for (i = 0; i < width - 1; i ++) {
        e = array[i];
        for (j = i; j < len; j += width + 1) {
            if (e != array[j]) {
                *result = false;
                LOG("found hor %lld and %lld\n", e, array[j]);
                goto finish;
            }
        }
    }

    // ver
    for (i = width; i < len - 1; i += width) {
        e = array[i];
        for (j = i; j < len; j += width + 1) {
            if (e != array[j]) {
                *result = false;
                LOG("found ver %lld and %lld\n", e, array[j]);
                goto finish;
            }
        }
    }

    *result = true;

finish:
    return ret;
}

Rust版本

fn toepitz_matrix(mat:&Vec<[i32;4]>) -> Result<bool, &'static str>
{
    let depth:usize = (*mat).len();
    let width:usize = (*mat)[0].len();
    let mut i:usize = 0;
    let mut j:usize = 0;
    let mut v:Vec<i32> = Vec::new();

    println!("the depth is {depth}");
    if 0 == depth || 0 == width {
        return Err("input len is wrong!\n");
    }

    while i < depth {
        let mut tv:Vec<i32> = (*mat)[i].into_iter().collect();
        v.append(&mut tv);
        i += 1;
    }
    i = 0;

    while i < width - 1 {
        let e = v[i];
        j = i;
        while j < v.len() {
            if e != v[j] {
                return Ok(false);
            }
            j += width + 1;
        }
        i += 1;
    }

    j = 0;
    i = width;
    while j < v.len() - 1 {
        let e = v[i];
        j = i;
        while j < v.len() {
            if e != v[j] {
                return Ok(false);
            }
            j += width + 1;
        }
    }

    return Ok(true);
}
carloscn commented 1 year ago

code

https://github.com/carloscn/structstudy/blob/master/c_programming/array/n46_toeplitz_matrix_766.c https://github.com/carloscn/structstudy/blob/master/rust_programming/array/src/n46_toeplitz_matrix_766.rs