美文网首页
2020-02-08 刷题 2(数组)

2020-02-08 刷题 2(数组)

作者: nowherespyfly | 来源:发表于2020-02-08 18:09 被阅读0次

36 有效的数独

由于二维数组一共只有81个数,所以基本都可以保证常数时间复杂度和常数空间复杂度。官方题解里,每行,每列,每个3x3分别一个哈希,一次循环就能判断完;我的做法是一个哈希表,对应了每个元素及其出现的位置,然后遍历这个哈希表。
另外,判断是否在一个patch(也就是3x3)内,只要用两个坐标除以三的商是否相等就可以。
标签:哈希表
代码:

time: 68.81%, memory: 38.65%
class Solution {
public:
struct point{
    point(int i_x, int i_y): x(i_x), y(i_y){}
    int x, y;
};
bool isValidSudoku(vector<vector<char>>& board) {
    map<char, vector<point>> pos_map;
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 9; j++){
            if (board[i][j] != '.'){
                pos_map[board[i][j]].push_back(point(i, j));
            }
        }
    }

    map<char, vector<point>>::iterator iter;
    for(iter = pos_map.begin(); iter != pos_map.end(); iter++){
        vector<point> tmp_vec = iter -> second;
        int vec_len = tmp_vec.size();
        if(vec_len <= 1)
            continue;
        else if(vec_len > 9){
            return false;
        }
        else{
            for(int i = 0; i < vec_len - 1; i++){
                for(int j = i + 1; j < vec_len; j++) {
                    if (tmp_vec[j].x == tmp_vec[i].x || tmp_vec[j].y == tmp_vec[i].y) 
                        return false;
                    if ((tmp_vec[i].x / 3 == tmp_vec[j].x / 3) && (tmp_vec[i].y / 3 == tmp_vec[j].y / 3))
                        return false;
                }
            }
        }
    }
    return true;
}
};

48 旋转图像

标签:原地旋转,数组,矩阵
官方题解的一个想法很巧妙,就是顺时针旋转90度,等效于先转置再翻转。学习到了。
本题采用的做法跟旋转数组很像,只是扩展到了二维数组。
基本的变换公式为:(x', y') = (y, n-1-x).
观察到旋转操作其实只发生在每一层内,可以在每一层分别旋转。具体操作还是很麻烦,但是认真一点是可以写对的。
代码:

time:100%, memory:5.05%
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
    int N = matrix.size();
    int round = N / 2;
    int cnt = 0;
    int start_x = 0, start_y = 0;
    int prev_x = 0, prev_y = 0;
    int next_x, next_y;
    int last_val = matrix[0][0], tmp_val;
    int cur_round = 0;
    while(cur_round < round){
        // move prev val to next val
        next_x = prev_y;
        next_y = N - 1 - prev_x;
        tmp_val = matrix[next_x][next_y];
        matrix[next_x][next_y] = last_val;
        last_val = tmp_val;
        cnt++;

        // update coord
        if(next_x == start_x && next_y == start_y){
            // not new round
            start_y++;
            if(cnt == (N - cur_round * 2) * 4 - 4){
                // new round
                start_x++;
                start_y = start_x;
                cur_round++;
                cnt = 0;
            }
            prev_x = start_x;
            prev_y = start_y;
            last_val = matrix[prev_x][prev_y];
        }
        else{
            prev_x = next_x;
            prev_y = next_y;
        }

    }
}
};

相关文章

  • 2020-02-08 刷题 2(数组)

    36 有效的数独 由于二维数组一共只有81个数,所以基本都可以保证常数时间复杂度和常数空间复杂度。官方题解里,每行...

  • 刷题-数组专项

    数组 二维数组中的查找题目描述:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每...

  • 2020-06-30 刷题2(数组)

    1. 判断字符是否唯一(面试题01.01) 用数组 2. 判定是否为字符串重排 还是用数组。

  • 2020-02-07 刷题 2(数组)

    66 加一 思路很简单,从后向前扫描,如果当前位小于9,就加一然后退出循环,否则置零继续向前循环。最后判断一下第一...

  • 2020-02-05 刷题2(数组)

    189 旋转数组 数组整体移位的题目。最简单的就是,用另一个数组来暂时存放,时间复杂度O(n), 空间复杂度O(n...

  • 数组-Python刷题笔记

    二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上...

  • leetcode刷题之数组

    leetcode刷题,使用python 1, 两数之和—— 0001 数组给定一个整数数组 nums 和一个整数...

  • [数组]442. Find All Duplicates in

    标签(空格分隔): 数组 leetcode 刷题 题目链接 给定一个数组,1≤a[i]≤n(n =数组的大小),里...

  • Java全排列递归算法

    刷题!刷题!发现对于数组元素的全排列很多题目都有涉及到,所以详细研究一下对一个数组进行全排列,我们可以这样考虑,我...

  • leecode刷题(3)-- 旋转数组

    leecode刷题(3)-- 旋转数组 旋转数组 给定一个数组,将数组中的元素向右移动 K 个位置,其中 K 是非...

网友评论

      本文标题:2020-02-08 刷题 2(数组)

      本文链接:https://www.haomeiwen.com/subject/dnkixhtx.html