楼主: smzfeng
跳转到指定楼层
上一主题 下一主题
收起左侧

微软intern面经,求答案

🔗
bingjie 2012-3-14 03:36:51 | 只看该作者
全局:
is that in this way?
Firstly find the number of positive and negtive numbers, then exchange corresponding positions.
I think it is O(n)
回复

使用道具 举报

🔗
 楼主| smzfeng 2012-3-14 04:53:29 | 只看该作者
全局:
回复 7# wwwyhx

为什么校园面试就碰到这种题……
回复

使用道具 举报

🔗
 楼主| smzfeng 2012-3-14 04:55:08 | 只看该作者
全局:
回复 9# rayray81502

这样顺序是会乱的
回复

使用道具 举报

🔗
 楼主| smzfeng 2012-3-14 04:56:13 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
 楼主| smzfeng 2012-3-14 04:57:42 | 只看该作者
全局:
回复 11# bingjie

是会乱序的吧
回复

使用道具 举报

🔗
wwwyhx 2012-3-14 06:13:21 | 只看该作者
全局:
没思路, 不会做
回复

使用道具 举报

🔗
3vilCoder 2012-3-14 06:43:03 | 只看该作者
全局:
贴下版大发的习题里面的M12.h。
没细看,供大家参考:
  1. //Given an array of positive and negative integers, re-arrange it
  2. //so that you have postives on one end and negatives on the other,
  3. //BUT retain the original order of appearance.
  4. //For eg. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15

  5. // There is a solution which took O(n) space and O(n) time which is simple
  6. // Below is a solution which adopt the concept of merge so the time cost is
  7. // O(nlogn) and space O(1)
  8. // Below is the merge sort solution, notice that if you want to turn -1,-2,3,4,5
  9. // to 3,4,5,-1,-2, you need to reverse the whole array, that's to 5,4,3,-2,-1; and for
  10. // the two segment, reverse each segment back. So, first reversion come to 3,4,5,-2,-1,
  11. // last reversion come to 3,4,5,-1,-2 which is the resilt you want

  12. void Rearrange(int a[], int n)
  13. {
  14.         assert(n>0);
  15.         if (1 == n) return;

  16.         if (2 == n)
  17.         {
  18.                 if (a[0]>=0 && a[1]<0)
  19.                 {
  20.                         int tmp = a[0];
  21.                         a[0] = a[1];
  22.                         a[1] = tmp;
  23.                 }

  24.                 return;
  25.         }

  26.         int nLft = n/2;
  27.         int nRgt = n-nLft;
  28.         int* pLft = a;
  29.         int* pRgt = a+nLft;
  30.         Rearrange(pLft, nLft);
  31.         Rearrange(pRgt, nRgt);

  32.         int* pIterL = pLft;
  33.         int* pIterR = pRgt;
  34.         while (*pIterL < 0 && pIterL-pLft<nLft) pIterL++;
  35.         while (*pIterR < 0 && pIterR-pRgt<nRgt) pIterR++;
  36.        
  37.         pIterR--;
  38.         int* pTmpL = pIterL;
  39.         int* pTmpR = pIterR;
  40.         while (pTmpL < pTmpR)
  41.         {
  42.                 int tmp = *pTmpL;
  43.                 *pTmpL = *pTmpR;
  44.                 *pTmpR = tmp;
  45.                 pTmpL++;
  46.                 pTmpR--;
  47.         }

  48.         int* pIterLEnd = pIterL+(pIterR-pRgt);
  49.         int* pIterRBeg = pIterLEnd+1;
  50.         pTmpL = pIterL;
  51.         pTmpR = pIterLEnd;
  52.         while (pTmpL < pTmpR)
  53.         {
  54.                 int tmp = *pTmpL;
  55.                 *pTmpL = *pTmpR;
  56.                 *pTmpR = tmp;
  57.                 pTmpL++;
  58.                 pTmpR--;
  59.         }

  60.         pTmpL = pIterRBeg;
  61.         pTmpR = pIterR;
  62.         while (pTmpL < pTmpR)
  63.         {
  64.                 int tmp = *pTmpL;
  65.                 *pTmpL = *pTmpR;
  66.                 *pTmpR = tmp;
  67.                 pTmpL++;
  68.                 pTmpR--;
  69.         }
  70. }

  71. void test()
  72. {
  73.         int a[] = {1,-3, 56, 7, -5, 9, -12, 15};
  74.         Rearrange(a, sizeof(a)/sizeof(int));
  75.         for (int i = 0; i < sizeof(a)/sizeof(int); i++)
  76.         {
  77.                 cout<<a[i]<<" ";
  78.         }
  79.         cout<<endl;
  80. }
复制代码
题目稍有变化,版大给的题目是要求负数在前面,这个相反,供大家参考了!

评分

参与人数 1大米 +20 萝卜 +2 收起 理由
jintian1_84 + 20 + 2 谢谢分享答案

查看全部评分

回复

使用道具 举报

🔗
 楼主| smzfeng 2012-3-14 06:49:54 | 只看该作者
全局:
回复 17# tsy3602

递归仍能保持O(1)?stack当中的空间不计入么?
回复

使用道具 举报

🔗
3vilCoder 2012-3-14 06:52:28 | 只看该作者
全局:
回复 18# smzfeng


    应该是这么理解了...具体问版大,他写的程序!!
回复

使用道具 举报

🔗
rayray81502 2012-3-14 08:04:27 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

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

本版积分规则

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