rotate array 旋转数组

class Solution {
public:
void rotate(vector<int>& nums, int k) {

int n=nums.size();
int i=0;

/*

//-------------------

//解法一  会超时

//--------------------
k=k%n;
while(i<k){
  int temp=nums[n-1];
  for(int j=n-1;j>0;j--){
    nums[j]=nums[j-1];
  }
  nums[0]=temp;
  i++;
  }
*/

//-------------------

//解法二

//--------------------

vector<int> num1;  新开一个数组,存放
k=k%n;
if(k==0) return;
while(i<k)
{
  num1.push_back(nums[n-k+i]);
  i++;
}
i=0;
while(i<n-k)
{
  num1.push_back(nums[i]);
  i++;
}
i=0;
while(i<n)
{
  nums[i]=num1[i];
  i++;
}

}

//-------------------

//解法三   三次旋转;很巧妙

//--------------------

};

时间: 2024-12-30 02:55:08

rotate array 旋转数组的相关文章

[LeetCode]12. 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] 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 Rotate Array 翻转数组

题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. 1 class Solution { 2 public: 3 void rotate(int nums[], int n, int k) { 4 if( !k || !n || n==1 || k==n ) return; 5 k %= n; 6 vector<int> cha; 7 cha.reserve

(每日算法)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]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

Find Minimum 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). Find the minimum element. You may assume no duplicate exists in the array. 这道题<剑指offer>上有原题,直接上代码 solution: int findMi

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整.数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况). 假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n].则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的.并且因为原数组是有序递增的,A[0..x] 中所有数都会大于 A[x+1..n] 中

153 Find Minimum in Rotated Sorted Array 旋转数组的最小值

假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ class Solution { public: int findMin(vector<int>& nums) { int low=0; int

[LeetCode] Rotate List 旋转链表

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 这道旋转链表的题和之前那道Rotate Array 旋转数组 很类似,但是比那道要难一些,因为链表的值不能通过下表来访问,只能一个一个的