Rotate Array
This topic Harvest:
Topic:
Rotate an array of n elements to the right by K steps.
For example, with n = 7 and k = 3, the array is [1,2,3,4,5,6,7]
rotated to [5,6,7,1,2,3,4]
.
Ideas:
My idea: Create a new array to hold the rotated content, but how to store the contents of the original array in the array is unclear.
Leetcode/discuss ideas: A new array, copy the original array, the contents of the new array is stored in the original array, nums[(i + k)%n] = Numscopy[i]
Idea two: Invert the array reversal first bit reverse (nums,nums+n) [7,6,5, 4,3,2,1]
In reverse reverse (nums,nums+k) [5,6,7, 4,3,2,1]
In reverse reverse (nums+k,nums+n) [5,6,7,4,3,2,1]
Code:
Code Listing 1: Ideas 1 time, space complexity are all (n)
1 classSolution2 {3 Public:4 voidRotateintNums[],intNintK//return value is empty5 {6 if(n = =0) || (k <=0))7 {8 return;//so the returnd is empty.9 }Ten One //Make a copy of Nums Avector<int>numscopy (n); - for(inti =0; I < n; i++) - { theNumscopy[i] =Nums[i]; - } - - //Rotate the elements. + for(inti =0; I < n; i++) - { +nums[(i + k)%n] =Numscopy[i]; A } at } -};
Code Listing 2: Thinking two time complexity is (n), spatial complexity is (1)
1 void rotate (intint int k) {2 reverse (nums,nums+n); // explanation See ideas 3 Reverse (nums,nums+k%n); 4 Reverse (nums+k%n,nums+n); 5 }
Factorial Trailing Zeroe
Topic:
Given an integer n, return the number of trailing zeroes in N!.
Given an integer n, find the number of 0 in n!
Ideas:
My idea: Just start to understand the problem, as a n!
Leetcode/dicuss ideas: The idea of a: 0 of the number, is to find 10 of the number, is to find the number of 2*5, 2 occurrences of the number must be more than 5, so is 5 of the number of decisions. Then ask for 5 numbers in N.
Idea two: Suppose N=100,100/5=20, but 100 is not 20 5, but should 20/5=4,20+4=24 5.
Code Listing 1: Ideas 1
1 classSolution {2 Public:3 intTrailingzeroes (intN) {4 intres=0;5 while(n) {//Why Iterate6N/=5;7res+=N;8 }9 returnRes;Ten } One};
Code Listing 2: Code 2
class Solution {public: int trailingzeroes (int n) { int0; for (longlong55) + = n/ i ; return count; }};
Daniel's explanation: Https://leetcode.com/discuss/42624/4-lines-4ms-c-solution-with-explanations
Well, to compute the number of trailing zeros, we need to first think clear on what would generate a trailing 0
? Obviously, a number multiplied by would has 10
a trailing 0
added to it. So we are need to find out how many's would appear in the expression of the 10
factorial. Since 10 = 2 * 5
and there is a bunch more 2
' s (each even number would contribute at least one 2
), we only need to Coun t the number of 5
' s.
Now let's see what numbers would contribute a 5
. Well, simply the multiples of 5
, like 5, ten, +, ...
. So is the result simply N/5
? Well, not. Notice that some numbers could contribute more than one 5
, like 5 * 5
. Well, what numbers would contribute more than one 5
? Ok, notice that is only multiples of the power of 5
would contribute more than one 5
. For example, multiples of
would contribute at least, 5
' s.
Well, what to count them all? If You try some examples, you may finally get the result, which is n / 5 + n / 25 + n / 125 + ...
. The idea behind this expression Is:all 5
the multiples of would contribute one 5
, the multiples of would 25
co Ntribute one more and the multiples of'll contribute another one more ... and so on 5
125
5
. Now, we can write down the following code, which are pretty short.
Code to run with main function:
1#include"stdafx.h"2#include"iostream"3 using namespacestd;4 5 classMyClass6 {7 Public:8 intFactorialtrailingzeroes (intN)9 {Ten intres =0; One //cout << res << Endl; A for(inti =5; I < n; I *=5) - { - //cout << i << Endl; //Test theRes + = n/i; - //cout << res << Endl; - } - returnRes; + } - }; + A class while at { - Public: - intFactorialtrailingzeroes (intN) - { - intres =0; - while(n) in { -n = n/5; toRes + =N; + } - returnRes; the } * }; $ Panax Notoginseng int_tmain (intARGC, _tchar*argv[]) - { the //MyClass solution; + while solution; A intNums; the intm =0; +CIN >>nums; -m = solution. Factorialtrailingzeroes (Nums);//32, 33 lines are reversed, so I can't get into the for loop. $cout << M <<Endl; $System"Pause"); - return 0; -}
2016.5.16--leetcode:rotate array,factorial Trailing Zeroe