Leetcode解题笔记-Rotate Array

题目要求:

将数组的位置向右旋转移动k位。

个人解法:

1.这个题目重点在于需要对边界进行判断,因为k的值有的时候会比n要大,所以要充分理解%的意义

2.定义一个copynums可以存储临时变量。

3. 在第二个循环中可以利用(i+k)%len的公式准确找到并赋值需要变化的位置。

相关代码:

public void rotate(int[] nums, int k) {
int len = nums.length;
if(len==0) return;
int[] copynums = new int[len];
for(int i = 0 ; i<len; i++){
copynums[i]=nums[i];
}
for (int i = 0 ; i<len; i++){
nums[(k+i)%len]=copynums[i];
}
}

时间: 2025-01-15 02:40:29

Leetcode解题笔记-Rotate Array的相关文章

LeetCode:(Array-189) Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

【LeetCode】189. Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

【LeetCode】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 [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 这道题目 有很多种解法,我用的是这种,相对也好理解,先所有元素reverse,再0-(n-k)的元素reverse,然后(n-k)-n的元素reverse. class Solution { pub

Leetcode解题笔记-Merge sorted Array

题目要求: 将两个排序好的nums1和nums2数组合并成为一个数组,nums1中有足够的空间,有m+n的空间,在nums1中有m个元素,在nums2中有n个元素 解题思路: 将nums2中的元素归并到nums1中,对于nums1以及nums2从后往前进行遍历,归并到nums1中, while(i>-1&&j>-1)nums1[k--]= (nums1[i--]>nums2[j--])?nums1[i--]:nums2[j--];

leetcode解题笔记-Remove Duplicates from Sorted Array

题目要求: 去除数组中相重复的的元素,并且返回新数组的长度.(要求不能再申请额外空间,只能在原来数组上进行操作) 个人理解: 1. 数组定义之后就是定长的不能改变,只能在原来数组上进行操作,不能像链表一样根据指针进行操作 2. 与remove element相似,利用数组中后续的元素进行代替,将数组的一部分变为无重复且排序好的结果子数组: if(nums[i-1]!=nums[i]) nums[id++] = nums[i]; // 如果前一项的值不等于后一项,那么在id+1项的值就等于当前项,

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

LeetCode OJ:Rotate Array(倒置数组)

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 将数组的内容倒置,看例子就知道是什么意思: 1 class Solution { 2 public: 3 void rotate(vector<int>& nums, int k) { 4 if(

LeetCode OJ 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 [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro