美文网首页
[LeetCode] 914. X of a Kind in a

[LeetCode] 914. X of a Kind in a

作者: 弱花 | 来源:发表于2018-11-02 11:37 被阅读0次

原题

题目原意可转换为
两组有大于等于2的公因数

/**
 * @param {number[]} deck
 * @return {boolean}
 */
var hasGroupsSizeX = function(deck) {
  var map = {};
  for (let i = 0; i < deck.length; i++) {
    if (map[deck[i]])
      map[deck[i]] += 1;
    else
      map[deck[i]] = 1;
  }
  var min = map[deck[0]];
  for (var i in map) {
    if (map[i] <= min) {
      min = map[i];
    }
  }
  if (min < 2) {
    return false;
  }
  var flag;
  for (var i = 2; i <= min; i++) {
    flag = true;
    for (var k in map) {
      if (map[k] % i != 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      return true;
    }
  }
  return false;
};

相关文章

网友评论

      本文标题:[LeetCode] 914. X of a Kind in a

      本文链接:https://www.haomeiwen.com/subject/feiqxqtx.html