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 ...
随机推荐
- Mysql服务配置优化
mysql服务器优化包含 硬件优化.操作系统配置优化(cpu调度.网络.内存.虚拟内存.磁盘io).Mysql服务配置优化(最大连接数.表缓存等.存储引擎).表结构优化.索引优化 总共5个方面. 本片 ...
- Centos7 systemctl和防火墙firewalld命令(参考https://www.cnblogs.com/marso/archive/2018/01/06/8214927.html)
一.防火墙的开启.关闭.禁用命令 (1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable fire ...
- UI自动化selenium
1.什么是UI自动化?模拟人用代码的方式去操作页面2.为什么要做UI自动化?后期迭代的时候,老功能比较多,人工维护成本较大,重复性工作较多,这个时候就考虑因为UI自动化3.什么时候做UI自动化?项目稳 ...
- SpringBoot的学习【3.HelloWorld配置细节】
/** * @SpringBootApplication用来标注主程序类. */ @SpringBootApplication public class First { public static v ...
- springmvc简单集成shiro
前言: 有天和同事聊天, 谈起权限管理, 他说他有个同事用shiro用的很溜. 正好现在有个管理平台项目, 有权限控制的需求, 因此想借此机会研究一番. 本文主要简单讲解一下对shiro的一些认识, ...
- 让MySQL数据库跑的更快的7个优化建议!
随着容量和负载的增加,MySQL 的性能会日趋缓慢.这里有七点建议能够保证 MySQL 的平稳运行. 性能是我们衡量应用的一种方式,而应用性能的一项指标就是用户体验,也就是平时我们常说的:“用户需要等 ...
- 工作中 sql 整理(一)
这篇文章记录关于SQL的内容,有些凌乱,是工作中点滴的积累,只能按照时间顺序,逐次记录. 一.update 关联更新 1.需求 Table A TableB A表中的主键和B表中的主键相关联,关联 ...
- if 循环
age_of_princal = 56guess_age = int(input(">>:")) if guess_age == age_of_princal: pri ...
- SpringAOP 注解方式
Spring-service-mvc.xml <context:component-scan base-package="com.restful.controller,com.rest ...
- PythonStudy——内存管理机制 Memory management mechanism
一.变量与对象 关系图如下: 1.变量:通过变量指针引用对象 变量指针指向具体对象的内存空间,取对象的值. 2.对象:类型已知,每个对象都包含一个头部信息(头部信息:类型标识符和引用计数器) 注意: ...