[Leetcode学习-c++&java]Beautiful Arrangement

问题:

难度:medium

说明:

给一个数字 N,然后返回可以 1 ~ N 以内 从 数组索引 1 开始,可以组成多少个完美数组,其中:

完美数组每一个元素符合:  arr[i] 可以被 i 整除,或者 i 可以被 arr[i] 整除。

题目连接:https://leetcode.com/problems/beautiful-arrangement/

输入范围:

  • 1 <= n <= 15

输入案例:

Example 1:
Input: n = 2
Output: 2
Explanation: 
The first beautiful arrangement is [1,2]:
    - perm[1] = 1 is divisible by i = 1
    - perm[2] = 2 is divisible by i = 2
The second beautiful arrangement is [2,1]:
    - perm[1] = 2 is divisible by i = 1
    - i = 2 is divisible by perm[2] = 1

Example 2:
Input: n = 1
Output: 1

我的代码:

一个暴力(全排列)就解决,如果快点就是写写数学公式,不过还有像打表这种不讲武德的东西,毕竟 N 是 15 以内。

Java:

class Solution {
    int count = 0;
    public int countArrangement(int n) {
        recurtion(n + 1, new boolean[n + 1], 1, 0);
        return count;
    }

    public void recurtion(int n, boolean[] visited, int index, int cur) {
        if(index == n) {
            count ++;
            return;
        }
        for(int i = 1;i < n;i ++) {
            if(i == cur) continue;
            if((i % index == 0 || index % i == 0) && !visited[i]) {
                visited[i] = true;
                recurtion(n, visited, index + 1, i);
                visited[i] = false;
            }
        }
    }
}

Java 打表:

class Solution {
    private static int[] map = new int[]{1, 2, 3, 8, 10, 36, 41, 132, 250, 700, 750, 4010, 4237, 10680, 24679};
    public int countArrangement(int n) {
        return map[n - 1];
    }
}

C++:

class Solution {
public:
	int count = 0;
	int countArrangement(int n) {
		short *visited = new short[n + 1];
		recurtion(n + 1, visited, 1, 0);
		return count;
	}
	void recurtion(int len, short *visited, int index, int cur) {
		if (index == len) {
			count++; return;
		}
		for (int i = 1; i < len; i++) {
			if (i == cur) continue;
			if ((i % index == 0 || index % i == 0) && visited[i]) {
				visited[i] = 0;
				recurtion(len, visited, index + 1, i);
				visited[i] = 1;
			}
		}
	}
};

C++就不打表了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值