LeetCode contest-95[876,877,👁878]
876. Middle of the Linked List
first submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
head2=head
l=1
while head.next!=None:
head=head.next
l+=1
w=l//2+1
l=1
while head2.next!=None:
if w==l:
break
head2=head2.next
l+=1
return head2
877. Stone Game
first submission
import time
class Solution:
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
Alex=0
Li=0
flag=True # t is alex,f is li
i=0
j=len(piles)-1
while i<j:
if piles[i]>=piles[j]:
num=piles[i]
i+=1
else:
num=piles[j]
j-=1
if flag:
Alex+=num
else:
Li+=num
return Alex>Li
if __name__ == "__main__":
data = [
{
"input":[5,3,4,5],
"output":True
},
];
for d in data:
print(d['input'])
# 计算运行时间
start = time.perf_counter()
result=Solution().stoneGame(d['input'])
end = time.perf_counter()
print(result)
if result==d['output']:
print("--- ok ---> spend time: ",end-start)
else:
print("--- error ---> spend time: ",end-start)
break
print()
else:
print("success")
用到了双指针哈哈,开森
878. Nth Magical Number
第N个神奇数字
如果正整数可以被 A 或 B 整除,那么它是神奇的。
返回第 N 个神奇数字。由于答案可能非常大,返回它模 10^9 + 7 的结果。
first submission
class Solution:
def nthMagicalNumber(self, N, A, B):
"""
:type N: int
:type A: int
:type B: int
:rtype: int
"""
num=0
if A>B:
A,B=B,A
ai=1
bi=1
for i in range(1,N+1):
print(i,ai,A*ai,bi,B*bi,end="")
if A*ai<B*bi:
num=A*ai
print("[1]",num)
ai+=1
elif A*ai==B*bi:
num=A*ai
print("[2]",num)
ai+=1
bi+=1
else:
num=B*bi
print("[3]",num)
bi+=1
return num
Time Limit Exceeded
Last executed input:
1000000000
40000
40000
超时是必然的。最后结束了,就做了两道题。这道超时
看下大神的答案【No.2 Neal@阳谷县 】
class Solution:
def gcd(self, a, b):
if 0 == b:
return a
return self.gcd(b, a % b)
def nthMagicalNumber(self, n, a, b):
"""
:type N: int
:type A: int
:type B: int
:rtype: int
"""
c = a * b // self.gcd(a, b)
lo, hi = 1, 1 << 60
while lo < hi:
mid = (lo + hi) // 2
t = mid // a + mid // b - mid // c
if t < n:
lo = mid + 1
else:
hi = mid
return lo % 1000000007
分析一下大神的解法
def gcd()
是求最大公约数
c = a * b // self.gcd(a, b)
求最小公倍数
lo, hi = 1, 1 << 60
构造一个大范围区间,[1,1<<60]
mid = (lo + hi) // 2
当前中点
t = mid // a + mid // b - mid // c
左半区间包含mid // a
个a
,mid // b
个b
,减去含有的最小公倍数个数mid//c
,结果t
则为左半区间满足数字的个数
if t < n:
lo = mid + 1
else:
hi = mid
和目标格式N
相比,下个目标区间
END.
第三题总结:其实我也也想到了要用最大公约数或者最小公倍数;想要判断AB含有重复的值。但是不知道可以定义一个大范围,然后二分法判断有多少个数字,以及最小公倍数的使用。数学又挡住了我。
LeetCode contest-95[876,877,👁878]的更多相关文章
- LeetCode Contest 166
LeetCode Contest 166 第一次知道LeetCode 也有比赛. 很久没有打过这种线上的比赛,很激动. 直接写题解吧 第一题 很弱智 class Solution { public: ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- leetcode contest 20
Q1: 520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right ...
- 【一天一道LeetCode】#95. Unique Binary Search Trees II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode OJ 95. Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode(95): 不同的二叉搜索树 II
Medium! 题目描述: 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [ [1,null,3,2], [3,2,null,1], ...
- [LeetCode&Python] Problem 876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- [leetcode tree]95. Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
随机推荐
- 福大软工 · BETA 版冲刺前准备(团队)
BETA 版冲刺前准备 队名:第三视角 作业链接 组长博客 应作业要求为了更加顺利地开展beta版本的冲刺,上次的alpha版本展示后,我们组对之前开发过程中存在的各种问题进行了全面的讨论,并对其进行 ...
- 划分树(poj2104)
poj2104 题意:给出n个数,有m次查询,每次查询要你找出 l 到 r 中第 k 大的数: 思路:划分树模板题 上述图片展现了查询时如何往下递推的过程 其中ly表示 [sl,l) 中有多少个数进入 ...
- ios 审核未通过 相机相册权限问题
苹果提交审核被打回来 附加的说明如下: We noticed that your app requests the user’s consent to access their camera but ...
- linux之目录知识
/var 目录下的路径知识: /var/log 记录系统及软件运行信息文件所在目录 /var/log/messages 系统级别日志文件 /var/log/secure 用户登录信息日志文件 / ...
- 根据ip获取地理信息
function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?form ...
- python day20面向对象-属性,类方法,静态方法
一.属性的初识 # class Person: # # def __init__(self,name,hight,weight): # self.name = name # self.__hight ...
- vs2017 使用Bower 抛出异常ECMDERR Failed to execute "git ls-remote --tags --heads
今天在使用Bower来下载vue包的时候,发现无法正常价新型,并且在输出窗口有以下提示 ECMDERR Failed to execute "git ls-remote --tags --h ...
- 将文件夹中的图像路径自动生成txt文件(便于opencv遍历处理图像)
代码: #include<iostream> #include<vector> #include<io.h> #include<fstream> usi ...
- 作用域&&闭包
在了解闭包之前,先了解作用域一,作用域简单来说就是变量和函数可以访问的范围,在es5中变量作用域一般分为全局作用域和局部作用域,这个主要依据是全局变量还是局部变量 情景1: <script> ...
- Selenium - 搭建环境
1. 在Python中安装第三方库 1)安装Selenium 通过pip安装 2). 下载geckodriverckod 从selenium3开始,webdriver/firefox/webdri ...