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 }

  

时间: 2024-10-10 08:15:51

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe的相关文章

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] Factorial Trailing Zeroe

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路: n!=2^x*3^y*5^z...,注意到一个2和一个5贡献一个末尾的0,因此只需计算min(x,z)即可.又因为n/2和n/5分别表示n!中的因子能被2和5整除的个数,n/2>=n/

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-1)]的用处 题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For exam

[LeetCode] Rotate Array / List

Question: http://leetcode.com/2010/04/rotating-array-in-place.html Rotate a one-dimensional array of n elements to the right by k steps.  For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d} // O (n) pub

[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

2016.5.16——leetcode:Reverse Bits(超详细讲解)

leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in bin

[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]189.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

LeetCode: Reverse Words in a String &amp;&amp; Rotate Array

Title: Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". https://leetcode.com/problems/reverse-words-in-a-string/ 思路:先将字符串全部逆转,在将每个单词逆转.具体实现,要注意空格,所以对字符串倒过来处理. cla