CareerCup-1.6

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;
#define N 5
void rotate(int data[N][N])
{
    int temp, m;
    for(int i=0; i<N-2; i++)
    {
        m = N-1;
        for(int j=i; j<N-i-1; j++)
        {
            temp = data[i][j];
            data[i][j] = data[m-j][i];
            data[m-j][i] = data[m-i][m-j];
            data[m-i][m-j] = data[j][m-i];
            data[j][m-i] = temp;
        }
    }
}

void print(int data[N][N])
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
            cout<<data[i][j]<<"\t";
        cout<<endl;
    }
    cout<<"End"<<endl;
}
int main()
{
    int data[N][N];
    int count = 0;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            data[i][j] = ++count;
    print(data);
    rotate(data);
    print(data);
    system("pause");
};

#include <iostream>
using namespace std;
#define N 5
void rotate(int data[N][N])
{
    int temp, m;
    for(int i=0; i<N-2; i++)
    {
        m = N-1;
        for(int j=i; j<N-i-1; j++)
        {
            temp = data[i][j];
            data[i][j] = data[m-j][i];
            data[m-j][i] = data[m-i][m-j];
            data[m-i][m-j] = data[j][m-i];
            data[j][m-i] = temp;
        }
    }
}

void print(int data[N][N])
{
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<N; j++)
            cout<<data[i][j]<<"\t";
        cout<<endl;
    }
    cout<<"End"<<endl;
}
int main()
{
    int data[N][N];
    int count = 0;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            data[i][j] = ++count;
    print(data);
    rotate(data);
    print(data);
    system("pause");
};
时间: 2024-07-31 21:04:45

CareerCup-1.6的相关文章

CareerCup之2.2 寻找单链表倒数第n个元素

[题目] 原文: 2.2 Implement an algorithm to find the nth to last element of a singly linked list. 译文: 实现一个算法从一个单链表中返回倒数第n个元素. [分析] (1)创建两个指针p1和p2,指向单链表的开始节点. (2)使p2移动n-1个位置,使之指向从头开始的第n个节点.(意思是就是使p1和p2距离n个位置) (3)接下来检查p2 - > = = null 如果yes返回p1的值,否则继续移动p1和 p

CareerCup之1.8 字符串移位包含问题

[题目] 原文: 1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring ( i.e., "waterbottle" is a rotat

CareerCup之1.7 Set Matrix Zeroes

[题目] 原文: 1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0. 译文: 写一个函数处理一个MxN的矩阵,如果矩阵中某个元素为0,那么把它所在的行和列都置为0. [分析] [思路一] 遍历一次矩阵,当遇到元素等于0时,记录下这个元素对应的行和列. 可以开一个行数组row和列数组col,当元素a[i][j]等于0时, 就把row[

CareerCup之1.6 Rotate Image

[题目] 原文: 1.6 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? 译文: 一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度. 你能原地进行操作吗?(即不开辟额外的存储空间) [分析]

CareerCup之2.1无序链表删除重复元素

[题目] 原文: 2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? 译文: 从一个未排序的链表中移除重复的项 进一步地, 如果不允许使用临时的缓存,你如何解决这个问题? [分析] (1)如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况.

CareerCup之1.5 空格替换

[题目] 原文: 1.5 Write a method to replace all spaces in a string with '%20'. 译文: 写一个函数,把字符串中所有的空格替换为%20 . [分析] 我们首先应该想到的是原来一个空格字符,替换之后变成'%','2','0'三个字符,因此字符串会变长.如果是在原来的字符串上做替换 ,那么我们就有可能覆盖修改在该字符串后面的内存.如果是创建信的字符串并在新的字符串上做替换,那么我们自己可以分配足够的 内存.由于两种不同的解决方案,我们

CareerCup之1.4判断字符串是否为变位词

[题目] 原文: 1.4 Write a method to decide if two strings are anagrams or not. 译文: 写一个函数判断两个字符串是否是变位词. [分析] 变位词(anagrams)指的是组成两个单词的字符相同,但位置不同的单词.比如说, abbcd和abcdb就是一对变位词.该题目有两种思路: [思路一] 由于变位词只是字母的顺序改变,字符长度,字符种类没有改变,所以根据此我们只要重新根据字典序排序一下,两个字符串也就一样了. The eyes

CareerCup之1.1字符串中字符判重

[题目] Chapter 1 | Arrays and Strings 原文: 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 译文: 实现一个算法来判断一个字符串中的字符是否唯一(即没有重复).不能使用额外的数据结构. (即只使用基本的数据结构) [分析] [思路一]首先,我们要搞清

CareerCup之1.3字符串去重

[题目] 原文: 1.3 Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not. FOLLOW UP Write the test cases for

CareerCup之1.2C风格字符串翻转

[题目] 原文: Write code to reverse a C-Style String. (C-String means that "abcd" is represented as five characters, including the null character.) 译文: 写代码翻转一个C风格的字符串.(C风格的意思是"abcd"需要用5个字符来表示,包含末尾的 结束字符) [分析] 这是一道经典的面试题目,虽然看似简单,但仍然有陷阱.唯一的陷阱