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 ...
随机推荐
- Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed.
地图定位 错误:使用CoreLocation获取地理位置信息,报错 Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be ...
- IOS懒人笔记应用源码
这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...
- Jquery入门之---------基本事件------------
Javascript有一个非常重要的功能,就是事件驱动. 当页面完成加载后,用户通过鼠标或键盘触发页面中绑定事件的元素即可触发.Jquery为开发者更有效率的编写事件行为,封装了大量有益的事件方法供我 ...
- UISearchBar去除背景颜色
UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:frame]; //这个设置背景透明可能无效 searchBar.background ...
- POJ C++程序设计 编程题#1 编程作业—多态与虚函数
编程题 #1 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 下面程序的输出结果是: ...
- CentOS下修复grub引导程序
1.早上装系统,试了好几次.最终grub修复成功! 解决方法大致如下: 开机遇见 grub>的时候 下面看解决方法 首先是既然插上U盘能进系统 那我们插上U盘 正常进入系统 然后看操作 [r ...
- C# @Page指令中的AutoEventWireup,CodeBehind,Inherits
AutoEventWireup 如果 Page 指令的 AutoEventWireup 属性被设置为 true(或者如果缺少此属性,因为它默认为 true) ,该页框架将自动调用页事件,即 Page_ ...
- c语言学习第四天数据类型1
int 代表整数,它在内存中占4个字节,二进制的表示方式是占用了三十二位,二进制中只包含0和1,那它的最大值就是全为1,但int是 有符号类型,所以最高位(左边的第一位)要拿出来做符号位,这样就只 ...
- Android Material Design:基于CoordinatorLayout实现向上滚动导航条ToolBar滚出、向下滚动导航条滚出
activity_main.xml: <android.support.design.widget.CoordinatorLayout xmlns:android="http://sc ...
- pure的bug记录2
<select id="stacked-state" style=" font-family: "Microsoft YaHei"; " ...