Given a deck of cards, each card are written on an integer.
At this point, you need to select a digital X, so that we can be full decks of cards according to the following rules into one or more groups:
Each group has X cards.
Within the group are written the same integer on all the cards.
> = 2 returns true only if you optional X.
Example 1:
Input: [1,2,3,4,4,3,2,1]
Output: true
interpretation: grouping is possible [1,1], [2,2], [3,3], [4,4 ]
example 2:
Input: [1,1,1,2,2,2,3,3]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 3:
Input: [1]
Output: false
interpretation: the packet does not satisfy the requirements.
Example 4:
Input: [1,1]
Output: true
interpretation: grouping is possible [1,1]
Example 5:
Input: [1,1,2,2,2,2]
Output: true
interpretation: grouping is possible [1,1], [2,2], [2,2]
prompt:
1 <= deck.length <= 10000
0 <= deck[i] < 10000
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards
class Solution { public: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } bool hasGroupsSizeX(vector<int>& deck) { int a[10010]; memset(a,0,sizeof(a)); int num; int maxx=0; for(int i=0;i<deck.size();i++){ a[deck[i]]++; num=a[deck[i]]; maxx=max(maxx,deck[i]); } for(int i=0;i<=maxx;i++){ if(a[i]!=0)num=gcd(num,a[i]); } if(num==1)return false; return true; } };