LeetCode //189. Rotate Array

这篇具有很好参考价值的文章主要介绍了LeetCode //189. Rotate Array。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

189. Rotate Array

Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

 

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints:
  • 1 < = n u m s . l e n g t h < = 1 0 5 1 <= nums.length <= 10^5 1<=nums.length<=105
  • − 2 31 < = n u m s [ i ] < = 2 31 − 1 -2^{31} <= nums[i] <= 2^{31} - 1 231<=nums[i]<=2311
  • 0 < = k < = 1 0 5 0 <= k <= 10^5 0<=k<=105

From: LeetCode
Link: 189. Rotate Array
文章来源地址https://www.toymoban.com/news/detail-539749.html

Follow up:
  • Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Solution:

Ideas:
1. We create a new dynamically allocated array that will hold the rearranged elements.
2. Then we iterate over the original array, placing each element on its new position in the new array. The new position is calculated as (i + k) % numsSize to ensure we wrap around to the start of the array when we reach the end.
3. We then copy the new array back into the original array.
4. Lastly, we free the memory we allocated for the new array.
This approach takes O(n) time because we’re doing a single pass from the array, and O(n) extra space to hold the rearranged elements.
Note: Please ensure your compiler supports C99 or later to use variable length arrays and the malloc, free function for dynamic memory allocation.
Code:
void rotate(int* nums, int numsSize, int k){
    int* newArr = malloc(numsSize * sizeof(int));

    for(int i = 0; i < numsSize; i++){
        newArr[(i + k) % numsSize] = nums[i];
    }

    for(int i = 0; i < numsSize; i++){
        nums[i] = newArr[i];
    }

    free(newArr);
}

到了这里,关于LeetCode //189. Rotate Array的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 189.轮转数组

    题目链接👉LeetCode 189.轮转数组👈 给定一个整数数组 nums ,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 假如我们要把数组 [1,2,3,4,5,6,7] ,向右旋转 3 次 👇图解👇 第1步:定义一个临时变量 tmp ,用来存放数组最后的元素 7 第2步:把数组前 n-1 个值往后挪 第3步:把

    2023年04月17日
    浏览(28)
  • 【LeetCode】189. 轮转数组

    题目链接: https://leetcode.cn/problems/rotate-array/ 📕题目要求: 给定一个整数数组  nums ,将数组中的元素向右轮转  k   个位置,其中  k   是非负数。 尽可能想出更多的解决方案,至少有  三种  不同的方法可以解决这个问题。 你可以使用空间复杂度为  O(1)  的  原地  算法

    2023年04月27日
    浏览(24)
  • Leetcode:【189. 轮转数组】

    给定一个整数数组  nums ,将数组中的元素向右轮转  k   个位置,其中  k   是非负数 难度:中等 题目链接:189. 轮转数组 示例 2: 提示: 1 = nums.length = 10^5 -2^31 = nums[i] = 2^31 - 1 0 = k = 10^5 核心思想 : 逆置数组 先逆置全部数组 再把右旋过去的数组逆置一下 最后把剩下部分数

    2024年02月07日
    浏览(22)
  • 【LeetCode】189. 轮转数组 - 双指针

    189. 轮转数组

    2024年02月13日
    浏览(29)
  • 【LeetCode-中等题】189. 轮转数组

    思路:通过,开辟数组 取模运算寻找新位置------ 位置(i+k)mod n =新位置 思路: 1、先全部翻转 2、在根据k 的值 对k-1 的两边区域进行翻转 3、注意 k如果 数组长度 就会出现下标越界,所以需要开始就k对数组长度取模 k %=n

    2024年02月11日
    浏览(23)
  • LeetCode //C - 61. Rotate List

    Given the head of a linked list, rotate the list to the right by k places.   Example 1: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Example 2: Input: head = [0,1,2], k = 4 Output: [2,0,1] Constraints: The number of nodes in the list is in the range [0, 500]. -100 = Node.val = 100 0 = k = 2 ∗ 1 0 9 0 = k = 2 * 10^9 0 = k = 2 ∗ 1 0 9 From: LeetCo

    2024年02月10日
    浏览(34)
  • LeetCode //C - 48. Rotate Image

    You are given an n x n 2D matri x representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place , which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.   Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: In

    2024年02月14日
    浏览(25)
  • 【LeetCode力扣】189 53 轮转数组 | 最大子数组和

    目录 1、189. 轮转数组 1.1、题目介绍 1.2、解题思路 2、53. 最大子数组和 2.1、题目介绍 2.2、解题思路   原题链接: 189. 轮转数组 - 力扣(LeetCode) ​ 示例 1: 输入: nums = [1,2,3,4,5,6,7], k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步: [6,7,1,2,3,4,5] 向右轮转

    2024年02月08日
    浏览(27)
  • leetcode每日一题——189.轮转数组(面试经典150题)

    189. 轮转数组 - 力扣(LeetCode) 给定一个整数数组  nums ,将数组中的元素 向右轮转  k   个位置 ,其中  k   是非负数。 示例1: 示例2: 1 = nums.length = 105 -231 = nums[i] = 231 - 1 0 = k = 105        对题目进行分析可知,我们需要根据轮转量k,将数组后面的k个元素按照原来的顺

    2024年02月12日
    浏览(25)
  • 【LeetCode 算法】Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数-Greedy

    给你一个正整数数组 nums 。每一次操作中,你可以从 nums 中选择 任意 一个数并将它 减小 到 恰好 一半 。(注意,在后续操作中你可以对减半过的数继续执行操作) 请你返回将 nums 数组和 至少 减少一半 的 最少 操作数。 1 = n u m s . l e n g t h = 1 0 5 1 = n u m s [ i ] = 1 0 7 1 = num

    2024年02月15日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包