2014-05-11 03:56

题目链接

原题:

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

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

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

代码:

 // http://www.careercup.com/question?id=6282862240202752
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std; void reverse(vector<int> &v, int ll, int rr)
{
int n = (int)v.size(); if (ll > rr) {
swap(ll, rr);
}
if (ll < || ll > n - || rr < || rr > n - ) {
return;
}
while (ll < rr) {
swap(v[ll], v[rr]);
++ll;
--rr;
}
} void rightShift(vector<int> &v, int k)
{
int n = (int)v.size(); if (n == ) {
return;
}
k = k >= ? k % n : (n - (n - k) % n) % n;
if (k == ) {
return;
}
reverse(v, , n - k - );
reverse(v, n - k, n - );
reverse(v, , n - );
} int main()
{
vector<int> v;
int n, k;
int i; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
cin >> k;
rightShift(v, k);
for (i = ; i < n; ++i) {
i ? (cout << ' '), : ;
cout << v[i];
}
cout << endl; v.clear();
} return ;
}

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. 判断jquery是否已经加载,如果没有动态加载

    方法一: // Only do anything if jQuery isn't defined if (typeof jQuery == 'undefined') { if (typeof $ == ...

  2. logcat保存当前应用程序的日志并上传服务器或指定邮箱

    给大家分享一个项目中用到的日志统计并提交服务器的日志工具类.通过过得当前app的PID,采用命令行的方式实用logcat工具过滤日志.代码区: package org.and.util; import ...

  3. ASP.NET MVC SignalR(1):背景

    系列目录:ASP.NET MVC SignalR 关键词:HTTP.轮询.WebSocket.Server-Sent Events.长轮询.forever frame. 1. HTTP HTTP(Hy ...

  4. POJ C++程序设计 编程作业—类和对象 编程题#1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序输出的结 ...

  5. Copying Rowsets

    I find that you often need to create and manipulate standalone rowsets. Sometimes you can get the da ...

  6. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...

  7. 索尼MT27i Android2.3.7 线刷Android4.04

    Author:KillerLegend From:http://www.cnblogs.com/killerlegend/p/3733150.html Date:2014.5.16 工具:Window ...

  8. keytool 获取证书信息

    keytool -list -v -keystore E:\androidkestore 红色部分为证书地址 输入密码后得到md5及sha1 google地图和百度地图申请key可能用到

  9. How to using to code import to GL journal[AX2012]

    static void THK_importLedgerJournalTrans(Args _args) { Filename fileName = "C:\\Users\\ksiu3880 ...

  10. ListView与.FindControl()方法的简单练习 #2 -- ItemUpdting事件中抓取「修改后」的值

    原文出處  http://www.dotblogs.com.tw/mis2000lab/archive/2013/06/24/listview_itemupdating_findcontrol_201 ...