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)
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的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- [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 ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- [leetcode]Rotate Array
in place交换 如果是k步,那么就是把后面k个放到前面了嘛. 我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来 对于AB我们要通过reverse操作得 ...
- 【leetcode❤python】172. Factorial Trailing Zeroes
#-*- coding: UTF-8 -*-#给定一个整数N,那么N的阶乘N!末尾有多少个0? 比如:N=10,N!=3628800,N!的末尾有2个0.#所有的尾部的0可以看做都是2*5得来的,所以 ...
- LeetCode(172)Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 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 ...
随机推荐
- DAY8-Python学习笔记
老样子课有点多,睡觉有点多,玩手机有点多,总结就是事情有点多.Python项目还没找好所以就没上手. 今天学习内容贴几张图...
- BZOJ3252 攻略(贪心+dfs序+线段树)
考虑贪心,每次选价值最大的链.选完之后对于链上点dfs序暴力修改子树.因为每个点最多被选一次,复杂度非常正确. #include<iostream> #include<cstdio& ...
- 51nod 1785 数据流中的算法 | STL的应用
51nod 1785 数据流中的算法 题面 动态求平均数.方差.中位数. 题解 这道题的坑: 平均数在答案中是向下取整输出并在后面添加".00" 方差:平方的平均数减去平均数的平方 ...
- 一种KEIL中定义过的变量在使用中提示未定义的情况
[环境] > KEIL5.25 > win10 > @2018-4-23 [问题] 头文件互包含导致的错误(使用了另一文件的类型定义) 文件<fileA.h> <f ...
- Android源码批量下载及导入到Eclipse
1.去http://code.google.com/p/msysgit/downloads/list 下载Git,进行安装 2.生成批量下载脚本文件 下载批量工具CreatAutoGetSh(工具 ...
- Codeforces 901C. Bipartite Segments(思维题)
擦..没看见简单环..已经想的七七八八了,就差一步 显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢. 没有偶数简单环,相当于只有奇数简 ...
- 解题:POI 2006 PRO-Professor Szu
题面 这个题是比较套路的做法啦,建反图后缩点+拓扑排序嘛,对于所有处在$size>=2$的SCC中的点都是无限解(可以一直绕) 然后注意统计的时候的小细节,因为无限解/大解也要输出,所以我们把这 ...
- BZOJ 4802 欧拉函数
4802: 欧拉函数 Description 已知N,求phi(N) Input 正整数N.N<=10^18 Output 输出phi(N) Sample Input 8 Sample Outp ...
- [POI2012]STU-Well
题意翻译 给定一个非负整数序列A,每次操作可以选择一个数然后减掉1,要求进行不超过m次操作使得存在一个Ak=0且max(∣xi−xi−1∣)最小,输出这个最小值以及此时最小的k (1≤n≤1 000 ...
- etcd启用https服务
目录 cfssl相关工具下载 生成etcd所需要的ssl证书 生成ca证书 生成etcd服务端证书 生成etcd客户端证书 修改etcd集群配置文件 重启etcd集群 验证集群健康情况 关于etcd的 ...