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) 额外空间并 ...
随机推荐
- 【转】Spark:Master High Availability(HA)高可用配置的2种实现
原博文出自于: 感谢! Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个 ...
- Spring Boot (30) 上传文件
文件上传 上传文件和下载文件是Java Web中常见的一种操作,文件上传主要是将文件通过IO流传输到服务器的某一个文件夹下. 导入依赖 在pom.xml中添加上spring-boot-starter- ...
- 专题七:UDP编程补充——UDP广播程序的实现
一.程序实现 UDP广播程序的实现代码: using System; using System.Net; using System.Net.Sockets; using System.Text; us ...
- WordPress极简主题Small Cat详细介绍
主题特性: HTML5.CSS3 使用标准语言编写,支持IE10以上浏览器 响应式 在桌面.平板.手机端均以最佳状态显示.也可分享到微信显示哦! 自定义 超过60多个后台自定义设置,让你的站点与众不同 ...
- APP上线被APPStore拒绝的各种原因
1.程序有重大bug,程序不能启动,或者中途退出.2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币.3.游戏里有实物奖励的话,一定要说清楚,奖励由本公司负责,和苹果没有关系.4.用到苹果的标志 ...
- 【C++】智能指针简述(四):shared_ptr
在开始本文内容之前,我们再来总结一下,前文内容: 1.智能指针采用RAII机制,在构造对象时进行资源的初始化,析构对象时进行资源的清理及汕尾. 2.auto_ptr防止拷贝后析构释放同一块内存,采用& ...
- Hive扩展功能(二)--HWI接口
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- 【译】x86程序员手册25-7.1任务状态段
7.1 Task State Segment 任务状态段 All the information the processor needs in order to manage a task is st ...
- Xamarin绑定ios静态库
以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...
- maven引入的包无法使用 解决方法
如果正常引入后在依赖中能够找到包,但是打不开 1.有可能是包下载不完整 把maven下载源由国外转成阿里镜像源找到 maven 的配置文件: settings.xml 文件: <mirror&g ...