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. hibernate的第一个程序

    #建表语句 create database hibernate; use hibernate; create table user( id int primary key, name varchar( ...

  2. 【CSS3】---层模型position之fixed固定定位、absolute绝对定位和relative相对定位

    什么是层模型? 什么是层布局模型?层布局模型就像是图像软件PhotoShop中非常流行的图层编辑功能一样,每个图层能够精确定位操作,但在网页设计领域,由于网页大小的活动性,层布局没能受到热捧.但是在网 ...

  3. 清除层div浮动

    clearboth { clear: both; display: block; height: 0; font-size: 0; overflow: hidden; } <div class= ...

  4. MVC3 Razor视图引擎的基础语法

    好久没有关注微软的开发了,今天看到了MVC3,顺便学习学习,我觉得Razor是个不错的做法,比使用<%%>简单多了,而且好看.首先“_”开头的cshtml文档将不能在服务器上访问,和asp ...

  5. MyFragment

    手机横竖屏自动切换不同的View: Landscape-Horizontal-横屏 Portrait-Vertical-竖屏 package com.example.shad_fnst.myfragm ...

  6. 面向对象的异常处理之深入理解java异常处理机制

    什么是异常? 异常是对问题的描述,将问题的对象进行封装: 异常体系的特点:异常体系中的所有类以及建立的对象: 都具有可抛性,也就是说可以被throw和throws关键字所操作,只有异常体系具有该特点: ...

  7. OSPF系列

    实验一.点对点链路上的OSPF 拓扑图 1. 首先配置好路由器R1接口地址和回环地址 2. 配置路由器R2的接口地址和回环地址 3. 配置路由器R3的接口地址和环回地址 4. 配置R1的OSPF协议 ...

  8. VLAN系列

    Write From Yangwj Sunday, March 9, 2014 一. Vlan的识别 1. 交换机端口是访问端口,它就属于某一个Vlan:如果是中继端口,它就可以属于所有Vlan. 2 ...

  9. 移动web开发的一些坑

    类似的题目一搜一大堆,我就不再写那些meta标签类似的内容了,记录一下自己实现中遇到的问题,如果能帮到你,那再好不过了. 1px border的问题,大家能搜到很多方案,但如何选择还是要根据实际情况, ...

  10. 4月13日学习笔记——jQuery工具函数

    浏览器及特性检测 jQuery.support.boxModel 如果这个页面和浏览器是以 W3C CSS 盒式模型来渲染的,则等于 true.通常在 IE 6 和 IE 7 的怪癖模式中这个值是 f ...