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 ...
随机推荐
- 获取本地ip地址
#import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { ...
- activiti搭建(三)整合Modeler
转载请注明源地址:http://www.cnblogs.com/lighten/p/5878169.html 接上一章,activiti工作流引擎虽然运行了起来,但是什么都没有.activiti官方在 ...
- ipv4、ipv6的socket同时监听“bind: Address already in use”的解决方法
创建ipv4和ipv6的socket,同时监听某个端口的ipv4和ipv6报文,运行时bind函数执行失败,提示“bind: Address already in use”.原因:ipv6的socke ...
- input文本框实现宽度自适应代码实例,input文本框
本章节介绍一下如何让一个文本框的宽度能够随着文本框中的内容的宽度增长而增长,也就是能够实现宽度自适应效果. 代码实例如下: <!DOCTYPE html> <html> < ...
- POJ C程序设计进阶 编程题#3:运算符判定
编程题#3:运算符判定 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 两个 ...
- 软件工程 speedsnail 第二次冲刺2
20150519 完成任务:划线第二天,能画出一条直黄线: 遇到问题: 问题1 划线的代码和移动的setcontentview冲突,无法同时显示 解决1 没有解决 明日任务: 线与移动共存
- SQL SERVER连接字符串学习
在使用connection string时遇到一些问题 字符串如下"Data Source= ******;Initial Catalog=******;Persist Security I ...
- MongoDB 3 + Windows 7 X64安装及配置
注册windows服务,使MongoDB自动启动 1.使用系统管理员运行cmd C:\Users\admin> d: C:\Users\admin> cd "mongodb的安装 ...
- HttpClient Post Form data and get Response String
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http:/ ...
- 定时重启Apache与MySQL方法
可以定时重启apache服务器等.让网站运行的效果更快. 采用at命令添加计划任务. 有关使用语法可以到window->“开始”->运行“cmd”->执行命令“at /”,这样界面中 ...