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. js运行机制

    情况一 script标签里面的运行顺序是同步的 遇到settimeout的时候就会变异步,最后执行 执行顺序为1342 情况二 只输出a 情况三 输出4444 异步队列插入的时间和执行时间 for循环 ...

  2. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  3. linux 执行脚本1.补充命令 2.后台执行

    nohup是永久执行&是指在后台运行2>&1 是将标准出错重定向到标准输出,这里的标准输出已经重定向到了out.file文件,即将标准出错也输出到out.file文件中.最后一个 ...

  4. Java语法基础学习DaySixteen(多线程)

    一.多线程的创建 1.作用 程序需要同时执行两个或多个任务时需要多线程. 程序需要实现需要等待的任务时,如用户输入.文件读写操作.网络操作.搜索等,需要多线程. 需要一些后台运行的程序时,需要多线程. ...

  5. php优秀框架codeigniter学习系列——CI_Router类学习

    这篇文章主要介绍CI核心框架工具类CI_Router. 如果说CI_URI类是用来解析URI,那么CI_Router类就应该是根据解析出来的URI来决定究竟访问哪一个文件和哪一个function. 详 ...

  6. angular5理解生命周期

    先来看下文档: 按照顺序有八个: 1.ngOnChanges()=>简单理解为当数据绑定输入属性的值发生变化时调用: 2.ngOnInit() => 在调用完构造函数.初始化完所有输入属性 ...

  7. CSS3 transform变形(3D转换)

    一.三维坐标 空间中三维坐标如下图所示: 向上为-Y,向下为+Y,向左为-X,向右为+X,向前为+Z,向后为-Z. 二.perspective(n)为 3D 转换元素定义透视视图 perspectiv ...

  8. CH3401 石头游戏(矩阵快速幂加速递推)

    题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...

  9. Dijkstra(迪杰斯特拉)模板

    直接将模板封装在结构体里面. struct Edge{ int from,to,dist; Edge(int u, int v,int d): from(u),to(v),dist(d){} }; s ...

  10. 学习笔记TF036:实现Bidirectional LSTM Classifier

    双向循环神经网络(Bidirectional Recurrent Neural Networks,Bi-RNN),Schuster.Paliwal,1997年首次提出,和LSTM同年.Bi-RNN,增 ...