2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Posted zhuzhu2016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe相关的知识,希望对你有一定的参考价值。
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]
.
思路:
我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚。
leetcode/discuss思路: 思路一:新建数组,复制原数组,将新数组的内容存放在原数组中, nums[(i + k)%n] = numscopy[i]
思路二:反转先将数组反转位reverse(nums,nums+n) [7,6,5,4,3,2,1]
在反转reverse(nums,nums+k) [5,6,7,4,3,2,1]
在反转reverse(nums+k,nums+n) [5,6,7,4,3,2,1]
代码:
代码1:思路1 时间、空间复杂度均为(n)
1 class Solution 2 { 3 public: 4 void rotate(int nums[], int n, int k) //返回值为空 5 { 6 if ((n == 0) || (k <= 0)) 7 { 8 return; //所以returnd的为空 9 } 10 11 // Make a copy of nums 12 vector<int> numsCopy(n); 13 for (int i = 0; i < n; i++) 14 { 15 numsCopy[i] = nums[i]; 16 } 17 18 // Rotate the elements. 19 for (int i = 0; i < n; i++) 20 { 21 nums[(i + k)%n] = numsCopy[i]; 22 } 23 } 24 };
代码2:思路二 时间复杂度为(n),空间复杂度为(1)
1 void rotate(int nums[], int n, int k) { 2 reverse(nums,nums+n); //解释见思路 3 reverse(nums,nums+k%n); 4 reverse(nums+k%n,nums+n); 5 }
Factorial Trailing Zeroe
题目:
Given an integer n, return the number of trailing zeroes in n!.
给定一个整数n,求n!中0的个数
思路:
我的思路:刚开始将题目理解错误,当成求n!了
leetcode/dicuss思路:思路一:求0的个数,就是找10的个数,就是找2*5的个数,2出现的次数一定比5多,所以是5的个数决定的个数。那就求n中5个数。
思路二:假设n=100,100/5=20,但是100中并不是有20个5 ,而应该20/5=4,20+4=24个5.
代码1:思路1
1 class Solution { 2 public: 3 int trailingZeroes(int n) { 4 int res=0; 5 while(n){ //为什么要迭代 6 n/=5; 7 res+=n; 8 } 9 return res; 10 } 11 };
代码2:代码2
class Solution { public: int trailingZeroes(int n) { int count = 0; for (long long i = 5; n / i; i *= 5) count += n / i; return count; } };
大牛的解释: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 about what will generate a trailing 0
? Obviously, a number multiplied by 10
will have a trailing 0
added to it. So we only need to find out how many 10
‘s will appear in the expression of the factorial. Since 10 = 2 * 5
and there are a bunch more 2
‘s (each even number will contribute at least one 2
), we only need to count the number of 5
‘s.
Now let‘s see what numbers will contribute a 5
. Well, simply the multiples of 5
, like 5, 10, 15, 20, 25, 35, ...
. So is the result simply n / 5
? Well, not that easy. Notice that some numbers may contribute more than one 5
, like 25 = 5 * 5
. Well, what numbers will contribute more than one 5
? Ok, you may notice that only multiples of the power of 5
will contribute more than one 5
. For example, multiples of 25
will contribute at least two 5
‘s.
Well, how 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 the multiples of 5
will contribute one 5
, the multiples of 25
will contribute one more 5
and the multiples of 125
will contribute another one more 5
... and so on. Now, we can write down the following code, which is pretty short.
带main函数跑的代码:
1 #include "stdafx.h" 2 #include "iostream" 3 using namespace std; 4 5 class MyClass 6 { 7 public: 8 int FactorialTrailingZeroes(int n) 9 { 10 int res = 0; 11 //cout << res << endl; 12 for(int i = 5; i < n ; i *= 5) 13 { 14 //cout << i << endl; //测试 15 res += n / i; 16 //cout << res << endl; 17 } 18 return res; 19 } 20 }; 21 22 class While 23 { 24 public: 25 int FactorialTrailingZeroes(int n) 26 { 27 int res = 0; 28 while (n) 29 { 30 n = n/5; 31 res += n; 32 } 33 return res; 34 } 35 }; 36 37 int _tmain(int argc, _TCHAR* argv[]) 38 { 39 //MyClass solution; 40 While solution; 41 int nums ; 42 int m = 0; 43 cin >> nums; 44 m = solution.FactorialTrailingZeroes(nums); //32,33行写反了,所以进不了for循环 45 cout << m << endl; 46 system("pause"); 47 return 0; 48 }
以上是关于2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe的主要内容,如果未能解决你的问题,请参考以下文章