编程练习(1)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.</span>

字符串的旋转

对于一个字符串,和字符串中的某一位置,请设计一个算法,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边。

给定字符串A和它的长度n以及特定位置p,请返回旋转后的结果。

测试样例:

"ABCDEFGH",8,4

返回:"FGHABCDE"


class StringRotation {
public:
    string rotateString(string A, int n, int p) {
        // write code here
        return A.substr(p+1)+A.substr(0,p+1);
    }
};

2.

之字形打印矩阵

对于一个矩阵,请设计一个算法,将元素按字形打印。具体见样例。

给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。

测试样例:

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

返回:[1,2,3,6,5,4,7,8,9,12,11,10]


class Printer {
public:
    vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
        // write code here
        vector<int> resu;
        for(int i=0;i<n;i++){
           	if(i%2 == 0){
                for(int j=0;j<m;j++){
                    resu.push_back(mat[i][j]);
                }
            }
            else{
                for(int j=m-1;j>=0;j--){
                    resu.push_back(mat[i][j]);
                }
            }
        }
        return resu;
    }
};


3.

顺时针旋转矩阵

有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。

给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵,保证N小于等于300

测试样例:

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

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


class Rotate {
public:
    vector<vector<int> > rotateMatrix(vector<vector<int> > mat, int n) {
        // write code here
        vector<vector<int>> resu(n,vector<int>(n));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                resu[i][j] = mat[n-1-j][i];
            }
        }
        return resu;
    }
};



4.

顺时针打印矩阵

对于一个矩阵,请设计一个算法从左上角(mat[0][0])开始,顺时针打印矩阵元素。

给定int矩阵mat,以及它的维数nxm,请返回一个数组,数组中的元素为矩阵元素的顺时针输出。

测试样例:

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

返回:[1,2,4,3]


class Printer {
public:
    vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) {
        // write code here
        vector<int> resu;
        int i=0,j=0,top=0,bottom=n,left=0,right=m;
        int type = 1;
        while(top<bottom && left<right){
            if(type == 1){
                for(int j=left;j<right;j++){
                    resu.push_back(mat[top][j]);
                }
                type = 2;
                top++;
            }
            else if(type == 2){
                for(int i=top;i<bottom;i++){
                    resu.push_back(mat[i][right-1]);
                }
                type = 3;
                right--;
            }
            else if(type == 3){
                for(int j=right-1;j>=left;j--){
                    resu.push_back(mat[bottom-1][j]);
                }
                type = 4;
                bottom--;
            }
            else if(type == 4){
                for(int i=bottom-1;i>=top;i--){
                    resu.push_back(mat[i][left]);
                }
                type = 1;
                left++;
            }
                
        }
        return resu;
    }
};


5.

折纸问题

请把纸条竖着放在桌子上,然后从纸条的下边向上方对折,压出折痕后再展开。此时有1条折痕,突起的方向指向纸条的背面,这条折痕叫做“下”折痕 ;突起的方向指向纸条正面的折痕叫做“上”折痕。如果每次都从下边向上方对折,对折N次。请从上到下计算出所有折痕的方向。
给定折的次数n,请返回从上到下的折痕的数组,若为下折痕则对应元素为"down",若为上折痕则为"up".
测试样例:
1
返回:["down"]

class FoldPaper {
public:
	vector<string> foldPaper(int n) {
		vector<string> resu;
		
		printPaper(1, n, 0,resu);
		resu.push_back("down");
		printPaper(1, n, 1, resu);

		return resu;

	}

	void printPaper(int i, int n, int flag, vector<string> &resu) {
		if (i<n) {
			if (flag == 0) {
				printPaper(i + 1, n, 0,resu);
				resu.push_back("down");
				printPaper(i + 1, n, 1, resu);
			}
			else if (flag == 1) {
				printPaper(i + 1, n, 0, resu);
				resu.push_back("up");
				printPaper(i + 1, n, 1, resu);
			}
		}
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值