2021-12-30 846. 一手顺子

2021-12-30  本文已影响0人  16孙一凡通工

排序加哈希表:排序,接着用哈希表存储元素出现的次数,然后遍历排序后的数组,从低元素出发,判定是否满足接下来的递增元素存在于哈希表当中,不满足就输出false.

Go版本:

func isNStraightHand(hand []int, groupSize int) bool {
sort.Ints(hand);
hashmap:=make(map[int]int);
for _,value:=range hand{
    hashmap[value]++;
}
for _,value:=range hand{
  if hashmap[value]<=0{
      continue;
  }
  for i:=0;i<groupSize;i++{
      if hashmap[value+i]<=0{
         return false;
      }
      hashmap[value+i]--;
  }

}
return true;


}
上一篇 下一篇

猜你喜欢

热点阅读