Careercup - Microsoft面试题 - 5684901156225024
2014-05-10 23:45
原题:
Arrange the numbers in an array in alternating order.
For example if the array is [a1, a2, a3, a4.. ]arrange the array such that b1<=b2>=b3<=b4 and so on.
Sampe Input:
Sample Output: < > < > <
题目:给定一个数组,请调整元素顺序,使得数组满足a <= b >= c <= d ...这样的交替单调性。
解法:之前也有类似题目,不过那一题要求严格的大于和小于。如果是大于等于和小于等于的话,可以保证在O(n)时间内完成算法。只要按顺序调整相邻元素就可以达到题目要求了。
代码:
// http://www.careercup.com/question?id=5684901156225024
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void arrangeArray(vector<int> &v) {
int n;
int i; n = (int)v.size();
if (n < ) {
return;
} int flag = ;
for (i = ; i < n - ; ++i) {
if (flag ? v[i] < v[i + ] : v[i] > v[i + ]) {
myswap(v[i], v[i + ]);
}
flag = !flag;
}
};
private:
void myswap(int &x, int &y)
{
int tmp = x;
x = y;
y = tmp;
}
}; int main()
{
int n;
int i;
vector<int> v;
Solution sol; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
sol.arrangeArray(v); for (i = ; i < n; ++i) {
cout << v[i];
cout << (i == n - ? '\n' : ' ');
} v.clear();
} return ;
}
Careercup - Microsoft面试题 - 5684901156225024的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- .NET环境配置(二)
打开IIS服务器 首先在设置程序池 应用程序池 设置 ASP.NET v4.0 ASP.NET v4.0 Classic CLassic.NET AppPool DefaultA ...
- Ubuntu系统使用记录(持续更新)
本篇文章记录在虚拟机上跑Ubuntu16.04遇到的一系列问题,熟悉一下Ubuntu的相关操作,进入终端的方法ctrl+alt+t. 1.修改屏幕分辨率,进入系统默认的是800x600 即便能够进入s ...
- 理解inode
转载:阮一峰 http://www.ruanyifeng.com/blog/2011/12/inode.html 一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最 ...
- Visitor
#include <iostream> #include <vector> using namespace std; #define DESTROY_POINTER(ptr) ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(九)-- 单元测试
本篇将结合这个系列的例子的基础上演示在Asp.Net Core里如何使用XUnit结合Moq进行单元测试,同时对整个项目进行集成测试. 第一部分.XUnit 修改 Project.json 文件内容, ...
- POI中设置Excel单元格格式样式(居中,字体,边框等)
创建sheet什么的就不多说了,直接进入正题 HSSFCellStyle cellStyle = wb.createCellStyle(); 一.设置背景色: cellStyle.setFillF ...
- wordpress学习-themes-001
这一篇主要是来记录wordpress theme的内容.关于为什么要自己编写wordpress theme的理由,相信大家都有各自的体会.想让自己的博客变的更加突出?更加个性话?wordpress t ...
- ajax使用。
<script> function createAjax(){ var request=false; //window对象中有XMLHttpRequest存在就是非IE,包括(IE7,IE ...
- Pycharm 使用 (一)
学习[Python基础教程]到后面的练习阶段就觉得python自带的IDLE有点out的感觉,于是就在网上搜索好用的IDE, 挺多人推荐Pycharm的 不仅跨平台而且还支持django等框架; 初次 ...
- jquery 匹配select下拉框与列表框
今天工作中用到 GrapyCity 的 wijmo ui 控件. 要给系统中所有类型的控件加统一样式 用法 $("input [type='text']").wijtext(); ...