当前位置:网站首页>LeetCode Daily 2 Questions 01: Rotating Arrays (both 1200 questions)
LeetCode Daily 2 Questions 01: Rotating Arrays (both 1200 questions)
2022-08-07 15:38:00 【The man was fishing for snow in the cold river alone.】
The title is as follows

Problem solving idea: We can use an extra array toPut each element in the correct position.Let nn represent the length of the array, we traverse the original array, put the element with the subscript ii of the original array to the position of the new array subscript (i+k)\bmod n(i+k)modn, and finally copy the new arrayto the original array.
public void rotate(int[] nums, int k) {int n = nums.length;//7 [1,2,3,4,5,6,7] k=3 [5,6,7,1,2,3,4]int[] newArr = new int[n];//new arrayfor (int i = 0; i <n; ++i) {//0 1 2 3 4 5 6 n=7newArr[(i + k) % n] =span> nums[i];//Array in newArr = Swap array positions in newArr}System.arraycopy(newArr, 0, nums, 0, n);//Copy the value in index newArr from zero index to zero in numsIndex and copy length 7} 边栏推荐
猜你喜欢

小技巧——postman生成在线文档

IPV6的内容
![leetcode: 636. Exclusive time of function [stack simulation]](/img/14/f10a1beba2d32cdf1152e07d2e7dc3.png)
leetcode: 636. Exclusive time of function [stack simulation]

亚马逊云科技 Build On 参与心得

JWT的创建

Short note_Altium Designer layout and routing reuse of the same module

(路透社数据集)新闻分类:多分类问题实战

JVM

06 【Generic】

Chapter 3 Mobile Terminal - Elementary Classroom Online Education System Effect Demonstration and Technical Preparation
随机推荐
ETCD Quick Start - 01 ETCD Overview
06 【泛型】
AQS同步组件-Semaphore(信号量)解析和案例
AdaptFormer学习笔记
OpenCV 颜色检测| color detection
LeetCode每日两题01:删除排序数组中的重复项 (均1200道)
n个骰子的点数------2022/08/04
Open3D 点云变换(变换矩阵)
【PTA】L2-002 链表去重(25分)
1325_FreeRTOS队列发送函数的实现分析
LeetCode 热题 HOT 100(9.电话号码的字母组合)
网页模板 pug 基本语法
微信小程序——小程序去除文本中的<br>标签
小技巧——postman配置token
AQS synchronization component - Semaphore (semaphore) analysis and case
微信小程序——image图片自适应
sql,update偶发性报错,ERROR: invalid input syntax for integer: '0.00'?
Redis的下载与安装Windows和Linux版
C语言文件读写操作/标准IO
MQTT X Newsletter 2022-07 | Auto update, MQTT X CLI support for MQTT 5.0, new conn command…