【leetcode】1250. Check If It Is a Good Array
2024-09-04 08:41:16
题目如下:
Given an array
nums
of positive integers. Your task is to select some subset ofnums
, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of1
from the array by any possible subset and multiplicand.Return
True
if the array is good otherwise returnFalse
.Example 1:
Input: nums = [12,5,7,23]
Output: true
Explanation: Pick numbers 5 and 7.
5*3 + 7*(-2) = 1Example 2:
Input: nums = [29,6,10]
Output: true
Explanation: Pick numbers 29, 6 and 10.
29*1 + 6*(-3) + 10*(-1) = 1Example 3:
Input: nums = [3,6]
Output: falseConstraints:
1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
解题思路:看到这个题目,大家或许能猜到这题对应着数学定律。至于是什么定律,我是不知道的。后来网上搜索才发现对应的定律是裴蜀定理,最后的解法就是求出所有元素的最大公约数,判断是否为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
最新文章
- 算法与数据结构(十一) 平衡二叉树(AVL树)
- .NET应用架构设计—面向查询的领域驱动设计实践(调整传统三层架构,外加维护型的业务开关)
- CAS学习笔记(二)—— cas server端的login-webflow详细流程
- json 递归查找某个节点
- MVC 返回图片
- linux gdb 没有符号表被读取。请使用 ";file"; 命令。
- sql中写标量函数生成大写拼音首字母
- 条款21:必须返回对象object时,不要返回其引用reference
- Android videoview循环播放视频
- 【转】Linux mount/unmount命令
- 使用MSBUILD 构建时出错 error MSB3086: Task could not find ";sgen.exe"; using the SdkToolsPath的解决方法
- andeoid学习笔记七
- php的标记形式
- MFC error C2065: “IDD_DIALOG1” : 未声明的标识符 转载
- iOS UIView非常用方法及属性详解
- flex eclipse综合spring入门
- BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
- python-----双色球实现(实例1)
- Sublime Text安装与配置
- Linux patch命令详解
热门文章
- ElasticSearch - activemq - tomcat 开机自启动
- Angular中引入外部js的使用方式
- PTA(Basic Level)1014.福尔摩斯的约会 &;&; PTA(Advanced Level)1061.Dating
- Rate Limiter
- Luogu P3511 [POI2010]MOS-Bridges
- MySQL之无限级分类表设计
- 51nod 1251 Fox序列的数量 (容斥)
- O016、搭建实验环境
- Python爬虫 Selenium与PhantomJS
- 07 MySQL之索引原理