Array(6) -- Maximum Subarray,Rotate Image,Unique Paths II,Spiral Matrix I, II

来源:互联网 发布:加工中心圆怎么编程 编辑:程序博客网 时间:2024/06/23 00:43

Maximum Subarray

此题类似于求最大积的Maximum Subarray,基本思想是DP,maxHere表示包含该数的最大值。

    int maxSubArray(vector<int>& nums) {        int maxHere = nums[0];        int totalMax = maxHere;        for(int i = 1; i < nums.size(); i++){            maxHere = max(maxHere+nums[i], nums[i]);            totalMax = max(totalMax, maxHere);        }        return totalMax;    }


Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

新的行 = 列; 新的列 = n-1-行。需要注意的点是i,j的循环范围,已经旋转过的不能再旋转。

    void rotate(vector<vector<int>>& matrix) {        int v2move, v2replace, tmp3;        int last_i, last_j;        int n = matrix.size();        float end = matrix.size();        int start = 0;        for(int i = 0; i < end; i++, end--, start++){            for(int j = start; j < end - 1; j++){                v2move = matrix[i][j];                last_i = i; last_j = j;                for(int k = 0; k < 4; k++){                    v2replace = matrix[last_j][n-1-last_i];                    matrix[last_j][n-1-last_i] = v2move;                    v2move = v2replace;                    tmp3 = last_i;                    last_i = last_j;                    last_j = n-1-tmp3;                }            }        }    }

Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

跟Unique Path I不同,这个无法通过组合的方法去计算,下面的解法多用了m+n的空间,避免了i-1>=0和j-1>=0的判断。

    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {        int m = obstacleGrid.size(), n = obstacleGrid[0].size();        vector<vector<int> > dp(m + 1, vector<int> (n + 1, 0));        dp[0][1] = 1;        for (int i = 1; i <= m; i++)            for (int j = 1; j <= n; j++)                if (!obstacleGrid[i - 1][j - 1])                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];        return dp[m][n];    } 


Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]

You should return [1,2,3,6,9,8,7,4,5].

按照spiral顺序添加元素,使用通过改变colCursor和rowCursor控制添加顺序。

    vector<int> spiralOrder(vector<vector<int>>& matrix) {        vector<int> rst;        if(matrix.size() < 1) return rst;         int upBound = -1, downBound = matrix.size();        int leftBound = -1, rightBound = matrix[0].size();        int rowCursor = 1, colCursor = 0;        int i = 0, j = 0;        int total = matrix.size() * matrix[0].size();        while(rst.size() < total){            while(j < rightBound && j > leftBound && i > upBound && i < downBound){                rst.push_back(matrix[i][j]);                i += colCursor;                j += rowCursor;            }            if(j == rightBound){                upBound++; j--; i++;                colCursor = 1; rowCursor = 0;            }            else if(j == leftBound){                downBound--; j++; i--;                colCursor = -1; rowCursor = 0;            }            else if(i == upBound){                leftBound++; i++; j++;                colCursor = 0; rowCursor = 1;            }            else if(i == downBound){                rightBound--; i--; j--;                colCursor = 0; rowCursor = -1;            }        }        return rst;    }


更简洁的做法。通过语句的顺序控制添加顺序,通过判断边界是否越界决定循环。

    vector<int> spiralOrder(vector<vector<int>>& matrix) {        if (matrix.empty()) return {};        int m = matrix.size(), n = matrix[0].size();        vector<int> spiral(m * n);        int u = 0, d = m - 1, l = 0, r = n - 1, k = 0;        while (true) {            // up            for (int col = l; col <= r; col++) spiral[k++] = matrix[u][col];            if (++u > d) break;            // right            for (int row = u; row <= d; row++) spiral[k++] = matrix[row][r];            if (--r < l) break;            // down            for (int col = r; col >= l; col--) spiral[k++] = matrix[d][col];            if (--d < u) break;            // left            for (int row = d; row >= u; row--) spiral[k++] = matrix[row][l];            if (++l > r) break;        }        return spiral;    }

Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

Given n = 3,

You should return the following matrix:
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]
思路和上面的题一样

    vector<vector<int>> generateMatrix(int n) {        vector<vector<int>> rst(n,vector<int>(n));        int up = 0, down = n-1, left = 0, right = n-1;        int count = 1;        while(1){            //up            for(int col = left; col <= right; col++) rst[up][col] = count++;            if(++up > down) break;                        //right            for(int row = up; row <= down; row++) rst[row][right] = count++;            if(--right < left) break;                        //down            for(int col = right; col >= left; col--) rst[down][col] = count++;            if(--down < up) break;                        //left            for(int row = down; row >= up; row--) rst[row][left] = count++;            if(++left > right) break;        }        return rst;    }


0 0
原创粉丝点击