查看: 2336| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

Google : Print a spiral array

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
given a number n... print a spiral matrix in O(1) space example if n=5 the op should be:

25 24 23 22 21
10 09 08 07 20
11 02 01 06 19
12 03 04 05 18
13 14 15 16 17

The question may appear trivial but its not...u have to print in O(1) space

上一篇:Amazon : Given an array, find the number of combinations to form a binary tree
下一篇:Adobe : Given an array A[i..j] find out maximum j-i such that A[i]<a[j]
🔗
 楼主| wwwyhx 2011-6-24 18:31:16 | 只看该作者
全局:
关键是构造一个函数原型如:int GetValue(int n, int i, int j)
通过 O(1)的时间复杂度获取”逻辑“数组上的a[i,j]的值:

int GetValue(int n, int i, int j)
{
        assert(n>0 && i<n && j<n);

        int x = n/2;
        int y = n/2;

        if (n%2 == 0)
                x--;

        if (x == i && y == j) return 1;

        bool bXDec = false;
        bool bYDec = true;
        int nSteps = 1;
        int nRet = 1;

        while (nSteps <= n && nRet <= n*n)
        {
                //calculate each step
                int nLeftX = nSteps;
                int nLeftY = nSteps;

                while (0 != nLeftY)
                {
                        bYDec ? y-- : y++;
                        nRet++;
                        if (i == x && j == y) return nRet;
                        nLeftY--;
                }

                while (0 != nLeftX)
                {
                        bXDec ? x-- : x++;
                        nRet++;
                        if (i == x && j == y) return nRet;
                        nLeftX--;
                }

                nSteps++;
                bXDec = !bXDec;
                bYDec = !bYDec;
        }

        return -1;
}

void PrintSpiral(int n)
{
        assert(n>0);

        for (int i = 0; i < n; i++)
        {
                for (int j = 0; j < n; j++)
                        cout<<GetValue(n, i, j)<<" ";
                cout<<endl;
        }
}
回复

使用道具 举报

🔗
Imbalism 2011-9-24 14:12:51 | 只看该作者
全局:
void _print(int n, int l)
{

        if(l == 1)
        {
                for(int i = 0; i < n; i++)
                        cout << n * n - i << " ";
                return;
        }
        if(l == n)
        {
                for(int i = n - 1; i >= 0; i--)
                        cout << n * n - (2 * (n - 1)) - i << " ";
                return;
        }
        cout << n * n - (3 * (n - 1)) - (n - l) << " ";
        _print(n - 2, l - 1);
        cout << n * n - (n - 1) - (l - 1) << " ";
}

void print(int n)
{
        cout << setw(2)<< setiosflags(ios::right) << setfill('0');       
        for(int i = 1; i <= n; ++i)
        {
                _print(5, i);
                cout << '\n';
        }
}

递归版, 没有简化式子
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表