Codility------CyclicRotation
A zero-indexed array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The goal is to rotate array A K times; that is, each element of A will be shifted to the right by K indexes.
Write a function:
class Solution { public int[] solution(int[] A, int K); }
that, given a zero-indexed array A consisting of N integers and an integer K, returns the array A rotated K times.
For example, given array A = [3, 8, 9, 7, 6] and K = 3, the function should return [9, 7, 6, 3, 8].
Assume that:
- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int[] solution(int[] A, int K) {
// write your code in Java SE 8
int length = A.length;
int temp[] = new int[length];
if(K<0 || K>100 || length == 0)
return temp;
if(length == 1)
return A;
for(int i=0; i< length; i++) {
temp[(i+K)%length] = A[i];
}
return temp;
}
}
Codility------CyclicRotation的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
随机推荐
- 【23.15%】【codeforces 703C】Chris and Road
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- PCI GXL学习之二次开发篇
作者:朱金灿 来源:http://blog.csdn.net/clever101 gxl的二次开发分初级和高级之分.初级是gxl提供了几百个模块供你编排成不同的作业.高级就是你可以编写你的算法模块,然 ...
- WPF 使用 SharpDX 在 D3DImage 显示
原文:WPF 使用 SharpDX 在 D3DImage 显示 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://linde ...
- Oracle 学习笔记 18 -- 存储函数和存储过程(PL/SQL子程序)
PL/SQL子程序 它包含了函数和过程.此功能是指用户定义的函数.和系统功能是不同的.子程序通常完成特定的功能PL/SQL座.,能够被不同的应用程序多次调用.Oracle提供能够把PL/SQL程序存储 ...
- 【转】Mybatis传多个参数(三种解决方案)
转自: http://www.2cto.com/database/201409/338155.html 据我目前接触到的传多个参数的方案有三种. 第一种方案: DAO层的函数方法 Public Use ...
- 整了一天,明白一个道理:线程里post数据,即loop.exec+quit,然而这个quit之后,导致无法在线程里建立新的loop.exec,直接就退出了
跟踪到exec的代码里,发现: 无奈,把第二个post移到主线程里去执行了. 如果大家发现有好办法,请告知我.
- 学习vi和vim编辑(4):高速移动定位
平时.第一步是编辑文本需要做将光标移动到需要编辑.因此,根据需要,将光标移动到目标数字键来编辑文本的速度在一定程度上. 一篇文章.主要介绍怎样高速移动光标. 依据屏幕来移动: 在一个有几千行文本的文件 ...
- Linux性能测试 top衍生命令 atop/htop/slaptop
1. Atop Atop 是一个类似 top 的工具,但比 top 更有料.通过 Atop,你能够监视 Linux 系统的性能状况,包括进程活动.CPU.内存.硬盘.网络等方面的使用情况等. 2. h ...
- 使用python3的base64编解码实现字符串的简易加密解密
import base64 copyright = 'Copyright (c) 2012 Doucube Inc. All rights reserved.' def main(): #转成byte ...
- easyui的datebox最简单的方法来格式化
看了网上有很多解决方案,我也写了一个比较简单的方法. 实现easyui的datebox格式化. 效果例如以下.用"++"隔开,看你喜欢用什么都能够. 1.html <span ...