LeetCode914. Card Packet

Subject description:

给定一副牌,每张牌上都写着一个整数。

此时,你需要选定一个数字 X,使我们可以将整副牌按下述规则分成 1 组或更多组:

每组都有 X 张牌。
组内所有的牌上都写着相同的整数。
仅当你可选的 X >= 2 时返回 true。

 

示例 1:

输入:[1,2,3,4,4,3,2,1]
输出:true
解释:可行的分组是 [1,1][2,2][3,3][4,4]
示例 2:

输入:[1,1,1,2,2,2,3,3]
输出:false
解释:没有满足要求的分组。
示例 3:

输入:[1]
输出:false
解释:没有满足要求的分组。
示例 4:

输入:[1,1]
输出:true
解释:可行的分组是 [1,1]
示例 5:

输入:[1,1,2,2,2,2]
输出:true
解释:可行的分组是 [1,1][2,2][2,2]

提示:

1 <= deck.length <= 10000
0 <= deck[i] < 10000
 

Outline of Solution 1:
1. This means that if the problem of the whole deck is divided into groups and x values are the same each card
2. Methods enumeration, each X to try them, 2 <= X < deck.length =
3. Comparative values in each set as if. At first I wanted each group points out, but as long as the pacesetters record of each group can compare the
usage 4.break is out of a loop, where we can continue to find the specified loop
problem-solving Code:

public class Solution {
	public boolean hasGroupsSizeX(int[] deck) {
		Arrays.sort(deck);
		int m = 0;
		int x;
		search:for (x = 2; x <= deck.length; x++) {
			if (deck.length % x == 0) {
				for (int i = 0; i < deck.length; i++) {
					if (i % x == 0) {
						m = deck[i];
					}
					if (deck[i] != m) {
						continue search;
					} else if (i == deck.length - 1) {
						return true;
					}
				}
			} else {
				continue;
			}
		}
		return false;
	}


Problem-solving ideas 2:
is the official title of the solution
1. The number of times each number appears stored in an array
2 and then use this number to compare X, X is also need to enumerate
3. The number of occurrences of X may also be about the number of
problem-solving Code:

class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        int N = deck.length;
        int[] count = new int[10000];
        for (int c: deck)
            count[c]++;

        List<Integer> values = new ArrayList();
        for (int i = 0; i < 10000; ++i)
            if (count[i] > 0)
                values.add(count[i]);

        search: for (int X = 2; X <= N; ++X)
            if (N % X == 0) {
                for (int v: values)
                    if (v % X != 0)
                        continue search;
                return true;
            }

        return false;
    }
}


Outline of Solution 3:
1. Since the X must be a divisor of all count [i], so long as X is a number from about all count [i] is the greatest common divisor to
solving the code:

class Solution {
    public boolean hasGroupsSizeX(int[] deck) {
        int[] count = new int[10000];
        for (int c: deck)
            count[c]++;

        int g = -1;
        for (int i = 0; i < 10000; ++i)
            if (count[i] > 0) {
                if (g == -1)
                    g = count[i];
                else
                    g = gcd(g, count[i]);
            }

        return g >= 2;
    }

    public int gcd(int x, int y) {
        return x == 0 ? y : gcd(y%x, x);
    }
}



Published 68 original articles · won praise 32 · views 6622

Guess you like

Origin blog.csdn.net/qq_44867340/article/details/105146972