263:丑数

This commit is contained in:
huangge1199@hotmail.com 2021-04-10 13:09:06 +08:00
parent 46756d2874
commit 7553e0a0e0
5 changed files with 177 additions and 32 deletions

View File

@ -11,11 +11,11 @@ import java.util.stream.Collectors;
public class LcpSolution { public class LcpSolution {
public static void main(String[] args) { public static void main(String[] args) {
LcpSolution solution = new LcpSolution(); LcpSolution solution = new LcpSolution();
System.out.println(solution.orchestraLayout(4, 1, 2)); // System.out.println(solution.orchestraLayout(4, 1, 2));
// int[] nums = {2, 2, 1, 9}; int[] nums = {2, 2, 3, 5};
// int target = 10; int target = 6;
// System.out.println(solution.purchasePlans(nums, target)); System.out.println(solution.purchasePlans(nums, target));
} }
public int magicTower(int[] nums) { public int magicTower(int[] nums) {
@ -67,34 +67,56 @@ public class LcpSolution {
return result % 9; return result % 9;
} }
// public int purchasePlans(int[] nums, int target) {
// long count = 0;
// List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
// Collections.sort(numList);
// Map<Integer, Long> map = new HashMap<>();
// int temp = -1;
// for (int num : numList) {
// if (num >= target) {
// break;
// }
// if (temp == -1) {
// for (int i = 1; i < num; i++) {
// map.put(i, Long.parseLong("0"));
// }
// map.put(num, Long.parseLong("1"));
// } else {
// if (map.containsKey(target - num)) {
// count += map.get(target - num);
// } else {
// count += map.get(temp);
// }
// for (int i = temp + 1; i < num; i++) {
// map.put(i, map.get(temp));
// }
// map.put(num, map.get(temp) + 1);
// }
// temp = num;
// }
// return (int) (count % (Math.pow(10, 9) + 7));
// }
public int purchasePlans(int[] nums, int target) { public int purchasePlans(int[] nums, int target) {
long count = 0; int[] sort = new int[target];
List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList()); long[] count = new long[target];
Collections.sort(numList); for (int num : nums) {
Map<Integer, Long> map = new HashMap<>(); if (num < target) {
int temp = -1; sort[num] += 1;
for (int num : numList) {
if (num >= target) {
break;
} }
if (temp == -1) {
for (int i = 1; i < num; i++) {
map.put(i, Long.parseLong("0"));
} }
map.put(num, Long.parseLong("1")); long sum = 0;
} else { for (int i = 1; i < target; i++) {
if (map.containsKey(target - num)) { sum += sort[i];
count += map.get(target - num); count[i] = sum;
} else {
count += map.get(temp);
} }
for (int i = temp + 1; i < num; i++) { long result = 0;
map.put(i, map.get(temp)); for (int num : nums) {
if (target > num) {
result += num <= target - num ? count[target - num] - 1 : count[target - num];
} }
map.put(num, map.get(temp) + 1);
} }
temp = num; Calendar calendar = Calendar.getInstance();
} return (int) (result / 2 % (Math.pow(10, 9) + 7));
return (int) (count % (Math.pow(10, 9) + 7));
} }
} }

View File

@ -0,0 +1,78 @@
//给你一个整数 n 请你判断 n 是否为 丑数 如果是返回 true 否则返回 false
//
// 丑数 就是只包含质因数 23 / 5 的正整数
//
//
//
// 示例 1
//
//
//输入n = 6
//输出true
//解释6 = 2 × 3
//
// 示例 2
//
//
//输入n = 8
//输出true
//解释8 = 2 × 2 × 2
//
//
// 示例 3
//
//
//输入n = 14
//输出false
//解释14 不是丑数因为它包含了另外一个质因数 7
//
//
// 示例 4
//
//
//输入n = 1
//输出true
//解释1 通常被视为丑数
//
//
//
//
// 提示
//
//
// -231 <= n <= 231 - 1
//
// Related Topics 数学
// 👍 221 👎 0
package leetcode.editor.cn;
//263:丑数
public class UglyNumber {
public static void main(String[] args) {
//测试代码
Solution solution = new UglyNumber().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isUgly(int n) {
if (n <= 0) {
return false;
}
while (n % 5 == 0) {
n = n / 5;
}
while (n % 3 == 0) {
n = n / 3;
}
while (n % 2 == 0) {
n = n / 2;
}
return n == 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,45 @@
<p>给你一个整数 <code>n</code> ,请你判断 <code>n</code> 是否为 <strong>丑数</strong> 。如果是,返回 <code>true</code> ;否则,返回 <code>false</code></p>
<p><strong>丑数 </strong>就是只包含质因数 <code>2</code><code>3</code> 和/或 <code>5</code> 的正整数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 6
<strong>输出:</strong>true
<strong>解释:</strong>6 = 2 × 3</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 8
<strong>输出:</strong>true
<strong>解释:</strong>8 = 2 × 2 × 2
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>n = 14
<strong>输出:</strong>false
<strong>解释:</strong>14 不是丑数,因为它包含了另外一个质因数 <code>7 </code>
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>true
<strong>解释:</strong>1 通常被视为丑数。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div>\n<div><li>👍 221</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long