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. 深入浅出ExtJS 第六章 布局

       6.1 布局的用途 6.1 布局的用途 //决定把什么东西放到什么位置; var vieport = new Ext.Viewport({ layout:'border', //使用Border ...

  2. VC++ DLL 发布到生产环境过程

    最近项目中用到了VC++ DLL,在本机调试时无任何问题,但是发布出来后,COPY到另外的机器就报错,说找不到DLL,由于自身工作接触这方面比较少,经过一番折腾后,终于解决,以下为解决步骤 一,平台工 ...

  3. Process Stats:了解你的APP如何使用内存(转)

    原文地址:http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html?m=1 原作 ...

  4. MySQL 数据类型 详解 (转载)

    数值类型 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许我们指 ...

  5. Wim技术之Wim文件的制作

    背景:操作的镜像文件为win8.1 update的ISO里的Wim文件 1.使用如下命令将支持WimBoot的instal.Wim文件转换成可以支持wimboot启动的映像文件 Dism /Expor ...

  6. [老老实实学WCF] 第一篇 Hello WCF

    老老实实学WCF  第一篇 Hello WCF WCF(Windows Communication Foundation)是微软公司推出的面向服务技术的集大成者,涵盖继承了其之前发布的所有的分布式应用 ...

  7. 2015年最新出炉的JavaScript开发框架

    前端框架简化了开发过程中,像 Bootstrap 和 Foundation 就是前端框架的佼佼者.在这篇文章了,我们编制了一组新鲜的,实用的,可以帮助您建立高质量的 Web 应用程序的 JavaScr ...

  8. How to Get Rid of /wordpress/ From your WordPress Site URL

    I brought up a website using wordpress, but I had to visit my website in a way I don't like -- www.e ...

  9. 4_1 wp8数据绑定与独立存储空间[wp8特色开发与编程技巧]

    Wp8数据绑定与独立存储空间 数据绑定为基于 Silverlight 的应用程序提供了一种显示数据并与数据进行交互的简便方法. 数据的显示方式独立于数据的管理. UI 和数据对象之间的连接或绑定使数据 ...

  10. background-clip 背景图片做适当的裁剪

    background-clip 用来将背景图片做适当的裁剪以适应实际需要. 语法: background-clip : border-box | padding-box | content-box | ...