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)

 class Solution
{
public:
void rotate(int nums[], int n, int k) //返回值为空
{
if ((n == ) || (k <= ))
{
return; //所以returnd的为空
} // Make a copy of nums
vector<int> numsCopy(n);
for (int i = ; i < n; i++)
{
numsCopy[i] = nums[i];
} // Rotate the elements.
for (int i = ; i < n; i++)
{
nums[(i + k)%n] = numsCopy[i];
}
}
};

  代码2:思路二 时间复杂度为(n),空间复杂度为(1)

 

 void rotate(int nums[], int n, int k) {
reverse(nums,nums+n); //解释见思路
reverse(nums,nums+k%n);
reverse(nums+k%n,nums+n);
}
 //在leetcode上可以运行的,int n = nums.size()无法通过
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k %= nums.size();
reverse(nums.begin(),nums.end());
reverse(nums.begin(),nums.begin()+k);
reverse(nums.begin()+k,nums.end());
}
};

  自己跑的代码:还存在问题,先放上去。

 // Rotate Array.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include"algorithm"
//#include "string"
#include "vector"
using namespace std;
/*
class MyClass1
{
public:
void RotateArray(int nums[], int n, int k)
{
if (n == 0 || k == 0) //return nums[n]; vector<int> numscopy[n]; for (int i = 0; i < n; i++)
{
numscopy[i] = nums[i];
} for (int i = 0; i < n; i++)
{
nums[(i + k)%n] = numscopy[i];
}
//return nums[n];
}
};*/ class MyClass2
{
public:
void RotateArray(int nums[], int n, int k)
{ k = k%n; reverse(nums, nums + n);
reverse(nums, nums + (n-k)); //参考代码为 reverse(nums,nums+(n-k)),但是输出不对
reverse(nums + (n-k), nums + n);
//return nums;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
size_t const N = ;
int k ;
int x = , y = ;
int arrary[] = { , , , , , , , };
//MyClass1 solution;
MyClass2 solution;
/* 如何一次输入
for (int j = 0; j < N; j++)
{
cin << arrary[j] << ", ";
}*/
cin >> k;
solution.RotateArray(arrary, N, k);
//将数组一次全部输出
cout << "[";
for (int i = ; i < N; i++)
{
cout <<arrary[i] << ", ";
}
cout << "]"<<endl;
//cout << arrary[i] << endl;
//cout << y << endl;
system("pause");
return ;
}

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

 class Solution {
public:
int trailingZeroes(int n) {
int res=;
while(n){ //为什么要迭代
n/=;
res+=n;
}
return res;
}
};

  代码2:代码2

class Solution {
public:
int trailingZeroes(int n) {
int count = ;
for (long long i = ; n / i; i *= )
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函数跑的代码:

 #include "stdafx.h"
#include "iostream"
using namespace std; class MyClass
{
public:
int FactorialTrailingZeroes(int n)
{
int res = ;
//cout << res << endl;
for(int i = ; i < n ; i *= )
{
//cout << i << endl; //测试
res += n / i;
//cout << res << endl;
}
return res;
}
}; class While
{
public:
int FactorialTrailingZeroes(int n)
{
int res = ;
while (n)
{
n = n/;
res += n;
}
return res;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
//MyClass solution;
While solution;
int nums ;
int m = ;
cin >> nums;
m = solution.FactorialTrailingZeroes(nums); //32,33行写反了,所以进不了for循环
cout << m << endl;
system("pause");
return ;
}

  

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

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. Python3解leetcode Rotate Array

    问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...

  3. LeetCode Rotate Array 翻转数组

    题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...

  4. [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  ...

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

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

  6. [leetcode]Rotate Array

    in place交换 如果是k步,那么就是把后面k个放到前面了嘛. 我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来 对于AB我们要通过reverse操作得 ...

  7. 【leetcode❤python】172. Factorial Trailing Zeroes

    #-*- coding: UTF-8 -*-#给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0.#所有的尾部的0可以看做都是2*5得来的,所以 ...

  8. LeetCode(172)Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  9. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

随机推荐

  1. Java多线程(二) —— 深入剖析ThreadLocal

    对Java多线程中的ThreadLocal类还不是很了解,所以在此总结一下. 主要参考了http://www.cnblogs.com/dolphin0520/p/3920407.html 中的文章. ...

  2. Nagios学习笔记

    1 Nagios功能 1.1  监控工具 1.2  可以监控主机/服务或者资源 1.3  四种状态值 OK,WARNING,CRITICAL,UNKNOWN CPU:90%(CRITICAL),80% ...

  3. Day22-中间件

    1.中间件,在其它程序中,有的叫管道,有的叫http handler.下面是原生的中间件 2.自己也可以写中间件 2.1 写中间件,新建文件夹Middle,新建m1.py 2.2 在setting里注 ...

  4. MT【144】托兰定理【图论】

    平面上$2n$个点$(n>1,n\in N)$,无三点共线,任意两点连线段,将其中任意$n^2+1$条线段染红色. 求证:三边都为红色的三角形至少有$\left[\dfrac{2}{3}(n+\ ...

  5. BZOJ 4520: [Cqoi2016]K远点对

    4520: [Cqoi2016]K远点对 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 638  Solved: 340[Submit][Status ...

  6. Longest Word in Dictionary through Deleting - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Longest Word in Dictionary through Deleting - LeetCode 注意点 长度一样的字符串要按字典序返回较小的 ...

  7. Android Progurad 代码混淆

    ref: ProGuard基础语法和打包配置.mdhttps://github.com/D-clock/Doc/blob/master/Android/Gradle/3_ProGuard%E5%9F% ...

  8. BZOJ 3438 小M的作物 & BZOJ 1877 [SDOI2009]晨跑

    我由衷地为我的朋友高兴.哈哈,yian,当你nick name破百上千时,再打“蒟蒻”就会被打的. 好的,说正事吧.请注意,这还是题解.但我发现,网络流实在是太套路了(怪不得这两年几乎销声匿迹).我们 ...

  9. 字符串连接比较(std::unique_ptr实现)

    比较代码之间可能相差大,可是速度相差很大,而且目的在于测试unique_ptr使用...; C/C++: #include <iostream> std::unique_ptr<ch ...

  10. pom.xml错误:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的解决方法

    pom.xml文件在添加了新的依赖后,一直报:org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter)的错误,Maven ...