2014-05-06 06:37

题目链接

原题:

Given an array of (unsorted) integers, arrange them such that a < b > c < d > e... etc.

题目:给定一个无序的数组,调整元素顺序,使得数组满足a < b > c < d > e ... 这种形式。

解法:这题没有说明两点:1. 数组元素是否存在重复值。 2. 给定的数组经过调整后是否一定有解。如果确定有解,那么我有两种方法。一种是线性算法,向后扫描的过程中逐个调整相邻元素,使其满足题意的大小交替变化的要求。如果没有重复元素时这种算法一定有解。另一种算法先要求排序数组,然后从数组的两端逐个取元素:小大小大小大...。这种算法对于存在重复元素的情况,也能找出正确解,而前种方法则可能无法处理存在重复元素的数组。排序要求的时间至少是O(n * log(n)),在效率上自然不够高了。对于数组的调整都可以就地完成,无需额外数组。

代码:

 // http://www.careercup.com/question?id=5724823657381888
#include <algorithm>
#include <vector>
using namespace std; class Solution {
public:
void arrangeArray(vector<int> &v) {
int n; n = (int)v.size();
if (n < ) {
return;
}
sort(v.begin(), v.end());
interleaveInPlace(v);
};
private:
void interleaveInPlace(vector<int> &v) {
int i, n;
int idx1, idx2;
int tmp1, tmp2; n = (int)v.size();
if (n <= ) {
return;
} idx1 = n - ;
tmp1 = v[idx1];
for (i = ; i < n - ; ++i) {
idx2 = (idx1 >= (n + ) / ) ? ( * n - - * idx1) : (idx1 * ); tmp2 = v[idx2];
v[idx2] = tmp1;
tmp1 = tmp2; idx1 = idx2;
}
};
};

Careercup - Google面试题 - 5724823657381888的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. CentOS 5.5 下安装Countly Web Server过程记录

    CentOS 5.5 下安装Countly Web Server过程记录 1. 系统更新与中文语言包安装 2. 基本环境配置: 2.1. NodeJS安装 依赖项安装 yum -y install g ...

  2. 搭建高性能计算环境(五)、应用软件的安装之Amber12

    应用软件通常安装在/opt目录下,这样系统中的各个用户都能方便使用,下面的软件都将安装到/opt目录. 1,上传需要的软件包Amber12.tar.gz.AmberTools13.tar.bz2.Am ...

  3. js dom 操作

    JS的DOM操作   1DOM是文档对象模型,这种模型为树模型:文档是指标签文档,对象是指文档中每个元素:模型是指抽象化的东西. 2间隔与延迟间隔执行一段代码(函数):window.setInterv ...

  4. PHPExcel上传sae遇到: -1:fail to get xml content

    在用PHPExcel1.8.0来处理excel时,本地测试时好使的,但是要把代码部署到SAE,在上传代码的时候就会遇到这个问题. 部署代码中遇到问题: -1:fail to get xml conte ...

  5. 【转】src与href属性的区别

    ref:http://www.jb51.net/web/77258.html src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src是 ...

  6. 小鸟哥哥博客 For SAE

    独立博客地址:http://www.zhujiawei.com.cn/ 辞职后出去玩了几个月,把积蓄都快花光了,打算熬到年底再找工作.最近闲来无聊,想起自己一年前趁着活动便宜,一口气买了10年的域名一 ...

  7. 用过的一些js函数[备份用的]

    1.类似php的htmlspecialchars函数,如需要可以自行增加其它代替 function _htmlspecialchars(str) { str = str.replace(/&/ ...

  8. 04-树5 Root of AVL Tree

    平衡二叉树 LL RR LR RL 注意画图理解法 An AVL tree is a self-balancing binary search tree. In an AVL tree, the he ...

  9. 比支付宝更好用的读IC卡信息工具

    编程语言:VC++ 更新时间:2014.10.23 操作系统:windowAll 工具:PCSC读卡器 在上一个博文<<解惑:NFC手机如何轻松读取银行卡信息?>>中,介绍了支 ...

  10. 【转】C# 子窗体如何调用父窗体的方法

    网络上有几种方法,先总结如下: 调用窗体(父):FormFather,被调用窗体(子):FormSub. 方法1: 所有权法       //FormFather:        //需要有一个公共的 ...