2014-05-11 03:56

题目链接

原题:

  1. Given an integer array. Perform circular right shift by n.
  2. Give the best solution.

题目:给数组进行循环移位,给出最优解。

解法:首先要考虑n的范围,对于负数和超过数组长度的数,先进行取模操作。然后用三次反转数组就可以完成循环移位。

代码:

  1. // http://www.careercup.com/question?id=6282862240202752
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. void reverse(vector<int> &v, int ll, int rr)
  8. {
  9. int n = (int)v.size();
  10.  
  11. if (ll > rr) {
  12. swap(ll, rr);
  13. }
  14. if (ll < || ll > n - || rr < || rr > n - ) {
  15. return;
  16. }
  17. while (ll < rr) {
  18. swap(v[ll], v[rr]);
  19. ++ll;
  20. --rr;
  21. }
  22. }
  23.  
  24. void rightShift(vector<int> &v, int k)
  25. {
  26. int n = (int)v.size();
  27.  
  28. if (n == ) {
  29. return;
  30. }
  31. k = k >= ? k % n : (n - (n - k) % n) % n;
  32. if (k == ) {
  33. return;
  34. }
  35. reverse(v, , n - k - );
  36. reverse(v, n - k, n - );
  37. reverse(v, , n - );
  38. }
  39.  
  40. int main()
  41. {
  42. vector<int> v;
  43. int n, k;
  44. int i;
  45.  
  46. while (cin >> n && n > ) {
  47. v.resize(n);
  48. for (i = ; i < n; ++i) {
  49. cin >> v[i];
  50. }
  51. cin >> k;
  52. rightShift(v, k);
  53. for (i = ; i < n; ++i) {
  54. i ? (cout << ' '), : ;
  55. cout << v[i];
  56. }
  57. cout << endl;
  58.  
  59. v.clear();
  60. }
  61.  
  62. return ;
  63. }

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

  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面试题 - 5428361417457664

    2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...

随机推荐

  1. CentOS 5.5 下安装Countly Web Server过程记录

    CentOS 5.5 下安装Countly Web Server过程记录 1. 系统更新与中文语言包安装 2. 基本环境配置: 2.1. NodeJS安装 依赖项安装 yum -y install g ...

  2. c/c++基本问题

    1. 使用g++将文件编译成库文件 g++ -c -O2 -fPIC test.cpp -o test.o && g++ -shared -Wall -o test.so test.o ...

  3. C#中判断文件夹中存在某个txt文本

    strFileName="D:\\strarray.txt"; if (File.Exists(strFileName))//判断文件是否存在 { }

  4. centos6.7下网络设置

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static"   # ...

  5. C++求斐波那契数

    题目内容:斐波那契数定义为:f(0)=0,f(1)=1,f(n)=f(n-1)+f(n-2)(n>1且n为整数) 如果写出菲氏数列,则应该是: 0 1 1 2 3 5 8 13 21 34 …… ...

  6. DBGridEh 点击表头排序方法

    方法1: (不用编程写代码) 程序中引用 单元 EhLibCDS设置DBGridEh的属性:      ColumnDefValues.Title.TitleButton = True      Op ...

  7. c语言内存分配-malloc

    malloc 原型:(原来返回类型是char) extern void *malloc(unsigned int num_bytes); 头文件: #include <stdlib.h> ...

  8. 【Django】Apache上运行多个Django项目

    运行单个项目的步骤参考:这里 1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 > ...

  9. WPF 多项选择下拉菜单

    背景 项目中有一个多项选择筛选的功能, 由于筛选条件太多, 用户又习惯在平板上进行操作, 所以要求我们把checkbox 放到一个combobox里面, 然后checkbox的选项要在combobox ...

  10. UIViewAnimationOptions swift 2

    UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...