Careercup - Microsoft面试题 - 6282862240202752
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的更多相关文章
- 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面试题 - 5428361417457664
2014-05-11 03:37 题目链接 原题: You have three jars filled with candies. One jar is filled with banana can ...
随机推荐
- 捣蛋phpwind之WindFrameWork
一直都有关注phpwind这个开源产品,从9.0开始就好关注拉,因为官方说把之前的代码重写了一遍,融入了windFramework这个框架,代码真的挺优美的,今日在做社区的一些功能,心血来潮就参考了p ...
- webserver 发布问题
1:web.config <system.web> <compilation debug="true" targetFramework="4. ...
- Check Box Select/Deselect All on Grid
The below function is to be used on a grid with multiple check boxes. Place the code behind a FieldC ...
- C#winform导出数据到Excel的类
/// <summary> /// 构造函数 /// </summary> public ExportData() { } /// <summary> /// 保存 ...
- jquery源码学习--iceDog
/** * @FileName : iceDog * @Author : PheonixHkbxoic * @Mail : hkbxoic@gmail.com * @DateTime : 2016-0 ...
- mariadb DML语句及用户授权
DML(Data Manipulation Language):INSERT, DELETE, UPDATE, SELECT INSERT [INTO] tbl_name [(col1,...) ...
- 仿酒仙网的一款jQuery侧栏弹出导航栏特效
仿酒仙网的一款jQuery侧栏弹出导航栏特效 一款常用于商城左侧商品导航的jquery菜单导航特效. 非常不错的一款商品分类特效.大家可以拿去研究研究 . 注意:该特效还支持挨千刀的IE6啊,之强大. ...
- PHP通过字符串调用函数
1. call_user_func function a($b,$c){ echo $b; echo $c; } call_user_func('a', "111","2 ...
- 自定义DatePicker,年月日,不显示其中某几项
经过源码研究:该结构主要包含三个NumberPicker: private final NumberPicker mDayPicker; private final NumberPicker mMon ...
- Delphi 2010初体验,是时候抛弃Delphi 7了
Delphi 2010已于近日由Embarcadero公司发布.作者Kim Madsen作为一名资深的Delphi开发者,在他的博客中谈到了Delphi 2010的新性能.它的使用感受以及对Delph ...