Cracking The Coding Interview 1.6
2014-04-03 09:45
344 查看
//原文: // // Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? // // 先转置在上下交换 #include <iostream> using namespace std; void swap(int &a, int &b) { int t=a; a=b; b=t; } void replace(int **matrix, int size) { for (int i = 0;i<size; i++) { for (int j=i;j<size; j++) { swap(matrix[i][j],matrix[j][i]); } } for (int i = 0; i<size/2; i++) { for (int j=0;j<size; j++) { swap(matrix[i][j],matrix[size-1-i][j]); } } } void display(int **matrix, int size) { for (int k = 0; k<size; k++) for (int t = 0; t<size; t++) { cout<<matrix[k][t]; if (t==size-1) { cout<<endl; } } } int main() { int size = 4; int **p = new int *[size]; for (int i=0; i<size; i++) { p[i] = new int[size]; } for (int k = 0; k<size; k++) for (int t = 0; t<size; t++) { p[k][t] = 2*k+t; } display(p,size); replace(p,size); cout<<endl; display(p,size); int a=3,b=4; swap(a,b); for (int k=0;k<size;k++) { delete [] p[k]; } delete p; return 0; }
相关文章推荐
- cracking the coding interview No1.6
- Cracking the Coding Interview Q1.6
- Cracking the coding interview
- Cracking the coding interview--Q3.1
- Cracking the coding interview--Q8.4
- Cracking the Coding Interview(1)
- Cracking the coding interview--Q2.1
- Cracking the coding interview--Q5
- Cracking the coding interview--Q1.2
- Cracking the coding interview--Q1.7
- Cracking the coding interview--Q8.7
- Cracking the coding interview--Q10.1~Q10.7
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目3
- 《Cracking the Coding Interview》——第4章:树和图——题目5
- 《Cracking the Coding Interview》——第6章:智力题——题目4
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目7
- Cracking the Coding Interview Q2.1
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目1
- [Cracking the Coding Interview] Chapter 1 - Arrays and Strings
- Cracking the Coding Interview – ch16,17,18