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 }
1 //在leetcode上可以运行的,int n = nums.size()无法通过 2 class Solution { 3 public: 4 void rotate(vector<int>& nums, int k) { 5 k %= nums.size(); 6 reverse(nums.begin(),nums.end()); 7 reverse(nums.begin(),nums.begin()+k); 8 reverse(nums.begin()+k,nums.end()); 9 } 10 };
自己跑的代码:还存在问题,先放上去。
1 // Rotate Array.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "iostream" 6 #include"algorithm" 7 //#include "string" 8 #include "vector" 9 using namespace std; 10 /* 11 class MyClass1 12 { 13 public: 14 void RotateArray(int nums[], int n, int k) 15 { 16 if (n == 0 || k == 0) //return nums[n]; 17 18 vector<int> numscopy[n]; 19 20 for (int i = 0; i < n; i++) 21 { 22 numscopy[i] = nums[i]; 23 } 24 25 for (int i = 0; i < n; i++) 26 { 27 nums[(i + k)%n] = numscopy[i]; 28 } 29 //return nums[n]; 30 } 31 };*/ 32 33 class MyClass2 34 { 35 public: 36 void RotateArray(int nums[], int n, int k) 37 { 38 39 k = k%n; 40 41 reverse(nums, nums + n); 42 reverse(nums, nums + (n-k)); //参考代码为 reverse(nums,nums+(n-k)),但是输出不对 43 reverse(nums + (n-k), nums + n); 44 //return nums; 45 } 46 }; 47 48 49 50 int _tmain(int argc, _TCHAR* argv[]) 51 { 52 size_t const N = 8; 53 int k ; 54 int x = 0, y = 0; 55 int arrary[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 56 //MyClass1 solution; 57 MyClass2 solution; 58 /* 如何一次输入 59 for (int j = 0; j < N; j++) 60 { 61 cin << arrary[j] << ", "; 62 }*/ 63 cin >> k; 64 solution.RotateArray(arrary, N, k); 65 //将数组一次全部输出 66 cout << "["; 67 for (int i = 0; i < N; i++) 68 { 69 cout <<arrary[i] << ", "; 70 } 71 cout << "]"<<endl; 72 //cout << arrary[i] << endl; 73 //cout << y << endl; 74 system("pause"); 75 return 0; 76 }
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 }
posted on 2016-05-17 09:32 zhuzhu2016 阅读(211) 评论(0) 编辑 收藏 举报