Rotate Image、Pascal's Triangle vector用法

来源:互联网 发布:批量编辑图片的软件 编辑:程序博客网 时间:2024/06/16 03:51
题目:

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

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

顺时针旋转90度

void retate(vector<vector<int> > &matrix){int len = matrix[0].size();vector<vector<int> > b(len);//二维vector 定义for (int i = 0; i<len; i++)// 二维vector初始化{for (int j = 0; j<len; j++){b[i].push_back(matrix[i][j]);}}for (int i = 0; i<len; i++){for (int j = 0; j<len; j++){matrix[j][len-i-1] =b[i][j] ;}}}



题目:

Pascal's Triangle

 

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[     [1],    [1,1],   [1,2,1],  [1,3,3,1], [1,4,6,4,1]]

//vector 作为函数返回值类型时,只用返回容器名就可以

vector<vector<int> > generate(int numRows)    {        vector<vector<int > >matrix(numRows);//二维vector 定义必须定义大小,如果是vector<vector<int > >matrix 则提示错误        if(numRows==0)return matrix;matrix[0].push_back(1);if (numRows == 1)return matrix;    matrix[1].push_back(1);    matrix[1].push_back(1);    if (numRows == 2)    return matrix;    else    {    for (int i = 2; i < numRows; i++)    {    matrix[i].push_back(1);    for (int j = 1; j < i; j++)    {    matrix[i].push_back(matrix[i - 1][j] + matrix[i - 1][j - 1]);    }    matrix[i].push_back(1);    }    return matrix;    }    }

0 0