2014-05-01 01:23

题目链接

原题:

WAP to modify the array such that arr[I] = arr[arr[I]].
Do this in place i.e. with out using additional memory. example : if a = {,,,}
o/p = a = {,,,} Note : The array contains to n- integers.

题目:给定一个长度为n的数组,由0至n - 1的排列组成。请做这样的变换,arr[i] = arr[arr[i]]。要求就地完成,不用额外数组。

解法:当数据范围有明确限制时,能够通过压缩维度来节省空间。对于一个数对(x, y),如果x和y都属于[0, n - 1],那么x * n + y就能唯一表示这个数对了。因此,可以先把变换后的结果存在高位上,然后再把高位的数通过除法移动到低位。算法是线性的,能够就地完成。

代码:

 // http://www.careercup.com/question?id=4909367207919616
#include <cstdio>
#include <vector>
using namespace std; void displaceInPlace(vector<int> &v)
{
int i;
int n; // all elements in the array has value between 0 and n - 1.
n = (int)v.size();
for (i = ; i < n; ++i) {
v[i] = v[v[i]] % n * n + v[i];
}
for (i = ; i < n; ++i) {
v[i] = v[i] / n;
}
} int main()
{
int i, n;
vector<int> v; while (scanf("%d", &n) == && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
displaceInPlace(v);
for (i = ; i < n; ++i) {
printf((i ? " %d" : "%d"), v[i]);
}
putchar('\n');
v.clear();
} return ;
}

Careercup - Facebook面试题 - 4909367207919616的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. 纯JS画点、画线、画圆的方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Android点击其他任意位置收起软键盘

    在Android应用开发中,经常出现这样的需求,用户在输入文字的过程中,可能不想继续输入了,通过滑动或者点击其他位置(除软键盘和EditText以外的任何位置),希望能够自动收回键盘,这个功能可能有些 ...

  3. Part 6 Group by in sql server

  4. DWZ (JUI) 教程 国际化问题(多语言/语言切换)

    DWZ 国际化也是比较简单的,网站的内容国际化和常规的项目国际化是一样的,不要做出特殊的调整. DWZ 自身框架的国际化,比如 翻页的上一页下一页等信息.这些信息都是在dwz.frag.xml 文件当 ...

  5. 英特尔® 实感™ SDK 前置摄像头 (F200) 常见问题解答

    原文地址 https://software.intel.com/zh-cn/articles/intel-realsense-sdk-faq-for-front-facing-camera-f200? ...

  6. ASP.Net 类(CS)文件怎样获取Web应用程序的路径

    Web应用程序,写了一个线程CS类别,这个类别将会放于Global.asax文件中执行,主要是监控程序下某一个文件是否有异动,而作出相应警示动作,如发送邮件等. 实现运行过程中,也许会有一个情况出现, ...

  7. Cocos2d-x移植到WindowsPhone8移植问题-libNetwork库移植问题

    Cocos2d-x提供了libNetwork库的源代码,而且还提供了libNetwork库的源代码Visual Studio 2012工程文件libNetwork.vcxproj,这样就更方便移植了. ...

  8. 【学习笔记】【C语言】数据

    1. 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文字数据.用迅 ...

  9. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  10. change

    #include<iostream> using namespace std; int main() { double a; cin>>a; cout<<a< ...