美文网首页
8.23 cc 数组和字符串[pt2]

8.23 cc 数组和字符串[pt2]

作者: 陈十十 | 来源:发表于2016-08-23 13:46 被阅读15次

to do

  • cc150 array&string (6-11)/11
  • leetcode review day 1

1.6] Rotate N*N matrix by 90 degrees

Rotate Image

  • note how to use start and end to keep track easily
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for (int layer=0; layer<matrix.size()/2; ++layer) {
            int start = layer, end = start+(n-1);
            for (int i=start; i<end; ++i) {
                int tmp = matrix[start][i];
                matrix[start][i] = matrix[end-(i-start)][start];
                matrix[end-(i-start)][start] = matrix[end][end-(i-start)];
                matrix[end][end-(i-start)] = matrix[i][end];
                matrix[i][end] = tmp;
            }
            n-=2;
        }
    }

1.7] Given M*N matrix, clear row and col where 0 occurs

Set Matrix Zeroes
check~ note the last for loop, to avoid repeatly setting zeros

    void setZeroes(vector<vector<int>>& matrix) {
        if (matrix.empty() || matrix[0].empty()) return;
        int m = matrix.size(), n = matrix[0].size();
        vector<bool> shouldSetRow(m, false);
        vector<bool> shouldSetCol(n, false);
        for (int i=0; i<m; ++i) {
            for (int j=0; j<n; ++j) {
                if (!matrix[i][j]) {
                    shouldSetRow[i] = true;
                    shouldSetCol[j] = true;
                }
            }
        }
        for (int i=0; i<m; ++i) {
            for (int j=0; j<n; ++j) {
                if (shouldSetRow[i] || shouldSetCol[j])
                    matrix[i][j] = 0;
            }
        }
    }

1.8] 假设提供了isSubstring,可以检查strA是否为strB的子串。给定两个字符串s1和s2,判断是否s2由s1旋转而来,只可用isSubstring一次。

恩不剧透了,想不出看P116

相关文章

网友评论

      本文标题:8.23 cc 数组和字符串[pt2]

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