资讯专栏INFORMATION COLUMN

526. Beautiful Arrangement and 254. Factor Combina

I_Am / 3497人阅读

摘要:用的思想,加上题目的特殊意思,来解决问题。如果个数字,有种结果。这里对原题多加了一个输出的要求,代码如下。也是为了去重,两个分解的解法是对称的,乘法对称的重点就是

用combination的思想,加上题目的特殊意思,来解决问题。

</>复制代码

  1. 526 Beautiful Arrangement

</>复制代码

  1. Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:
  2. 1.The number at the ith position is divisible by i.
  3. 2.i is divisible by the number at the ith position.
  4. 如果3个数字,有3种结果。
  5. [1, 2, 3]
  6. [2, 1, 3]
  7. [3, 2, 1]
  8. 3
  9. 这里对Leetcode原题多加了一个输出的要求,代码如下。

</>复制代码

  1. public Solution{
  2. int count = 0;
  3. public int countArrangement(int N) {
  4. if (N == 0) return 0;
  5. helper(N, 1, new int[N + 1], new ArrayList());
  6. return count;
  7. }
  8. private void helper(int N, int pos, int[] used, List list) {
  9. if (pos > N) {
  10. count++;
  11. System.out.println(list.toString());
  12. return;
  13. }
  14. for (int i = 1; i <= N; i++) {
  15. if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) {
  16. used[i] = 1;
  17. list.add(i);
  18. helper(N, pos + 1, used, list);
  19. list.remove(list.size()-1);
  20. used[i] = 0;
  21. }
  22. }
  23. }
  24. }

</>复制代码

  1. 254 Factor Combinations

</>复制代码

  1. 比如给出的数字是12,质因数分解的结果如下:
  2. [
  3. [2, 6],
  4. [2, 2, 3],
  5. [3, 4]
  6. ]

</>复制代码

  1. public class Solution {
  2. public List> getFactors(int n) {
  3. List> res = new ArrayList>();
  4. if(n <= 3) return res;
  5. dfs(n, res, new ArrayList(), -1);
  6. return res;
  7. }
  8. public void dfs(int n, List> res, List path, int lower){
  9. // 和一般combination不同的是,每一步都要导入到结果中。
  10. if(lower != -1){
  11. path.add(n);
  12. res.add(new ArrayList<>(path));
  13. path.remove(path.size()-1);
  14. }
  15. // lower 是为了解决重复分解的问题,比如12 = 2*2*3 = 3*2*2,但是后一个是重复的, 所以分解之后factor不能变小。
  16. // upper 也是为了去重,12 = 3*4 = 4*3, 两个分解的解法是对称的,乘法对称的重点就是sqrt(n).
  17. int upper = (int) Math.sqrt(n);
  18. for(int i=Math.max(2, lower); i<= upper; i++){
  19. if(n%i == 0){
  20. path.add(i);
  21. dfs(n/i, res, path, i);
  22. path.remove(path.size()-1);
  23. }
  24. }
  25. }
  26. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66950.html

相关文章

  • Leetcode 相似题只有题号不含代码。

    找出string里的单词。 186. Reverse Words in a String II, 434. Number of Segments in a String combination类型题 77. Combinations 39. Combination Sum 40. Combination Sum II 216. Combination Sum III 494. Target S...

    StonePanda 评论0 收藏0
  • [LeetCode] 526. Beautiful Arrangement

    Problem Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position ...

    GeekQiaQia 评论0 收藏0
  • [LeetCode] 254. Factor Combinations

    Problem Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4.Write a function that takes an integer n and return all possible combinations of its factors. Note: You ...

    Leck1e 评论0 收藏0
  • 容器最大盛水量

    摘要:容器最大盛水量给定个非负整数,,,,其中每个表示坐标,处的点。找到两条线,它们与轴一起形成一个容器,使得容器含有最多的水。 容器最大盛水量 Container With Most Water 给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点。 绘制n条垂直线,使得线i的两个端点在(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,使得容器...

    luckyw 评论0 收藏0
  • 如何使用ABSL代码调用Web service

    摘要:需求在里创建,然后通过代码消费。创建一个新的基于这个标准的创建一个因为我是在当前系统上的里调用当前系统提供的,所以选择当然这个的也是需要在这个地方自己创建一个的可以维护成给该创建的维护的将该的下载到本地。基于前一步创建的创建一个。 需求:在C4C UI里创建web service(maintain ticket),然后通过ABSL代码消费。1. 创建一个新的Communication ...

    ASCH 评论0 收藏0

发表评论

0条评论

I_Am

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<