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 ...
随机推荐
- Windows Phone 开发起步之旅之一 平台环境的搭建
最近大家都在写博客园的技术文章,按耐不住了,也把自己平时学习中遇到和学习到的一些东西写出来,供大家分享也好,自己留个纪念也好,有个可以查看的东西. 言归正传,随着微软对Windows Phone的投入 ...
- LTE参考资料
NETMANIAS TECHNICAL DOCUMENTS: http://www.netmanias.com/en/?m=board&id=techdocs
- 第一章:1-06、 试将TCP/IP和OSI的体系结构进行比较。讨论其异同之处?
<计算机网络>谢希仁著第四版课后习题答案答:(1)OSI和TCP/IP的相同点是二者均采用层次结构,而且都是按功能分层.(2)OSI和TCP/IP的不同点:①OSI分七层,自下而上分为物理 ...
- js日期格式化方法 dateFormatFn
var dateFormatFn=function(val,fmt){ var _this = new Date(val); console.log(_this,_this.getFullYear() ...
- Mongodb添加地理位置索引
1.同步站点信息到mongo中(支持mysql.sqlserver数据同步) 2.在Collections文件夹下所在文档右键,在菜单中选择Add Index, 3.然后进行数据查询{ "m ...
- linux iostat命令详解 磁盘操作监控工具
Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...
- php中的一些小细节(1)
<?php $a=false; echo $a; //var_dump($a); ?> 输出结果为: (即为空): 为什么会这样子? 查看官网对echo的相关资料得出: 结论:ec ...
- ajax+php+mysql更新
html代码 <input type="button" id="quxiao" class="quxiao" name="q ...
- Android Socket通信
1.TCP: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- android使用library工程问题
在windows系统下,library project必须和project处于相同的盘符中,因为如果在不同盘符,project.properties中的android.library.referenc ...