Careercup - Google面试题 - 5724823657381888
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- 使用Spring开发第一个HelloWorld应用
http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...
- win7如何设置某个软件不弹出用户账户控制
手动修改注册表: 在 HKEY_CURRENT_USERS\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers 键下面 ...
- 基于CSS+dIV的网页层,点击后隐藏或显示
一个基于CSS+dIV的网页层,用JavaScript结合Input按钮进行控制,点击后显示或隐藏,网页上常用到的特效之一,实用性较强,相信对大家的前端设计有帮助. <!DOCTYPE html ...
- 【原】简述使用spark集群模式运行程序
本文前提是已经正确安装好scala,sbt以及spark了 简述将程序挂载到集群上运行的步骤: 1.构建sbt标准的项目工程结构: 其中: ~/build.sbt文件用来配置项目的基本信息(项目名 ...
- Spark官方文档——独立集群模式(Standalone Mode)
除了部署在Mesos之上, Spark也支持独立部署模式,包括一个Spark master进程和多个 Spark worker进程.独立部署模式可以运行在单机上作为测试之用,也可以部署在集群上.如果你 ...
- Android开发环境下关于如何导出手机通讯录数据库【Written By KillerLegend】
首先度Linux中的权限(Permissions)进行一些说明: permissions一共有10个符号位,[- --- --- ---],在这里我们从左至右由0开始编号,各个符号位的编号分别为0,1 ...
- 支付宝收款连接 非API
<a href="https://shenghuo.alipay.com/send/payment/fill.htm?_form_token=mMYOrAXfReOtBBCMmoaK7 ...
- openstack的控制节点部署
openstack的控制节点部署 主要是使用了本地安装的那个镜像. 会出现几个问题, 1.重启服务无法启动. 2.环境变量无法正确配置可以自己配置
- SRF之数据验证
实现表单输入数据的验证,包括客户端验证和服务器端验证 如何使用 数据验证在业务层的实体类字段上增加数据验证的特性,例如 public class User { [Required(ErrorMessa ...
- [笔记]--Ubuntu安装Sublime Text 2
sublime text 2 有两种安装方式,一种是添加软件源,然后用命令安装.另外一种是下载安装包.解压手动安装.Sublime Text 2 入门及技巧 一.下载安装 1.在Sublime Tex ...