Topic:
Given an array, move the elements in the array to the right by the K position, where K is a non-negative number.
Example 1:
Input: [1,2,3,4,5,6,7] k = 3 output: [5,6,7,1,2,3,4] Explanation:[7,1,2,3,4,5,6][6,7,1,2,3,4,5][5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] k = 2 output: [3,99,-1,-100] Explanation: Rotate Right 1 steps: [99,-1,-100,3] Rotate Right 2 steps: [3,99,-1,-100]
Description
- Think of as many solutions as possible, at least three different ways to solve the problem.
- Requires an in-place algorithm with a space complexity of O (1).
Topic Analysis:
For such a small white, like the author, get the first thought is to copy a copy of an array, but the topic requires "Using space complexity is O (1) in situ algorithm", so this method should pass off.
It's easy to think of a python solution to make a K-wheel move directly to an array, such as when I first wrote this Python code:
1 classSolution:2 defrotate (self, nums, K):3 """4 : Type Nums:list[int]5 : Type K:int6 : Rtype:void does not return anything, modify Nums in-place instead.7 """8 forJinchRange (0,k):9Temp=nums[len (nums)-1]Ten forIinchRange (0,len (nums)-1): OneNums[len (nums)-i-1]=nums[len (nums)-i-2] ANums[0]=temp
Error Instance
But it turns out that this method is too inefficient to pass the test of time complexity.
I have forgotten the advanced features of Python and C-slicing.
For the problem solving method with C:
There are many ways to solve the problem, you can refer to this blog: 80302654
The author also tried to solve this problem with C + +, relative to the use of the solution to solve the problem, C + + The trouble is that they need to find the length of the array numssize
The author learned how to determine how many elements are in an array in C language:
1, sizeof (array)/sizeof (array[0]);
2. sizeof (array)/sizeof (types of arrays, such as int,double, etc.)
However, this method is not reliable. Reason:
Development tools and the operating environment, sometimes sizeof (int) results are different, the author in Wintc and VC + + in the results of 2 and 4, so that can not get the correct (desired) results.
It is said that the type of int and the operating environment, in the case of 16bits is the case of 2,32bits is 4.
In addition, the range of representations for each data type is specified in C, but the number of bytes is not strictly specified.
Have to write the function yourself:
1#include <stdio.h>2 intMainvoid)3 {4 intI=0;5 intnums[]={1};6 intNumssize=0;7 for(i=0; nums[i]!=null;i++)8 {9numssize++;Ten } Oneprintf"%d", numssize); A return 0; -}Array Length calculation
1 classSolution {2 Public:3 voidRotate (vector<int>& Nums,intk) {4 intI=0; 5 inttemp[ -];6 intNumssize=0;7 for(i=0; nums[i]!=null;i++)8 {9numssize++;Ten } One for(i=numssize-1; i>numssize-1-k;i--)//no equals number i>=numssize-1-k ATemp[i+k-numssize] = nums[i];//Save the number of overwritten nums[i] - for(i=numssize-1; i>=k; i--) -Nums[i] = nums[i-k];//Nums[i] is overwritten, saved in advance to temp, and then nums[i]= shifted before a number the - for(i =0; i<=k-1; i++)//Assign Value -nums[i]=Temp[i]; - + }; -};C + + solutions
Unfortunately: When the input array is an array of only one element, the program error, the reason has not been detected, I hope to have passed the great God can point guidance!
Error message:
execution error message:malloc (): Memory corruption:0x000000000258ae90 * * *
Last input performed:[1] 1
解答代码:
C语言:
1 voidRotateint* Nums,intNumssize,intk) {2 intnow =0, start =0, Data_to_next =0, temp =0; 3 intCNT = numssize;//Used to indicate the number of jumps, the number of times to stop the loop4 5K = k%numssize; 6 if(k==0) 7 return; 8 9 for(start =0; start<numssize;start++) Ten { Onenow =start; AData_to_next = Nums[start%numssize];//first number, first proposed, for exchange with the next number - while(1) - { thenow = now+K; - -temp = nums[now%numssize];//Swap once -Nums[now%numssize] =Data_to_next; +Data_to_next =temp; -cnt--; + A if((now%numssize) = = start)//if the switch to start starts, it ends this jump exchange at Break; - - } - - if(cnt==0) - Break; in } - to +}
Python:
1 classSolution:2 defrotate (self, nums, K):3 """4 : Type Nums:list[int]5 : Type K:int6 : Rtype:void does not return anything, modify Nums in-place instead.7 """8Nums_len =Len (nums)9nums[:] = nums[nums_len-k:] + nums[:nums_len-k]
"Leetcode" rotation array "C, Python"