【leetcode】1250. Check If It Is a Good Array
题目如下:
Given an array
numsof 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 of1from the array by any possible subset and multiplicand.Return
Trueif 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^51 <= 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
【leetcode】1250. Check If It Is a Good Array的更多相关文章
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【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 ...
- 【LeetCode】1003. Check If Word Is Valid After Substitutions 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环 日期 题目地址:https://leetcod ...
- 【LeetCode】958. Check Completeness of a Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【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 ...
- 【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 ...
- 【LeetCode】448. Find All Numbers Disappeared in an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 方法一:暴力求解 方法二:原地变负做标记 方法三:使用set ...
- 【leetcode】448. Find All Numbers Disappeared in an Array
problem 448. Find All Numbers Disappeared in an Array solution: class Solution { public: vector<i ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
随机推荐
- Identification of Encryption Algorithm Using Decision Tree
本文主要做了两件事,一是提出了一种使用C4.5算法生成的决策树来识别密文所使用的加密算法的方法,二是为这一算法设计了一个特征提取系统提取八个特征作为算法的输入,最终实现了70%~75的准确率. 准备工 ...
- Linux里添加用户的一些简单命令
普通用户---------普通用户对应提示符$ 超级用户-----------超级用户对应提示符# 1.添加用户:useradd diqi 2.查看用户:tail -1 /etc/passwd 3.设 ...
- aws和ufile挂载数据盘EBS
aws的话挂载的ebs需要格式化,参考:https://docs.aws.amazon.com/zh_cn/AWSEC2/latest/UserGuide/ebs-using-volumes.html ...
- 定时执行exe、windows任务计划、windows服务
环境: Windows10 + VS2015 + SQL Server2014 + .NET Framework4.5 + C# + WCF 问题: 业务功能需要,做了一个windows应用程序供主程 ...
- 纯H5 AJAX文件上传加进度条功能
上传代码js部分 //包上传 $('.up_apk').change(function () { var obj = $(this); var form_data = new FormData(); ...
- 给网页中的button加动画效果
网页中的很多事件交互都是通过点击页面中的按钮来实现的,给按钮加一点动画效果也会让网页看起来生动一些,以下就是一个简单的例子: 此按钮的动画主要是通过css的transform动画,伪元素,伪类来实现: ...
- C# 面向对象7 命名空间
命名空间 **namespace(命名空间),用于解决类重名问题,可以看作"类的文件夹" **如果代码和被使用的类在一个namespace则不需要using **在不同命名空间下的 ...
- WPF ListView多行显示
//前台 <ListView Margin="14,152,12,74" Name="lvList" SelectionMode="Multip ...
- SpringDataJpa实体类常用注解
最近公司在使用SpringDataJpa时,需要创建实体类,通过实体类来创建数据库表结构,生成数据库表. 下面我们就来看下在创建实体类时一些常用的注解吧!!! 1.实体类常用注解 @Entity 标识 ...
- 查询SAP数据库表的大小
事物代码DB02 Perfomrmance->Additional Functions->SQL Command Editor->写数据表->执行 select bytes/1 ...
