旋转数组leetcode 189

旋转数组

解题思路:环形旋转

class Solution {
    public void rotate(int[] nums, int k) {
        int len = nums.length;
        k = k%len;
        int count = 0;
        int temp = 0;
        int start = 0;
        int i = 0;
        int swp = 0;
        while(count<len){
            i=start;
            temp = nums[i];
            do{
                swp = nums[(i+k)%len];
                nums[(i+k)%len] = temp;
                temp = swp;
                ++count;
                i=(i+k)%len;
            } while(i!=start);
            ++start;
        }
    }
}

原文地址:https://www.cnblogs.com/erdanyang/p/11497292.html

时间: 2024-11-06 14:02:00

旋转数组leetcode 189的相关文章

前端与算法 leetcode 189. 旋转数组

目录 # 前端与算法 leetcode 189. 旋转数组 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 189. 旋转数组 题目描述 189. 旋转数组 概要 把他当做一到简单的题来做,不要想太多了就好也可以不整那些花里胡哨的,直接旋转数组n次,我一开始也想到了这个办法,但是觉得太简单而且效率低下,想了很久也没想到合适的办法 提示 使用额外的数组 解析 用一个额外的数组将每个元素放到对应的位置就好 下标为i的位置对应(i+k)%数组长度 ,然后把新的数组拷贝(深拷贝)到原

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]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

leetcode旋转数组查找 二分查找的变形

http://blog.csdn.net/pickless/article/details/9191075 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return it

[LeetCode]111. Find Minimum in Rotated Sorted Array II旋转数组最小值II

Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might be

[LeetCode]26. Search in Rotated Array II旋转数组查找II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解法:同旋转数组查找I,本题也使用二分查找,只是在nums[mid]和nums[right

[LeetCode]25. Search in Rotated Array旋转数组查找

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

leetcode之Search in Rotated Sorted Array,剑指offer之旋转数组的最小数字

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

剑指Offer-6.旋转数组的最小数字(C++/Java)

题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1.NOTE:给出的所有元素都大于0,若数组大小为0,请返回0. 分析: 这道题和LeetCode上153,154是一样的.只不过153的数组元素是不重复的,154则允许数组有重复元素. 这里直接要求输入的是非递减排序的数组,所以和154题是一样的. LeetCode 153. Fi