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 // aa,mid // bb,减去含有的最小公倍数个数mid//c,结果t则为左半区间满足数字的个数

if t < n:
lo = mid + 1
else:
hi = mid

和目标格式N相比,下个目标区间

END.

第三题总结:其实我也也想到了要用最大公约数或者最小公倍数;想要判断AB含有重复的值。但是不知道可以定义一个大范围,然后二分法判断有多少个数字,以及最小公倍数的使用。数学又挡住了我。

LeetCode contest-95[876,877,👁878]的更多相关文章

  1. LeetCode Contest 166

    LeetCode Contest 166 第一次知道LeetCode 也有比赛. 很久没有打过这种线上的比赛,很激动. 直接写题解吧 第一题 很弱智 class Solution { public: ...

  2. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  3. leetcode contest 20

    Q1: 520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right ...

  4. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 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 ...

  7. LeetCode(95): 不同的二叉搜索树 II

    Medium! 题目描述: 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Beta 冲刺 (4/7)

    Beta 冲刺 (4/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 准备四六级 展示GitHub当日代码/ ...

  2. JAVA数据库操作回滚小结

    一:总结的原因 在最近的工作中,遇到了一个一对多关系多表数据传输,传送成功状态绑定在主数据表上,因为代码不健壮问题造成了主表传送状态更新失败,而子表数据就被重复插入.又由于数据传输频率很高,我们的测试 ...

  3. Linux修改日期、时间,系统与硬件时间

    Linux的时间分为两种,硬件时间和系统时间两种: 一.查看与修改系统时间 查看系统时间:date # date Fri Nov 26 15:20:18 CST 1999 用指定的格式显示系统时间:  ...

  4. Shell 变量替换及测试

    声明:$ 后面跟linux可执行命令 一.变量替换                   语法                      说明 ${变量名#匹配规则} 从变量的开头进行规则匹配,将符合最 ...

  5. mysql慢查询----pt-query-digest详解慢查询日志(linux系统)

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  6. 剑指Offer 35. 数组中的逆序对 (数组)

    题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...

  7. 解决使用angular2路由后,页面刷新后报404错误。

    点击路由链接跳转页面是正常的,但是当刷新页面时就出现了404错误. 解决方法如下: 在app.module.ts中添加import: import {HashLocationStrategy,Loca ...

  8. Linux监控平台、安装zabbix、修改zabbix的admin密码

    1.Linux监控平台 2. zabbix监控 3.zabbix的安装下载:wget -c https://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbi ...

  9. java-IO流-字符流-FileReader、FileWriter、自定义小数组的拷贝、BufferedReader、BufferedWriter、readLine()和newLine()方法、LineNumberReader、使用指定的码表读写字符

    ###21.01_IO流(字符流FileReader) * 1.字符流是什么     * 字符流是可以直接读写字符的IO流     * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要 ...

  10. Redis缓存相关问题总结

    使用缓存是系统性能优化的第一黄金法则. 缓存的设计和使用对一个系统的性能至关重要,平时接触到项目无论多少也都会在某些层面用到缓存,比如用HashMap实现,Ehcache,memcached.redi ...