Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 759 Bytes

1250.检查「好数组」.md

File metadata and controls

40 lines (34 loc) · 759 Bytes

1250.检查「好数组」

/*
 * @lc app=leetcode.cn id=1250 lang=typescript
 *
 * [1250] 检查「好数组」
 */

// @lc code=start
function isGoodArray(nums: number[]): boolean {}
// @lc code=end

解法 1: 数学

function isGoodArray(nums: number[]): boolean {
  function gcd(a: number, b: number): number {
    return b ? gcd(b, a % b) : a
  }
  let res = 0
  for (const a of nums) {
    res = gcd(res, a)
  }
  return res === 1
}

Case

test.each([
  { input: { nums: [12, 5, 7, 23] }, output: true },
  { input: { nums: [29, 6, 10] }, output: true },
  { input: { nums: [3, 6] }, output: false },
])('input: nums = $input.nums', ({ input: { nums }, output }) => {
  expect(isGoodArray(nums)).toEqual(output)
})