【leetcode】1250. Check If It Is a Good Array
题目如下:
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
【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 ...
随机推荐
- 【计算机视觉】背景建模--Vibe 算法优缺点分析
一.Vibe 算法的优点 Vibe背景建模为运动目标检测研究邻域开拓了新思路,是一种新颖.快速及有效的运动目标检测算法.其优点有以下两点: 1.思想简单,易于实现.Vibe通常随机选取邻域20个样本为 ...
- HDU 1231 最大连续子序列 (动态规划)
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 2019牛客暑期多校训练营(第二场)-D Kth Minimum Clique
题目链接:https://ac.nowcoder.com/acm/contest/882/D 题意:求给定点权无向图中,点权和第k小的完全子图的点权和.(包括空集) 思路:从空集开始,每找到一个完全子 ...
- 牛客小白月赛12-C(欧拉筛解积性方程)
题目链接:https://ac.nowcoder.com/acm/contest/392/C 题意:给定n,求: 思路:令res[i]=iN (%MOD),因为xn是一个积性函数,即(x*y)n=x ...
- 【转帖】Linux上,最常用的一批命令解析(10年精选)
Linux上,最常用的一批命令解析(10年精选) https://juejin.im/post/5d134fbfe51d4510727c80d1 写的挺好呢 Linux这么多命令,通常会让初学者望而生 ...
- ssl安全验证
#ssl验证 r=requests.get('https://www.12306.cn',verify=False) print(r.content.decode('utf-8')) 结果:
- 版本控制器之SVN(二)
安装重启以后,在菜单栏找到TortoiseSVN程序 启动以后 点击: 填写相应的信息: 可以看到项目的相关信息 选中仓库,右键 > Browse Repository 进入如下界面: 可以打开 ...
- 第一次编译ffmpeg
今天开始玩ffmpeg了. 从官网下载来的压缩包,不会编译诶,于是我开始研究起来了. 下面就是实时记录的随笔: 首先是从官网下载来的ffmpeg,就是下面这个版本,目前的最新版吧. http://ff ...
- Dango之视图函数
1.request对象 HTTPRequest对象就是咱们的视图函数的参数request def home(request): print(request) #<WSGIRequest: GET ...
- 【Java】Java程序报错:EXCEPTION_ACCESS_VIOLATION (0xc0000005)
运行Java程序的时候,报错:EXCEPTION_ACCESS_VIOLATION (0xc0000005): 根据原网页的说明: EXCEPTION_ACCESS_VIOLATION In rare ...