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 ...
随机推荐
- CentOS 5.5 下安装Countly Web Server过程记录
CentOS 5.5 下安装Countly Web Server过程记录 1. 系统更新与中文语言包安装 2. 基本环境配置: 2.1. NodeJS安装 依赖项安装 yum -y install g ...
- c/c++基本问题
1. 使用g++将文件编译成库文件 g++ -c -O2 -fPIC test.cpp -o test.o && g++ -shared -Wall -o test.so test.o ...
- C#中判断文件夹中存在某个txt文本
strFileName="D:\\strarray.txt"; if (File.Exists(strFileName))//判断文件是否存在 { }
- centos6.7下网络设置
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static" # ...
- 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 …… ...
- DBGridEh 点击表头排序方法
方法1: (不用编程写代码) 程序中引用 单元 EhLibCDS设置DBGridEh的属性: ColumnDefValues.Title.TitleButton = True Op ...
- c语言内存分配-malloc
malloc 原型:(原来返回类型是char) extern void *malloc(unsigned int num_bytes); 头文件: #include <stdlib.h> ...
- 【Django】Apache上运行多个Django项目
运行单个项目的步骤参考:这里 1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 > ...
- WPF 多项选择下拉菜单
背景 项目中有一个多项选择筛选的功能, 由于筛选条件太多, 用户又习惯在平板上进行操作, 所以要求我们把checkbox 放到一个combobox里面, 然后checkbox的选项要在combobox ...
- UIViewAnimationOptions swift 2
UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...