1. 数组小挪移CyclicRotation Rotate an array to the right by a given number of steps.
数组小挪移;
package com.code; import java.util.Arrays; public class Test02_2 { public int[] solution(int[] A, int K) {
int size = A.length;
if(size < 2){
return A;
}
int [] res = new int[size];
for(int i=0;i<size;i++){
res[(i+K)%size] = A[i];
}
return res;
}
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
Test02_2 t02 = new Test02_2();
int[] s01 = t02.solution(a, 3);
System.out.println(Arrays.toString(s01));
}
} /** 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.
*/
1. 数组小挪移CyclicRotation Rotate an array to the right by a given number of steps.的更多相关文章
- 189. Rotate Array -- 将数组前一半移到后一半
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- c/c++ 模板与STL小例子系列<一 >自建Array数组
c/c++ 模板与STL小例子系列 自建Array数组 自建的Array数组,提供如下对外接口 方法 功能描述 Array() 无参数构造方法,构造元素个数为模板参数个的数组 Array(int le ...
- [Swift]LeetCode453. 最小移动次数使数组元素相等 | Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- js小练习-移除数组中的元素
移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 代码: <!DOCTYPE HTML><html> <he ...
- js回调函数,字符串,数组小析
(一)回调函数:是指通过函数参数传递到其他代码的,某一块可执行代码的引用.这一设计允许了底层代码调用在高层定义的子程序.在抖动函数中,回调函数用于在实现一些功能之后采取的另外的措施,比如div,照片抖 ...
- 差分数组|小a的轰炸游戏-牛客317E
小a的轰炸游戏 题目链接:https://ac.nowcoder.com/acm/contest/317/E 思路 这题考查的是对差分数组原理和前缀和的理解. 四个数组分别记录朝着四个方向下放的个数 ...
- c语言数组小谈
#include <stdio.h> #include <stdlib.h> #define N 5 int main() { double score[5]; int i; ...
- iOS 数组遍历过程中移除
参考:https://blog.csdn.net/u011619283/article/details/53135502 常见crash 原因是数组在移除元素后,继续遍历会有越界问题. 解决思路: 遍 ...
- 【数组】leetcode——移除元素
编号:27. 移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度. 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 ...
随机推荐
- 关于sql的case when用法简述
刚入手公司项目,需要添加一个功能,用到了SQL的case when以及concat SELECT eve.cc, eve.sc, case concat(cc,sc) ' THEN '' ' THEN ...
- 为什么我的对象被 IntelliJ IDEA 悄悄的修改了?
背景 最近,在复习JUC的时候调试了一把ConcurrentLinkedQueue的offer方法,意外的发现Idea在debug模式下竟然会 "自动修改" 已经创建的Ja ...
- js实现左右点击图片层叠滚动特效
需要加载js有 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></s ...
- [ Luogu 4917 ] 天守阁的地板
\(\\\) \(Description\) 定义二元函数\(F(x,y)\)表示,用 \(x\times y\) 的矩形不可旋转的铺成一个任意边长的正方形,所需要的最少的矩形个数. 现在\(T\)组 ...
- PAT 甲级1135. Is It A Red-Black Tree (30)
链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...
- python学习笔记(5)—— tuple 本质探究
>>> t=(1,2,3,['a','b','c'],4,5) >>> t[3][0]='x' >>> t (1, 2, 3, ['x', 'b' ...
- HDU_1072_Nightmare
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需 ...
- CAD从二制流数据中加载图形(com接口Delphi语言)
主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...
- RabbitMQ系列(八)--顺序消费模式和迅速消息发送模式
MQ使用过程中,有些业务场景需要我们保证顺序消费,而如果一个Producer,一个Queue,多个Consumer的情况下是无法保证顺序的 举例: 1.业务上产生三条消息,分别是对数据的增加.修改.删 ...
- Ubuntu搭建LAMP开发环境
1.安装Apache sudo apt-get install apache2 测试: 浏览器访问 (如:http://localhost),出现It Works!网页. 查看状态: service ...