`

Leetcode -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再局部reverse完成偏移若干位操作。

public class Solution {
    // method 1: time O(N), space O(N)
    public void rotate1(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k % nums.length == 0)
            return;
        int n = nums.length;
        k = k % n;
        int[] tmp = new int[k];
        for (int i = 0; i < k; i++) {
            tmp[i] = nums[n - k + i];
        }
        for (int i = n - k - 1; i >= 0; i--) {
            nums[i + k] = nums[i];
        }
        for (int i = 0; i < k; i++) {
            nums[i] = tmp[i];
        }
    }
    // method 2: time O(k * n), space O(1), timeout
    public void rotate2(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k % nums.length == 0)
            return;
        int n = nums.length;
        k = k % n;
        if (k < n / 2) {
            for (int i = 0; i < k; i++) {
                int tmp = nums[n - 1];
                for (int j = n - 1; j > 0; j--) {
                    nums[j] = nums[j - 1];
                }
                nums[0] = tmp;
            }
        } else {
            for (int i = 0; i < k; i++) {
                int tmp = nums[0];
                for (int j = 0; j < n - 1; j++) {
                    nums[j] = nums[j + 1];
                }
                nums[n - 1] = tmp;
            }
        }
    }
    // method 3: time O(n), space O(1)
    public void rotate(int[] nums, int k) {
        if (nums == null || nums.length == 0 || k % nums.length == 0)
            return;
        int n = nums.length;
        k = k % n;
        reverse(nums, 0, n - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, n - 1);
    }
    public void reverse(int[] nums, int left, int right) {
        while (left < right) {
            int tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
            left++;
            right--;
        }
    }
    
}
分享到:
评论

相关推荐

    leetcode-cpp刷题

    ### LeetCode-CPP刷题知识点概述 #### 一、引言 《LeetCode-CPP刷题》是一本针对程序员面试及算法训练的书籍,由戴方勤编著,旨在帮助读者提升解决算法问题的能力,特别是在准备北美乃至全球范围内技术公司的面试时...

    扔鸡蛋leetcode-LeetCode-Note-Mary:玛丽的leetcode笔记本

    Rotate Array (旋转数组) #数组 33. Search in Rotated Sorted Array(搜索旋转排序数组)#数组 2020/12/08 19. Remove Nth Node From End of List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted...

    _leetcode-python.pdf

    标题“_leetcode-python.pdf”所指的知识点: 该标题表明此文件是一份关于LeetCode的Python解题集。LeetCode是一个面向程序员的在线编程题库,提供各种编程语言的算法和数据结构题目,用于帮助编程者提升技能,尤其...

    算法-leetcode-剑指offer上的题很多

    - **字符串旋转(Rotate String)**: 将字符串进行旋转操作。 - **反转单词(Reverse Words)**: 将字符串中的单词顺序反转。 - **验证回文字符串(Valid Palindrome)**: 判断一个字符串是否是回文。 - **最长回文子串...

    戳气球leetcode-leetcode:leetcode

    189.rotate-array 283.move-zero Range Sum Query - Immutables 66.Plus One 快手-跳格子 Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array ...

    蓄水池算法leetcode-leetcode:leetcode

    rotate: A Linear Time Majority Vote Algorithm 题解: Maximum Gap: Something about largest-rectangle-in-histogram: 最长递增子序列: 最长公共自序列: Something about: best time to buy and sell stack: ...

    Leetcode-best-DSA-问题:必须执行这些leetcode编码问题以提高您的问题解决能力

    LeetCode中的这类问题涵盖了排序、查找、反转等操作,如“两数之和”(Two Sum)、“旋转数组”(Rotate Array)和“合并两个有序链表”(Merge Two Sorted Lists)。 2. **栈和队列**:这两种线性结构在处理逆序...

    LeetCode题解 - Java语言实现-181页.pdf

    1. Rotate Array in Java 数组旋转是一个基本的数组操作,要求将数组中的元素旋转一定的位置。这种操作可以使用Java的System.arraycopy()方法或手动循环实现。 2. Evaluate Reverse Polish Notation 逆波兰表示法...

    leetcode浇花-LCSolutions:我的力扣解决方案

    leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer #0009 - Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates...

    leetcode答案-LeetCode:LeetCode刷题记录,README显示实时进度/题目

    https://leetcode.com/problems/rotate-array/description/ * * algorithms * Easy (28.74%) * Total Accepted: 262.7K * Total Submissions: 913.5K * Testcase Example: '[1,2,3,4,5,6,7]\n3' * * Given an array,...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 ...Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    javalruleetcode-LeetCode:LeetCode算法问题

    RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 ...

    leetcode1004-leetcode:leetcode

    Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. ...

    LeetCode最全代码

    462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...

    leetcode跳跃-leetcode:leetcode解题之路

    旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/...

    算法刷题笔记leetcode/lintcode

    - Rotate String(旋转字符串) - Reverse Words in a String(反转字符串中的单词) - Valid Palindrome(回文字符串验证) - Longest Palindromic Substring(最长回文子串) - Space Replacement(URL化) ...

    leetcode分类-leetcode:leetcode刷题

    3. **数组旋转**:如“旋转数组”(Rotate Array),需要对数组进行特定步数的逆时针或顺时针旋转。 4. **滑动窗口**:在数组中使用滑动窗口进行统计,如“最长连续序列”(Longest Consecutive Sequence)。 5. *...

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    leetcode中国Final450_数据结构 实际上,这个存储库包含成为优秀竞争程序员最重要的数据结构和算法。 他们的名字是: Questions ----------- *Reverse the array *Find the maximum and minimum element in an array...

    leetcode中国-DP:DP

    leetcode中国大批 0. Count Prime 1. Reverse the array 2. Find the maximum and minimum element in an array 3. Find the "Kth" max and min element of an array 4. Given an array which consists of only 0, 1...

    Leetcode题目+解析+思路+答案.pdf

    - **Rotate List**:将链表顺时针旋转指定次数。 - **Reorder List**:按照特定规则重新排列链表。 - **Partition List**:将链表按值分割成两个部分。 - **Add Two Numbers**:两个非负整数相加,结果存储在...

Global site tag (gtag.js) - Google Analytics