【LeetCode】Math

[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 == 0 || num == 1) {
 8             return num;
 9         }
10         while(num % 2 == 0) {
11             num = num / 2 ;
12         }
13         while(num % 3 == 0) {
14             num = num / 3;
15         }
16         while(num % 5 == 0) {
17             num = num / 5;
18         }
19         return num == 1;
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(1,1);
 8         int idx2 = 0, idx3 = 0, idx5 = 0;
 9         for(size_t i = 2; i <= n; ++i) {
10             int v2 = vec[idx2] * 2, v3 = vec[idx3] * 3, v5 = vec[idx5] * 5;
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-1];
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(), 0);
 6         vector<int> ans(1, 1);
 7         for (size_t i = 2; i <= n; ++i) {
 8             int ith = INT_MAX;
 9             for (int j = 0; j < primes.size(); ++j) {
10                 int tmp = ans[idx[j]] * primes[j];
11                 ith = min(tmp, ith);
12             }
13             for (int j = 0; 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-1];
22     }
23 };

时间: 2024-10-09 11:09:22

【LeetCode】Math的相关文章

【LeetCode】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

【LeetCode】Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

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

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】编辑距离

dp方程“ 1.初始化:dp[0][i]=i; dp[j][0]=j; 2.dp[i][j]=         dp[i-1][j-1](相等) dp[i-1][j]+1 ,,dp[i][j-1]+1; dp[i-1][j-1] (这个对应是改的况) 注意字符串下标开始位置就OK了 1 public class Solution { 2 public int minDistance(String word1, String word2) { 3 char c1[]=word1.toCharArr

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<