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. 【Django】用pycharm初学习使用Django

    开发框架流程 M V C(99%的开发都是这种流程.) 1.URL控制器 2.Views 视图 3.models 库   1.首先创建一个Django   2.创建成功后里面几个模块的功能   用它来 ...

  2. matlab dist函数

    dist——欧式距离加权函数(Euclidean distance weight function) 语法: Z = dist(W,P)    df = dist('deriv')    D = di ...

  3. 自定义样式,使用浏览器阅读epub格式的电子书

    epub格式的电子式一般用专门的阅读器打开,但是如果可以使用浏览器打开,就可以随意更改css了,获得极致的体验效果. 比如可以自定义字体.行间距.背景色.字体大小.缩进等等... 当然,如果您不需要添 ...

  4. Make a Crystal UVA - 11014 (容斥定理)

    题意:给定一个NxNxN的正方体,求出最多能选几个整数点,使得任意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O),那么所 ...

  5. 【poj2411】 Mondriaan's Dream

    http://poj.org/problem?id=2411 (题目链接) 题意 一个$n*m$的网格,用$1*2$的方块填满有多少种方案. Solution 轮廓线dp板子.按格dp,对上方和左方的 ...

  6. 前端学习 -- Css

    Css:Cascading Style Sheets CSS叫做层叠样式表,用来设置页面中元素的样式.背景颜色.字体颜色.字体大小... 编写位置: 1,内联样式: 将样式编写到标签的style属性中 ...

  7. svn提交失败 :“svn: E200007: Commit failed”

    引言: 在切换subversion服务器地址之后,发生的无法正确提交代码的问题org.apache.subversion.javahl.ClientException: svn: E200007: C ...

  8. 使用kafka消息队列解决分布式事务(可靠消息最终一致性方案-本地消息服务)

    微服务框架Spring Cloud介绍 Part1: 使用事件和消息队列实现分布式事务 本文转自:http://skaka.me/blog/2016/04/21/springcloud1/ 不同于单一 ...

  9. Git3:Git分支

    目录 一.概念 二.创建与合并分支 1.创建分支原理分析 2.创建分支语法 三.解决冲突 四.分支管理策略 1.保留分支历史 2.分支管理原则 五. bug分支 六.推送和拉取远程分支 一.概念 分支 ...

  10. pyqt5的代码

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...