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) 额外空间并 ...
随机推荐
- CircuitBreaker design pattern---reference
It's common for software systems to make remote calls to software running in different processes, pr ...
- Hive insert into directory 命令输出的文件没有列分隔符分析和解决
参考资料:http://stackoverflow.com/questions/16459790/hive-insert-overwrite-directory-command-output-is-n ...
- 致创业者:APP已死 服务永生
前几日,有位创业者和我讲他在带领团队做一个将爱踢球的人集中在一起的App,我告诉他你的创业方向错了.原因在于你的目的是要为爱踢球的人提供服务,而你现在却在竭尽全力的做App,你应该做的是设计你为爱踢球 ...
- CF848A From Y to Y
思路1: 每次贪心地选择满足i * (i - 1) / 2 <= k最大的i并从k中减去i * (i - 1) / 2,直至k为0.由于函数x * (x - 1) / 2的增长速度比2x要慢,所 ...
- opencv边缘滤波
2018-03-0422:16:11 import cv2 as cv import numpy as np def bi_demo (image): print ("ceshi" ...
- [Windows Server 2008] IP安全策略限制端口方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:限制143 ...
- Linux终端常用快捷操作
命令或文件名自动补全:在输入命令或文件名的前几个字母后,按Tab键,系统会自动补全或提示补全 上下箭头:使用上下箭头可以回溯之前的命令,增加命令的重用,减少输入工作量 !加之前输入过的命令的前几个字母 ...
- 在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法
在网页中引用DWG控件,交互绘图,和响应鼠标点击对象的方法 [MXDRAW CAD控件文档] 下面帮助的完整例子,在控件安装目录的Sample\Ie\iedemo.htm中. 1. 主要用到 ...
- JDBC配置MSSQL
使用JDBC连接SQL SERVER 这可能是个很老套的话题,但不管怎么说还是有用的.姑且把配置方法贴出来吧.1. 确认Sql Server的的运行状态打开Sql Server配置管理器,确认Sql ...
- 校内测之zay与银临 (day2)(只有T1)
一些与题目无关的碎碎念 推出式子来一定要化简!!!freopen不要写错!!!特判不要瞎搞!!!! 做到以上三点能高35分qwq T1 江城唱晚 你看数据那么大,显然又是一道数学题. 这里有n个种海棠 ...