[263] Ugly Number [Easy]

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

  1. //Author: Wanying
  2. //注意 0 和 1 的corner case, 你居然还没一次AC==
  3. //想好了再写,不然等着挂吧==!!!!!
  4. class Solution {
  5. public:
  6. bool isUgly(int num) {
  7. if (num == || num == ) {
  8. return num;
  9. }
  10. while(num % == ) {
  11. num = num / ;
  12. }
  13. while(num % == ) {
  14. num = num / ;
  15. }
  16. while(num % == ) {
  17. num = num / ;
  18. }
  19. return num == ;
  20. }
  21. };

[264] Ugly Number II [Medium]

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

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

  1. //Author: Wanying
  2. //DP: idx2, idx3, idx5
  3.  
  4. class Solution {
  5. public:
  6. int nthUglyNumber(int n) {
  7. vector<int> vec(,);
  8. int idx2 = , idx3 = , idx5 = ;
  9. for(size_t i = ; i <= n; ++i) {
  10. int v2 = vec[idx2] * , v3 = vec[idx3] * , v5 = vec[idx5] * ;
  11. int ith = min(v2, min(v3, v5));
  12. if (ith == v2) {
  13. idx2++;
  14. }
  15. if (ith == v3) {
  16. idx3++;
  17. }
  18. if (ith == v5) {
  19. idx5++;
  20. }
  21. vec.push_back(ith);
  22. }
  23. return vec[n-];
  24. }
  25. };

[313] Super Ugly Number [Medium]

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

  1. //Author: Wanying
  2. class Solution {
  3. public:
  4. int nthSuperUglyNumber(int n, vector<int>& primes) {
  5. vector<int> idx(primes.size(), );
  6. vector<int> ans(, );
  7. for (size_t i = ; i <= n; ++i) {
  8. int ith = INT_MAX;
  9. for (int j = ; j < primes.size(); ++j) {
  10. int tmp = ans[idx[j]] * primes[j];
  11. ith = min(tmp, ith);
  12. }
  13. for (int j = ; j < primes.size(); ++j) {
  14. int tmp = ans[idx[j]] * primes[j];
  15. if (ith == tmp) {
  16. idx[j] ++;
  17. }
  18. }
  19. ans.push_back(ith);
  20. }
  21. return ans[n-];
  22. }
  23. };

[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, 同理.....

  1. /*
  2. * Author: Wanying
  3. * Language: C++
  4. * Date: 2017/03/11
  5. * 考虑到25, 125 这种数字中不止一个5, 所以公式如下: ans = n/5 + n/25 + n/125 + ......
  6. * n/5 是移除所有单个的5, n/25是移除多余的5, 同理.....
  7. */
  8. class Solution {
  9. public:
  10. int trailingZeroes(int n) {
  11. int ans = ;
  12. long long x = ; //x一直乘5, int会溢出
  13. while (n >= x) {
  14. ans += n / x;
  15. x *= ;
  16. //cout << x << " ";
  17. }
  18. return ans;
  19. }
  20. };

[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 表示

  1. /*
  2. * author: Wanying
  3. * Date: 2017/03/11
  4. * 思路就是模拟竖式除法,用个map记录循环节
  5. * company: google
  6. */
  7.  
  8. class Solution {
  9. public:
  10. string fractionToDecimal(int numerator, int denominator) {
  11. long long n = numerator, d = denominator; //不能用int, corner case: n = INT_MIN, d = -1; 除完了越界
  12. string ans = "";
  13. if (n == ) return "";
  14. if (n < ^ d < ) {
  15. ans += "-";
  16. }
  17. n = abs(n), d = abs(d);
  18. ans += to_string(n/d);
  19. //能整除直接返回整数部分
  20. if (n % d == ) {
  21. return ans;
  22. }
  23. ans += ".";
  24. long long r = (n % d); // 余数也要用 long long, 不然(-1, -2147483648) 这组会溢出
  25. map<int, int> mp; //模拟竖式除法,找循环节
  26. while (r) {
  27. if (mp.count(r) > ) {
  28. ans.insert(mp[r], "(");
  29. ans += ")";
  30. break;
  31. }
  32. mp[r] = ans.size();
  33. cout << r << " ";
  34. r *= ;
  35. ans += to_string(r/d);
  36. r = r % d;
  37. }
  38. return ans;
  39. }
  40. };

【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. linux 系统磁盘管理体系

    目录 linux 系统磁盘管理体系 一.磁盘的基本概念 二.磁盘的内部结构 三.磁盘的外部结构 四.磁盘的接口及类型 五.fdisk磁盘分区实践 六.gdisk 分区 七.parted 高级分区工具. ...

  2. Educational Codeforces Round 69 E - Culture Code (最短路计数+线段树优化建图)

    题意:有n个空心物品,每个物品有外部体积outi和内部体积ini,如果ini>outj,那么j就可以套在i里面.现在我们要选出n个物品的一个子集,这个子集内的k个物品全部套在一起,且剩下的物品都 ...

  3. Codeforces 360C DP 计算贡献

    题意:给你一个长度为n的字符串,定义两个字符串的相关度为两个串对应的子串中第一个串字典序大于第二个串的个数.现在给你相关度,和第二个串,问满足条件的第一个串有多少个? 思路:设dp[i][j]为填了前 ...

  4. oracle10G rac asm 安装总结

    前言 安装步骤是参考三思博主(http://blog.chinaunix.net/uid-22741583-id-177284.html),安装的时候由于概念没搞清楚,急于求成,折腾了两天才顺利装完, ...

  5. eclipse里部署struts2

    Struts2是一个比较出色的基于MVC设计模式的框架,是由Struts1和WebWork发展而来的,性能也比较稳定,现在是Apache软件基金会的一个项目,下面就来配置Struts2进行初始化的开发 ...

  6. ActiveMQ消息中间件Producer和Consumer

    ActiveMQ消息中间件Producer和Consumer 原创jethai2015-08-18 18:08:56评论(0)1480人阅读   生产者代码: 1 2 3 4 5 6 7 8 9 10 ...

  7. VC连接SQLite3的方法(MFC封装类)

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,支持跨平台,操作简单,能够使用很多语言直接创建数据库.官方网站:www.sqlite.org 在VC环境下编写连接SQLite的 ...

  8. uploadify的使用错误

    在看singwa的视频教程中,学习使用hui-admin模版,在使用uploadify插件上传图片中出现错误. ReferenceError: Can't find variable: $因为使用JQ ...

  9. python 比较俩个列表中的元素是否相同

    如果元素都是数字: # a = [121, 144, 19, 161, 19, 144, 19, 11]# b = [121, 14641, 20736, 361, 25921, 361, 20736 ...

  10. Java BIO socket

    package org.rx.socks; import lombok.extern.slf4j.Slf4j; import org.rx.core.LogWriter; import org.rx. ...