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 ...
随机推荐
- [视频]物联网应用-ARM mbed-来至MultiTech Systems的解决方案
ARM公司面向物联网及可穿戴市场,近期可谓是动作频频,先是发布了专为物联网及可穿戴领域而生的Cortex-M7架构,接着又发布了mbed物联网操作系统.意图在物联网领域构筑一套坚不可摧的生态系统. 这 ...
- SMTP sendMail 失败解决办法
If you are seeing messages like this in your message log when running a process through the process ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- delphi构造&析构调用顺序
_ClassCreate ->Create ->AfterConstruction(->DoCreate / OnCreate) BeforeDestruction(->DoD ...
- DELPHI 单元文件结构
unit Unit1; interface {接口部分开始} uses {引用单元列表,这是可选的,如果包含必须紧跟interface关键字} {接口部分声明常量/类型/变量/过程和函数,这些声明对引 ...
- 自适应游标共享技术02(一个简单的例子来走近ACS)
为了不让其他因素干扰实验,参数设置如下: optimizer_mode=ALL_ROWS(使用CBO) optimizer_features_enable=11.2.0.3(使用最新的优化参数) op ...
- 批量kill进程
ps -e | grep java |awk '{print $1}'|xargs kill -9
- Linux之kernal分析与启动20160610
说一下LINUX内核的分析与启动: 一. 内核启动流程,据此配置内核(机器ID) 1.1 修改Makefile 1.2 选择默认配置 : make s3c2410_defconfig 1.3 make ...
- 第七节:使用实现了dispose模式的类型
知道类型如何实现dispose模式之后,接下来看一下开发人员怎样使用提供了dispose模式的类型.这里不再讨论前面的SafeHandle类,而是讨论更常用的FileStream类. 可以利用File ...