数组小挪移;

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.的更多相关文章

  1. 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 ...

  2. c/c++ 模板与STL小例子系列<一 >自建Array数组

    c/c++ 模板与STL小例子系列 自建Array数组 自建的Array数组,提供如下对外接口 方法 功能描述 Array() 无参数构造方法,构造元素个数为模板参数个的数组 Array(int le ...

  3. [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 ...

  4. js小练习-移除数组中的元素

    移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 代码: <!DOCTYPE HTML><html>    <he ...

  5. js回调函数,字符串,数组小析

    (一)回调函数:是指通过函数参数传递到其他代码的,某一块可执行代码的引用.这一设计允许了底层代码调用在高层定义的子程序.在抖动函数中,回调函数用于在实现一些功能之后采取的另外的措施,比如div,照片抖 ...

  6. 差分数组|小a的轰炸游戏-牛客317E

    小a的轰炸游戏 题目链接:https://ac.nowcoder.com/acm/contest/317/E 思路  这题考查的是对差分数组原理和前缀和的理解. 四个数组分别记录朝着四个方向下放的个数 ...

  7. c语言数组小谈

    #include <stdio.h> #include <stdlib.h> #define N 5 int main() { double score[5]; int i; ...

  8. iOS 数组遍历过程中移除

    参考:https://blog.csdn.net/u011619283/article/details/53135502 常见crash 原因是数组在移除元素后,继续遍历会有越界问题. 解决思路: 遍 ...

  9. 【数组】leetcode——移除元素

    编号:27. 移除元素 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度. 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 ...

随机推荐

  1. js jquery 获取服务器控件的三种方法

    由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,google了下,总结有以下3种方法: 服务器控件代码:<asp:TextBox ID=" ...

  2. NHibernate学习笔记(3)-实体反射到数据库

    一.开发环境 NHiberate版本:4.0.4 开发工具:VS2013 数据库:SQLServer2012 二.开发流程 1.编写领域类与映射文件 namespace Domain { public ...

  3. python与arduino串口通讯对接opencv实现智能物品分拣

    2018-05-0118:53:50 先上图: 再来视频: http://v.youku.com/v_show/id_XMzU3NzAwNzMyNA==.html?spm=a2hzp.8244740. ...

  4. Python学习日记之中文支持

    解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可

  5. oracle数据库定时备份

    现有需求,设计一批处理文件,实现周一全部备份,周二周三周五周六增量备份,周四周日累计备份.并且每日将上个月本日数据删除(如今日8月7号,则删除7月7号的数据).并将备份压缩以减小所占空间. 思路: 备 ...

  6. CAD把当前图形保为一个jpg文件(com接口Delphi语言)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 procedure TForm2.Button1Click(Sender: TObje ...

  7. 【转】上传并解析excel

    [转载自]http://blog.csdn.net/u011563331/article/details/51322523 通过解析excel,将数据存储到数据库中.现在将方法保存下来. 使用的是ap ...

  8. 05网页<div></div>块内容

    网页<div></div>块内容 <header>此处为新 header 标签的内容</header> <navigation>此处为新 n ...

  9. cin的返回对象

    //有时间回来补坑 //先记上几个有用的博客 https://blog.csdn.net/candj/article/details/4419585https://www.cnblogs.com/gy ...

  10. 为什么map对象不能使用stl中的sort函数

    STL所提供的各式各样算法中,sort()是最复杂最庞大的一个.这个算法接受两个RandomAccestlerators(随机存取迭代器),然后将区间内的所有元素以渐增方式由小到大重新排列.第二个版本 ...