题目如下:

Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand.

Return True if the array is good otherwise return False.

Example 1:

Input: nums = [12,5,7,23]
Output: true
Explanation: Pick numbers 5 and 7.
5*3 + 7*(-2) = 1

Example 2:

Input: nums = [29,6,10]
Output: true
Explanation: Pick numbers 29, 6 and 10.
29*1 + 6*(-3) + 10*(-1) = 1

Example 3:

Input: nums = [3,6]
Output: false 

Constraints:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

解题思路:看到这个题目,大家或许能猜到这题对应着数学定律。至于是什么定律,我是不知道的。后来网上搜索才发现对应的定律是裴蜀定理,最后的解法就是求出所有元素的最大公约数,判断是否为1即可。

裴蜀定理(或贝祖定理,Bézout's identity)得名于法国数学家艾蒂安·裴蜀,说明了对任何整数a、b和它们的最大公约

数d,关于未知数x和y的线性不定方程(称为裴蜀等式):若a,b是整数,且gcd(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别地,一定存在整数x,y,使ax+by=d成立。

它的一个重要推论是:a,b互质的充要条件是存在整数x,y使ax+by=1。

代码如下:

class Solution(object):
def isGoodArray(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def gcd(m, n):
if not n:
return m
else:
return gcd(n, m % n)
val = nums[0]
for i in range(1,len(nums)):
val = gcd(val,nums[i])
return val == 1

【leetcode】1250. Check If It Is a Good Array的更多相关文章

  1. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  2. 【leetcode】1232. Check If It Is a Straight Line

    题目如下: You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coord ...

  3. 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...

  4. 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 【leetcode】1003. Check If Word Is Valid After Substitutions

    题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...

  6. 【leetcode】958. Check Completeness of a Binary Tree

    题目如下: Given a binary tree, determine if it is a complete binary tree. Definition of a complete binar ...

  7. 【LeetCode】448. Find All Numbers Disappeared in an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 方法一:暴力求解 方法二:原地变负做标记 方法三:使用set ...

  8. 【leetcode】448. Find All Numbers Disappeared in an Array

    problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...

  9. 【leetcode】 First Missing Positive

    [LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...

随机推荐

  1. C语言|作业12—学期总结

    一. 我学到的内容 二. 我的收获 作业链接 收获 C语言l博客作业01 对这个专业.学科以及markdown语法有了初步了解,打印出了"Hello world!" C语言l博客作 ...

  2. 2017.10.28 C组比赛总结

    这次比赛有点坑... [GDKOI2004]石子游戏 方法:判断奇偶性 输入n 如果n是奇数,输出 xiaoshi 如果n是偶数,输出 xiaoyong 比赛得分:30 错因:找错规律了(忘记了两个人 ...

  3. python多进程,并获取每个进程的返回值

    pool = multiprocessing.Pool(processes=10) row = [...] for row in rows: task_id = row[1] img_id = row ...

  4. 1,全局变量;2,图形验证码;3,解决bug的毅力

    通过这一整天的学习,主要解决了这三个: 1,全局变量 在函数外部定义: var gloabl: function test(){ global = " ":    //不能写成va ...

  5. Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...

  6. ccs之经典布局(一)(水平垂直居中)

    经典的css布局有以下几种,下面分别用不同的方法进行实现且进行对比. 一.水平居中 水平居中布局指的是当前元素在父级元素的容器中,水平方向上显示的是居中的,有以下几种方式来完成布局: 1.margin ...

  7. 帝国cms 重置用户名和密码

    5.1至7.0版本:用phpmyadmin修改phome_enewsuser表里的记录:把password字段的值设为:“322d3fef02fc39251436cb4522d29a71”:把salt ...

  8. countUp.js-数字滚动效果(简单基础使用)

    最近写了个移动端宣传页,里面有数字的效果,所以有使用到countUp.js. 以下内容有包括:h5页面countUp.js的引入和实例.参数说明.事件简单使用和描述.countUp.js源代码. 附上 ...

  9. Js 将图片的绝对路径转换为base64编码(3)

    图片文件改变一方法:$('#file').change(function(){var oFReader = new FileReader();oFReader.readAsDataURL(this.f ...

  10. 测试Random类nextInt()方法连续两次结果一样的概率

    public static void main(String[] args) { int count = 0; int a = 0; Random r = new Random(); while (t ...