2014-05-10 23:45

题目链接

原题:

  1. Arrange the numbers in an array in alternating order.
  2. For example if the array is [a1, a2, a3, a4.. ]arrange the array such that b1<=b2>=b3<=b4 and so on.
  3. Sampe Input:
  4. Sample Output: < > < > <

题目:给定一个数组,请调整元素顺序,使得数组满足a <= b >= c <= d ...这样的交替单调性。

解法:之前也有类似题目,不过那一题要求严格的大于和小于。如果是大于等于和小于等于的话,可以保证在O(n)时间内完成算法。只要按顺序调整相邻元素就可以达到题目要求了。

代码:

  1. // http://www.careercup.com/question?id=5684901156225024
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. class Solution {
  8. public:
  9. void arrangeArray(vector<int> &v) {
  10. int n;
  11. int i;
  12.  
  13. n = (int)v.size();
  14. if (n < ) {
  15. return;
  16. }
  17.  
  18. int flag = ;
  19. for (i = ; i < n - ; ++i) {
  20. if (flag ? v[i] < v[i + ] : v[i] > v[i + ]) {
  21. myswap(v[i], v[i + ]);
  22. }
  23. flag = !flag;
  24. }
  25. };
  26. private:
  27. void myswap(int &x, int &y)
  28. {
  29. int tmp = x;
  30. x = y;
  31. y = tmp;
  32. }
  33. };
  34.  
  35. int main()
  36. {
  37. int n;
  38. int i;
  39. vector<int> v;
  40. Solution sol;
  41.  
  42. while (cin >> n && n > ) {
  43. v.resize(n);
  44. for (i = ; i < n; ++i) {
  45. cin >> v[i];
  46. }
  47. sol.arrangeArray(v);
  48.  
  49. for (i = ; i < n; ++i) {
  50. cout << v[i];
  51. cout << (i == n - ? '\n' : ' ');
  52. }
  53.  
  54. v.clear();
  55. }
  56.  
  57. return ;
  58. }

Careercup - Microsoft面试题 - 5684901156225024的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. 一步一步学习SignalR进行实时通信_1_简单介绍

    一步一步学习SignalR进行实时通信\_1_简单介绍 SignalR 一步一步学习SignalR进行实时通信_1_简单介绍 前言 SignalR介绍 支持的平台 相关说明 OWIN 结束语 参考文献 ...

  2. HTML 表单总结http://images2015.cnblogs.com/blog/1001203/201607/1001203-20160730200559841-2144892373.png

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. foreach 和 list.foreach 初步测试

    单纯从速度上讲 小数据量下foreach 较快,list.Foreach 由于 public void ForEach(Action<T> action) { ; i <this._ ...

  4. 【转】详解JavaScript中的this

    ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...

  5. map与set的遍历

    map有四种方式: 1.直接遍历 keySet 2.使用Iterator //注意next放回的对象是map.Entry<K,V>,而使用的iterator是通过entrySet返回的一个 ...

  6. SqlBulkCopy 插入100W条数据时 属性BatchSize的作用

    (1)100W条insert语句在一个连接内一句一句加 花了01:17:19.0542805 (2) SqlBulkCopy 插入100W条数据 设置BatchSize=500 耗时:00:03:29 ...

  7. “猜你喜欢”是怎么猜中你心思的?

    文/Joseph A. Konstan & John Riedl)如今,到网上购物的人已经习惯了收到系统为他们做出的个性化推荐.Netflix 会推荐你可能会喜欢看的视频.TiVo 会自动把节 ...

  8. poj 1679 The Unique MST

    题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...

  9. JSON对象和String之间的互转及处理

    如题,本文列举了一些在web前端开发中常用的转换及处理方式.使用JSON而不是字符串,主要是为了方便处理. JSON:JavaScript 对象表示法(JavaScript Object Notati ...

  10. 如何从官网下载springframework和document

    spring官网 http://spring.io/ --->spring project--->点击github图标 --->artifactory --->进入到了http ...