Leetcode--easy系列8

#172 

Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

一个很直观的想法是用递归求出n!然后/10 计算末尾0的个数,但是不能AC。

换一种思路,末尾0来自于2*5,而对于任意一个正整数 n,1~n之间5的个数小于2的个数(间隔一个为2一个为5);

只要求出1~n之间所有数因式分解后5的个数:(即包括10,15,20,35....中的5)。具体过程如下:

----->  n! = (5 * 1) * (5 * 2) * (5
* 3)......* (5 * (min(n/5))) * (其他不能分解产生5的整数连乘,如3 4 7 8 等)

其中5的个数为
 min(n/5)    【取下界】  ,再取剩余数(1*2*3*...min(n/5))中5的个数

------>n!/(5^(min(n/5)))
  =  1 *2 *3 ....* min(n/5)   =  (5 * 1) * (5 * 2) * (5
* 3)......* (5 * (min(n/25))) * (其他不包含5的整数连乘)

其中5的个数为
 min(n/25)    【取下界】  ,再取剩余数(1*2*3*...min(n/25))中5的个数

------>依次类推,最后得到5的个数为
min(n/5)  + min (n/25) +.........    复杂度O(log5(n));

return
n/5
+ n/25
+ n/125
+ n/625
+ n/3125+...;

int trailingZeroes(int n) {
	//能被10整除多少次
	int count=0;
    while(n)
    {
        count = count +n/5;
        n=n/5;
    }
	return count;
}

#189 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].

旋转数组:这个题中 k具有周期性,周期为n, 也就是说 k>n 时  移动  k%n  步的移动结果 与移动 k 步的结果是一样的。

每个元素向前移动 k 步,(当然可以一次将所有元素移动一步,一共移动k次)

简单的想法是先保存一个原始数组,再将每个元素移动到其前面k个位置   算法如下:

或者是将数组分为 前numsSize-k个元素 和 后面 k  个元素两部分,然后把后面k个元素放到前面numsSize-k 个元素前面去。需要O(numsSize)空间

//10ms
void rotate(int* nums, int numsSize, int k) {
    //一个一个移动,k可以大于numsSize
	int *a;
    <span style="white-space:pre">	</span>int i;

	k = k%(numsSize);//移动1倍时还原---周期性

	a=(int *)malloc(sizeof(int)*numsSize);
    <span style="white-space:pre">	</span>if(k==0)
        <span style="white-space:pre">	</span>return;
	for(i = 0; i < numsSize; i++)
	<span style="white-space:pre">	</span>a[i] = nums[i];
	for(i = 0; i < numsSize; i++)
	{
		nums[(i+k)%numsSize] = a[i];
	}
}

时间复杂度为 O(k%numsSize)   空间复杂度为 O(n),事实上,移动时,可以先保存数据,相当于先挖坑再填坑。以达到O(1) 的空间复杂度

nums[(i+k)%numsSize] = a[i];

还有一种 in-place的做法,原地数组转置,算法如下:

//12ms
void reverse(int *nums, int left,int right)
{
	int temp;
	while(left<=right)
	{
		temp = nums[left];
		nums[left] = nums[right];
		nums[right] = temp;
		left++;
		right--;
	}
}

void rotate(int* nums, int numsSize, int k)
{
	k = k % numsSize;
	reverse(nums,0,numsSize-1);
	reverse(nums,0,k-1);
	reverse(nums,k,numsSize-1);
}

#190 Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

求二进制表示的32位无符号整数转置后的数。(每次通过 &1 取最低位    然后左移)

//4ms
uint32_t reverseBits(uint32_t n){
    int i,re=0;
    for (i = 0; i < 32; i++)
        re += (n >> i & 1)<<(31-i);
    return re;
}

#191 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1‘ bits it has (also known as the Hamming
weight
).

For example, the 32-bit integer ’11‘ has binary representation 00000000000000000000000000001011,
so the function should return 3.

求二进制数中1的个数,时间复杂度为O(k)  k=log2(n) 为二进制数的位数

//0ms
int hammingWeight(uint32_t n) {
    int count=0;
    while(n)
    {
        if(n&0x01==1)
            count++;
        n >>=1;
    }
    return count;
}
时间: 2024-08-03 12:12:40

Leetcode--easy系列8的相关文章

Leetcode permutation 系列

关于permutation的讲解,请参见http://blog.csdn.net/xuqingict/article/details/24840183 下列题目的讲解均是基于上面的文章: 题1: Next Permutation Total Accepted: 8066 Total Submissions: 32493My Submissions Implement next permutation, which rearranges numbers into the lexicographic

【Leetcode长征系列】Letter Combinations of a Phone Number

原题: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

[LeetCode蠕动系列]Sort List

这题前一阵子就看到了,一直没时间做,昨晚睡前想了想,要求n*log(n)以内的时间复杂度,第一时间想到的就是归并.快排和希尔排序(注:希尔排序时间为O(n^1.3),在数据量大于2的情况下小于n*log(n)),个人以为,链表的特性更适合归并,所以采用归并排序,实现的merge代码如下: public static ListNode merge(ListNode rhead, ListNode lhead) { ListNode head = null; if (rhead.val <= lhe

HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)

不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 30222 Accepted Submission(s): 12144 Problem Description 人称"AC女之杀手"的超级偶像LELE近期忽然玩起了深沉,这可急坏了众多"Cole"(LELE的粉丝,即

【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal

原题: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的子集,递归求解即可. 代码: /** * Definition for binary tree * struct Tre

【Leetcode长征系列】Single Number II

原题: Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 用一个32位的数组存每一位bit值之后.得到答案后每一位除

【Leetcode长征系列】Pow(x, n)

原题: Implement pow(x, n). 思路:递归计算pow. class Solution { public: double pow(double x, int n) { long long int mid = n/2; int d = n%2; if(n==0) return 1; if(n==1) return x; if(d==1) return pow(x, (n/2)+1) * pow(x, n/2); else return pow(x, n/2) * pow(x, n/

【Leetcode长征系列】Sqrt(x)

原题: Implement int sqrt(int x). Compute and return the square root of x. ==============================以下为引用==================================== 牛顿迭代法 为了方便理解,就先以本题为例: 计算x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,如左图所示. 首先取x0,如果x0不是解,做一个经过(x0,f(x0))这个点的切线,与x轴的交

【Leetcode长征系列】Balanced Binary Tree

原题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1. 思路:递归判断左右子树是否为BST. 代码: /** * Def