careercup-1.6

1.6 给定一幅由N*N矩阵表示的如下,其中每个像素的大小为4个字节,编写一个方法,将图像选择90度。不占用额外内存空间能否做到?

思路:

我们这里以逆时针旋转为例(写代码时比较容易理解,顺时针旋转的实现思想相似),可以先将原矩阵以主对角线为对称轴,交换主对角线两侧的的元素,得到新的矩阵(如果是顺时针旋转,可以交换副对角线两侧的元素),再将该矩阵的第i行和第n-i-i行相交换,即得到逆时针旋转后的矩阵。

C++代码实现:

#include<iostream>
using namespace std;

void rotate(int (*arr)[4],int n)
{
    int i,j;
    /*for(i=0;i<n;i++)
    {
        for(j=1;j<n;j++)
        {
            swap(arr[i][j],arr[j][i]);
        }
    }
    for(i=0;i<n/2;i++)
        for(j=0;j<n;j++)
            swap(arr[i][j],arr[n-i-1][j]);*/
    for(i=0; i<n; i++)
    {
        for(j=0; j<n-i-1; j++)
        {
            swap(arr[i][j],arr[n-j-1][n-i-1]);
        }
    }
    for(i=0; i<n/2; i++)
        for(j=0; j<n; j++)
            swap(arr[i][j],arr[n-i-1][j]);

}

int main()
{
    int A[4][4] =
    {
        {11,12,13,14},
        {15,16,17,18},
        {19,20,21,22},
        {23,24,25,26}
    };
    rotate(A,4);
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
            cout<<A[i][j]<<",";
        cout<<endl;
    }
}
时间: 2024-11-16 22:14:06

careercup-1.6的相关文章

[CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数

18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. LeetCode上的原题,请参见我之前的博客Find Median from Data Stream. 解法一: priority_queue<int> small; priority_queue<int,

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字

CareerCup Facebook Total number of substring palindrome

Write a function for retrieving the total number of substring palindromes. For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba So the result is 6. Updated at 11/11/2013: After the interview I got know that the O(n^3) s

Careercup | Chapter 4

二叉查换树,左孩子小于等于根,右孩子大于根. 完全二叉树,叶子都在最后一层,所有结点(除了叶子)都有两个孩子. 平衡二叉树,左右子树的高度在一定范围内. 4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two s

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] 18.10 Word Transform 单词转换

18.10 Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary. 这道题让我们将一个单词转换成另一个单词,每次只能改变一个字母,

Careercup | Chapter 6

6.3 You have a five-quart jug, a three-quart jug, and an unlimited supply of water (but no measuring cups). How would you come up with exactly four quarts of water? Note that the jugs are oddly shaped, such that filling up exactly "half" of the

[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels. LeetCode上的原题,请参见我之前的解法Maximal Square.书上给了两种解法,但是比较长:

[CareerCup] 18.3 Randomly Generate Integers 随机生成数字

18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 这道题让我们从一个数组中随机取出m个数字,要求每个数字被取出的概率相同,其实这道题用的是之前那道18.2 Shuffle Cards的方法,同样我们可以用递归和迭代两种方法来做,递归的思路还用的回溯法,回溯到i+

[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes. LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制,Course Schedule 课程清单 和 Course Schedule II 课程清单之二.目前看来CareerCup中有关图的题在第四章中仅此一道,这是