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 ...
随机推荐
- 谁能告诉delphi7 的updatebatch使用属性说明?
谁能告诉delphi7 的updatebatch使用属性说明? ADODataSet1.UpdateBatch(arAll); 就是提交你的数据集到数据库 arCurrentOnly the upda ...
- rabbitmq .erlang.cookie文件疑惑
1.安装方式常见的rabbitmq安装方式有两种:rpm安装和二进制安装(编译安装). 2..erlang.cookie是什么.erlang.cookie是erlang实现分布式的必要文件,erlan ...
- BZOJ3162 独钓寒江雪(哈希+树形dp)
数独立集显然是可以树形dp的,问题在于本质不同. 假设已经给树确立了一个根并且找到了所有等效(注意是等效而不是同构)子树,那么对转移稍加修改使用隔板法就行了. 关键在于找等效子树.首先将树的重心(若有 ...
- LOJ #143. 质数判定
题目描述 判定输入的数是不是质数. 输入格式 若干行,一行一个数 x. 行数不超过 1.5×104. 输出格式 对于输入的每一行,如果 x 是质数输出一行 Y,否则输出一行 N. 样例 样例输入 ...
- Redis中国用户组|唯品会Redis cluster大规模生产实践
嘉宾:陈群 很高兴有机会在Redis中国用户组给大家分享redis cluster的生产实践.目前在唯品会主要负责redis/hbase的运维和开发支持工作,也参与工具开发工作 Outline 一.生 ...
- mybatis.5.动态SQL
1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...
- Using Immutable in React + React-Redux
React-Redux Introduction React-Redux is a library for React based on Redux package. And the core ide ...
- LGP4173残缺的字符串
题解 由于有通配符,所以$kmp$失效了: 将通配符看成0,其余字符看成互不相同的数字,$A,B$串对应得到$a,b$数组; 定义: $f(p) = \sum_{i=0}^{m-1} a_{i}b_{ ...
- Access to a protected network share using Win32 C++
WNetAddConnection2 DWORD WNetAddConnection2A( LPNETRESOURCEA lpNetResource, LPCSTR lpPassword, LPCST ...
- 【Asp.net入门4-01】基本开发工具