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. Linux 云服务器中安装 rinetd 进行转发端口实现

    端口转发映射的程序叫rinetd,直接make编译安装即可. wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -x ...

  2. Forth嵌套定义的执行过程图示

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  3. 4 Django应用 第3部分(视图部分)

    接着昨天写的那篇笔记,今天继续学习DJango中的内容.这一章主要是介绍Django中的视图部分. 4.1视图理念 4.2编写第一个视图 4.3编写更多的视图 4.4给视图编写功能 4.5render ...

  4. Sql语句内功心法

    CREATE SCHEMA <模式名> AUTHORIZATION <用户名> 定义模式实际上定义了一个命名空间,在这个空间可以进一步定义该模式包含的数据库对象,例如基本表,视 ...

  5. Java容器解析系列(5) AbstractSequentialList LinkedList 详解

    AbstractSequentialList为顺序访问的list提供了一个骨架实现,使实现顺序访问的list变得简单; 我们来看源码: /** AbstractSequentialList 继承自 A ...

  6. eclipse中创建DataBase Connections

    1.window --> show view --> other --> Data Management --> Data Exploerer --> ok: 2.右键单 ...

  7. ceph存储集群测试方案

    --测试目的 测试ceph集群的读写性能,根据测试数据了解整个ceph集群的性能情况. --测试环境 1.8节点ceph集群环境,1台虚拟机(cpu 8核,内存8G),8k的块大小,时长2小时 2.8 ...

  8. C# 图片处理方法 整理汇总

    /// <summary> /// 图片转为base64编码字符 /// </summary> /// <param name="path">图 ...

  9. Java六大必须理解的问题

    Java六大必须理解的问题 对于这个系列里的问题,每个学Java的人都应该搞懂.当然,如果只是学Java玩玩就无所谓了.如果你认为自己已经超越初学者了,却不很懂这些问题,请将你自己重归初学者行列.内容 ...

  10. HDU 2062:Subset sequence(思维)

    Subset sequence Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...