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. win7 32位 import cv2 失败 ImportError:DLL load fail:找不到指定模块

    引起问题的可能性太多,这里记录比较一下比较少见的错误原因 缺少dll文件 https://www.dll-files.com/api-ms-win-downlevel-shlwapi-l1-1-0.d ...

  2. No module named 'MySQLdb' python3.6 + django 1.10 + mysql 无法连接

    学习python 连接mysql数据库的时候遇到了问题 首先安装mysql: 工具栏 ===>file ==> default settings==>Project Interpre ...

  3. ie8 ajaxSubmit 上传文件提示下载

    转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json,返回text/html

  4. P2231 [HNOI2002]跳蚤

    题目描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最 ...

  5. [洛谷P3829][SHOI2012]信用卡凸包

    题目大意:有$n$张一模一样的信用卡,每个角进行了圆滑处理,问这些卡组成的“凸包”的周长 题解:发现是圆滑处理的圆心围成的凸包加上一个圆周即可 卡点:输入长宽弄反,然后以为是卡精 C++ Code: ...

  6. 聊聊flink的NetworkEnvironmentConfiguration

    本文主要研究一下flink的NetworkEnvironmentConfiguration NetworkEnvironmentConfiguration flink-1.7.2/flink-runt ...

  7. C#基础-代码部署数据库及IIS站点

    一.前言        最近忙里偷闲,做了一个部署数据库及IIS网站站点的WPF应用程序工具. 二.内容        此工具的目的是: 根据.sql文件在本机上部署数据库 在本机部署IIS站点,包括 ...

  8. BZOJ 4408: [Fjoi 2016]神秘数

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 464  Solved: 281[Submit][Status ...

  9. Java EE之通过表单上传文件

    public class Ticket { private String customerName; private String subject; private String body; priv ...

  10. bzoj3672: [Noi2014]购票(树形DP+斜率优化+可持久化凸包)

    这题的加强版,多了一个$l_i$的限制,少了一个$p_i$的单调性,难了好多... 首先有方程$f(i)=min\{f(j)+(dep_i-dep_j)*p_i+q_i\}$ $\frac {f(j) ...