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 ...
随机推荐
- ASP.NET MVC5 入门
参考资料<ASP.NET MVC5 高级编程>第5版 第1章 入门 1.1 ASP.NET MVC 简介 ASP.NET MVC是一种构建Web 应用程序的框架,它将一般的MVC(Mode ...
- PHP中数据库的连接
<?php //1.链接MySQL服务器 $conn = mysql_connect("localhost", "root" , 199452); //2 ...
- php简单缓存类
<?phpclass Cache { private $cache_path;//path for the cache private $cache_expire;//seconds ...
- nginx 解决400 bad request 的方法(转载)
nginx的400错误比较难查找原因,因为此错误并不是每次都会出现的,另外,出现错误的时候,通常在浏览器和日志里看不到任何有关提示. 经长时间观察和大量试验查明,此乃request header过大所 ...
- 一款jQuery实现重力弹动模拟效果特效,弹弹弹,弹走IE6
一款jQuery实现重力弹动模拟效果特效 鼠标经过两块黑色div中间的红色线时,下方的黑快会突然掉落, 并在掉落地上那一刻出现了弹跳的jquery特效效果,非常不错,还兼容所有的浏览器, 适用浏览器: ...
- ViewPager使用 -------滑动图片
package com.zzw.viewpage; import java.util.ArrayList; import java.util.List; import android.app.Acti ...
- 从PC跳转至wap
<script language="JavaScript">function mobile_device_detect(url){var thisOS=navigato ...
- delphi 更改不了窗体的标题
delphi定义变量名千万要注意,不能和关键字同名,今天我无意间定义了一个caption的变量 导致我怎么都不能修改窗的标题.
- DBGridEh 点击表头排序方法
方法1: (不用编程写代码) 程序中引用 单元 EhLibCDS设置DBGridEh的属性: ColumnDefValues.Title.TitleButton = True Op ...
- 蒙牛乳业六厂—第一家MES工厂
在上海西门子工业自动化(SIAS)与蒙牛液态奶事业部以及蒙牛集团信息中心的共同努力下,经过项目组成员1年半时间的具体实施,中国乳品行业第一个真正意义上的生产执行系统MES,于2008年6月在蒙牛乳业集 ...