[263] Ugly Number [Easy]

一个数的质因子只有2,3,5就叫丑数,写个函数判断丑数。

 //Author: Wanying
//注意 0 和 1 的corner case, 你居然还没一次AC==
//想好了再写,不然等着挂吧==!!!!!
class Solution {
public:
bool isUgly(int num) {
if (num == || num == ) {
return num;
}
while(num % == ) {
num = num / ;
}
while(num % == ) {
num = num / ;
}
while(num % == ) {
num = num / ;
}
return num == ;
}
};

[264] Ugly Number II [Medium]

第一个丑数是1,返回第n个丑数是多少。

思路:dp, 第u个丑数一定是 min(vec[idx2]*2, vec[idx3]*3, vec[idx5] *5) || 所以,要存idx2,idx3,idx5, 而且vec里面不能有重复的丑数.

 //Author: Wanying
//DP: idx2, idx3, idx5 class Solution {
public:
int nthUglyNumber(int n) {
vector<int> vec(,);
int idx2 = , idx3 = , idx5 = ;
for(size_t i = ; i <= n; ++i) {
int v2 = vec[idx2] * , v3 = vec[idx3] * , v5 = vec[idx5] * ;
int ith = min(v2, min(v3, v5));
if (ith == v2) {
idx2++;
}
if (ith == v3) {
idx3++;
}
if (ith == v5) {
idx5++;
}
vec.push_back(ith);
}
return vec[n-];
}
};

[313] Super Ugly Number [Medium]

跟Ugly Number II类似, 变化在于质因子不是[2,3,5]了,变成了透传进来的数组。 所以思路就从用3个idx变成了用一个数组的idx,其他一样,注意bug-free.

 //Author: Wanying
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
vector<int> idx(primes.size(), );
vector<int> ans(, );
for (size_t i = ; i <= n; ++i) {
int ith = INT_MAX;
for (int j = ; j < primes.size(); ++j) {
int tmp = ans[idx[j]] * primes[j];
ith = min(tmp, ith);
}
for (int j = ; j < primes.size(); ++j) {
int tmp = ans[idx[j]] * primes[j];
if (ith == tmp) {
idx[j] ++;
}
}
ans.push_back(ith);
}
return ans[n-];
}
};

[172] Factorial Trailing Zeroes [Easy]

给定一个数n, 求出n!中结尾0的个数。

考虑到造成结尾0的,只有5*2的倍数, 或者直接10, 100这种,所以,我们就用 n/5 得到n中5的个数,但是可能还有25,125这种含有不止一个5的情况,所以公式如下:

ans = n/5 + n/25 + n/125 + ......

n/5 是移除所有单个的5, n/25是移除多余的5, 同理.....

 /*
* Author: Wanying
* Language: C++
* Date: 2017/03/11
* 考虑到25, 125 这种数字中不止一个5, 所以公式如下: ans = n/5 + n/25 + n/125 + ......
* n/5 是移除所有单个的5, n/25是移除多余的5, 同理.....
*/
class Solution {
public:
int trailingZeroes(int n) {
int ans = ;
long long x = ; //x一直乘5, int会溢出
while (n >= x) {
ans += n / x;
x *= ;
//cout << x << " ";
}
return ans;
}
};

[166] Fraction to Recurring Decimal [Medium]

给两个数,分别代表分子,分母,求小数的表示形式,可能有循环节

  • Given numerator = 1, denominator = 2, return "0.5".
  • Given numerator = 2, denominator = 1, return "2".
  • Given numerator = 2, denominator = 3, return "0.(6)".

模拟竖式除法, 用个map记录循环节,还是不懂的话,自己在草稿纸上写个 3/444 你就懂了 :)

注意corner case, int可能越界, 全用long long 表示

 /*
* author: Wanying
* Date: 2017/03/11
* 思路就是模拟竖式除法,用个map记录循环节
* company: google
*/ class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
long long n = numerator, d = denominator; //不能用int, corner case: n = INT_MIN, d = -1; 除完了越界
string ans = "";
if (n == ) return "";
if (n < ^ d < ) {
ans += "-";
}
n = abs(n), d = abs(d);
ans += to_string(n/d);
//能整除直接返回整数部分
if (n % d == ) {
return ans;
}
ans += ".";
long long r = (n % d); // 余数也要用 long long, 不然(-1, -2147483648) 这组会溢出
map<int, int> mp; //模拟竖式除法,找循环节
while (r) {
if (mp.count(r) > ) {
ans.insert(mp[r], "(");
ans += ")";
break;
}
mp[r] = ans.size();
cout << r << " ";
r *= ;
ans += to_string(r/d);
r = r % d;
}
return ans;
}
};

【LeetCode】Math的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  3. 【LeetCode】746. 使用最小花费爬楼梯

    使用最小花费爬楼梯 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或 ...

  4. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  5. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  6. 【Leetcode】104. 二叉树的最大深度

    题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

  7. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. sql datetime类型数据如果进行模糊查询

    select * from Table1 where CONVERT(nvarchar(50),CreateTime,120) like '%2019'

  2. tcpdump 与 抓包分析

    在Windows下一般使用WireShark 抓包软件,tcpdump 是 Linux 系统的抓包软件.它可以抓取 TCP/IP 协议族的数据包,支持针对网络层.协议.主机.端口的过滤,并提供 and ...

  3. 【串线篇】概述SpringMvc和spring整合

    SpringMVC和Spring整合的目的:分工明确: SpringMVC的配置文件就来配置和网站转发逻辑以及网站功能有关的(视图解析器,文件上传解析器,支持ajax,xxx):springmvc.x ...

  4. C# 使用猫拨打电话

    主窗口一个textbox与btnstart按钮 代码是使用别人!只是去掉部分不用的!只用于拨号!用于辅助打电话! form1 using System; using System.Collection ...

  5. JavaSE---多线程---集合类

    1.概述 1.1 Java提供的ArrayList.LinkedList.HashMap等都是线程不安全的类,如何解决: 1.1 使用Collections类提供的方法将  其  包装成线程安全类: ...

  6. JAXB中的@XmlRootElement注解详解

    @Retention(value=RUNTIME) @Target(value=TYPE) public @interface XmlRootElement @Inherited @Retention ...

  7. UNP学习 多播

    一.概述 单播地址标识单个接口,广播地址标识子网上的所有接口,多播地址标识一组接口. 单播和广播是编址方案的两个极端,多播的目的就在于提供一种折衷的方案. 二.多播地址 我们必须区分IPv4多播地址和 ...

  8. 【LeetCode 32】最长有效括号

    题目链接 [题解] 设dp[i]表示以第i个字符结尾的最长有效括号的长度. 显然只要考虑s[i]==')'的情况 则如果s[i-1]=='(',则dp[i] = dp[i-2]+2; 如果s[i-1] ...

  9. 【Shiro】五、Apache Shiro加密

    Shiro提供了更好封装,更好使用的加密算法API,可以作为平时使用的一个工具类的预选方案. Shiro的密码学 基本特性 接口驱动,基于POJO 对JCE(Java Cryptography Ext ...

  10. eclipse run error:g++ not found in Path

    网上有人说: 在eclipse下 windows-->Preference-->C/C++-->Build-->Setting然后选择Discovery标签,将里面的内容全部R ...