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整除的数字,那么就计算一下就能得到. ...
随机推荐
- AndroidStudio使用properties资源文件
在Android项目开发中,为了一些公用资源使用方便,可以在assets资源文件夹中将需要用到的资源写成.properties或者.json的文件形式,并进行读取使用.在做html5+javascri ...
- JSP和Servlet学习笔记1 - 访问配置
1. 访问 WebContent 目录下的 JSP 文件 在 WebContent 目录下的文件可以直接在浏览器中访问.新建一个 test.jsp 文件 <%@ page language=&q ...
- 获取web.config配置文件的sectionGroup
1)web.config 文件内容如下: <configuration> <configSections> <sectionGroup name="KaiXin ...
- vue 使用ztree
1 全局引入jquery , 不明白的看我上一个的随笔 , 特别简单 2 我没有封装组件 , 项目里面这个效果的只用了一次 , 没有必要 在你的<script>标签下面引入这俩东西 , 前 ...
- 分布式高级(十三)Docker Container之间的数据共享
sudo docker run -it -v /usr/lib:/usr/lib/dbdata --name dbcontainer-192.168.1.184 ubuntu:14.04 sudo d ...
- DDD实战8_2 利用Unity依赖注入,实现接口对应实现类的可配置
1.在Util类库下新建DIService类 /// <summary> /// 创建一个类,对应在配置文件中配置的DIServices里面的对象的 key /// </summar ...
- 编码(encode)问题
1. UTF-8 与 GBK UTF-8: 允许含 BOM,但通常不含 BOM 用以解决国际上字符的一种多字节编码, 英文:8 bits(1 byte) 中文:24 bits(3 bytes) UTF ...
- matlab 工具函数 —— normalize(归一化数据)
function x = normalize(x, mu, sigma) x = bsxfun(@minus, x, mu); x = bsxfun(@rdivide, x, sigma); end ...
- ASP.NET Core 使用 EF 框架查询数据 - ASP.NET Core 基础教程 - 简单教程,简单编程
原文:ASP.NET Core 使用 EF 框架查询数据 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 使用 EF 框架查询数据 上一章节我们学习了如何设置 ...
- VCL to UniGUI Migration Wizard
Free Evaluation Edition of The Automatic Migration Scripting Wizard For Converting Legacy Delphi Cod ...