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 ...
随机推荐
- .NET中的计时器控件Timer
本章借介绍一些粗浅的Timer控件使用方法. 介绍Timer控件的常用属性和事件 1. Interval 属性表示 Timer控件的时间间隔. 类型是int默认是毫秒. 2. Enabled 属性 表 ...
- JS数组(Array)处理函数总结
1.concat() 连接两个或更多的数组该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本.例如: <script type="text/javascript"&g ...
- HTML5应用之时钟
利用HTML5的Canvas API可以完成我们以前意想不到的动画效果,以前我们想在网页上放置一个时钟,需要先用flash工具制作一个钟表,并写上复杂的JavaScript代码,然后载入到页面中.而H ...
- canvas 绘制矩形和圆形
canvas绘制有两神方法:1).填充(fill)填充是将图形内部填满. 2).绘制边框 (stroke)绘制边框是不把图形内部填满,只是绘制图形的外框. 当我们在绘制图形的时候,首先要设定好绘制的样 ...
- Java 中的构造方法
首先创建一个Transport类,定义好类的属性和方法,并且写好构造方法,先看下无参数的构造方法: public class Transport { //名字 public String name; ...
- 百度 迷你版 UMeditor富文本编辑器 使用方法
第一步:下载编辑器 到官网下载 umeditor 最新版源码版本,下载之后打开 _examples/index.html 就可以看到演示例子.[下载页面] 第二步:部署编辑器到页面 解压下载的包,放到 ...
- Kafka入门学习(一)
====常用开源分布式消息系统 *集群:多台机器组成的系统叫集群. *ActiveMQ还是支持JMS的一种消息中间件. *阿里巴巴metaq,rocketmq都有kafka的影子. *kafka的动态 ...
- Learning Scrapy笔记(一)- Scrapy简单介绍
Scrapy简述 Scrapy十一个健壮的,用来从互联网上抓取数据的web框架,Scrapy只需要一个配置文件就能组合各种组件和配置选项,并且Scrapy是基于事件(event-based)的架构,使 ...
- Python脚本控制的WebDriver 常用操作 <六> 打印当前页面的title及url
下面将使用WebDriver来答应浏览器页面的title和访问的地址信息 测试用例场景 测试中,访问1个页面然后判断其title是否符合预期是很常见的1个用例: 假设1个页面的title应该是'hel ...
- WPF:定制Checkbox样式,让“正确”绿得好看,让“错误”红的显眼
WPF提供了样式.模板.触发器.状态管理.矢量形状等方式,让我们不需要背景图片,也可以轻松定制控件的风格样式.下面是笔者针对Checkbox进行的样式定制,让“正确”绿得好看,让“错误”红的显眼. ...