263:丑数
This commit is contained in:
parent
46756d2874
commit
7553e0a0e0
@ -11,11 +11,11 @@ import java.util.stream.Collectors;
|
||||
public class LcpSolution {
|
||||
public static void main(String[] args) {
|
||||
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 target = 10;
|
||||
// System.out.println(solution.purchasePlans(nums, target));
|
||||
int[] nums = {2, 2, 3, 5};
|
||||
int target = 6;
|
||||
System.out.println(solution.purchasePlans(nums, target));
|
||||
}
|
||||
|
||||
public int magicTower(int[] nums) {
|
||||
@ -67,34 +67,56 @@ public class LcpSolution {
|
||||
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) {
|
||||
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));
|
||||
int[] sort = new int[target];
|
||||
long[] count = new long[target];
|
||||
for (int num : nums) {
|
||||
if (num < target) {
|
||||
sort[num] += 1;
|
||||
}
|
||||
}
|
||||
long sum = 0;
|
||||
for (int i = 1; i < target; i++) {
|
||||
sum += sort[i];
|
||||
count[i] = sum;
|
||||
}
|
||||
long result = 0;
|
||||
for (int num : nums) {
|
||||
if (target > num) {
|
||||
result += num <= target - num ? count[target - num] - 1 : count[target - num];
|
||||
}
|
||||
}
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
return (int) (result / 2 % (Math.pow(10, 9) + 7));
|
||||
}
|
||||
}
|
||||
|
78
LeetCode/src/main/java/leetcode/editor/cn/UglyNumber.java
Normal file
78
LeetCode/src/main/java/leetcode/editor/cn/UglyNumber.java
Normal file
@ -0,0 +1,78 @@
|
||||
//给你一个整数 n ,请你判断 n 是否为 丑数 。如果是,返回 true ;否则,返回 false 。
|
||||
//
|
||||
// 丑数 就是只包含质因数 2、3 和/或 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)
|
||||
|
||||
}
|
45
LeetCode/src/main/java/leetcode/editor/cn/UglyNumber.md
Normal file
45
LeetCode/src/main/java/leetcode/editor/cn/UglyNumber.md
Normal 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
Loading…
Reference in New Issue
Block a user