单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod

class Solution:
# @return: The same instance of this class every time
__m_Solution=None @classmethod
def getInstance(self):
# write your code here
if self.__m_Solution == None:
self.__m_Solution=Solution()
return self.__m_Solution def __init__(self):
pass

遍历二叉树路径,输出与给定值相同的所有路径,这里是递归调用的,本来像把递归改成循环的 但是高了好久 没搞出来····

class Solution:
# @param {int} target an integer
# @return {int[][]} all valid paths
def binaryTreePathSum(self, root, target):
# Write your code here
res = []
path = []
import copy
def getallpath(current, path, res):
if current == None: return
path.append(current.val)
if current.left != None:
getallpath(current.left,copy.deepcopy(path), res)
if current.right != None:
getallpath(current.right,copy.deepcopy(path),res)
if current.left is None and current.right is None:
res.append(path) getallpath(root,path,res)
ps=[]
for p in res:
sum=0
for v in p:
sum=sum+v
if sum==target:ps.append(p)
return ps

三角形计数 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形

直接遍历会提示超过时间限制

选择排序加二分查找的方法来提高效率,python默认的递归有上线,需要设置,  还有二分查找的方法是先确认最大最小边然后找第三边,这样条件就变成一个

class Solution:
# @param S: a list of integers
# @return: a integer
def triangleCount(self, S):
# write your code here
def quicksort(a): # 快速排序
if len(a) < 2:
return a
else:
pivot = a[0]
less = [i for i in a[1:] if i <= pivot]
greator = [i for i in a[1:] if i > pivot]
return quicksort(less) + [pivot] + quicksort(greator) n = len(S)
if n < 3: return
import sys
sys.setrecursionlimit(1000000)
S = quicksort(S)
sum = 0
for i in range(n):
for j in range(i + 1, n):
l = i + 1
r = j
target = S[j] - S[i]
while l < r:
mid = (l + r) // 2
if S[mid] > target:
r = mid
else:
l = mid + 1
sum = sum + j - l return sum

将一个二叉查找树按照中序遍历转换成双向链表。

"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
this.val = val
this.left, this.right = None, None
Definition of Doubly-ListNode
class DoublyListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = self.prev = next
""" class Solution:
"""
@param root, the root of tree
@return: a doubly list node
"""
def bstToDoublyList(self, root):
# Write your code here
l=[]
if root is None:return
stack=[]
current=root
dlist=None
cur=None
last=None
while len(stack)> 0 or current is not None:
while current is not None:
stack.append(current)
current=current.left
current=stack.pop()
l.append(current.val)
cur=DoublyListNode(current.val)
cur.prev=last
if last is not None: last.next=cur
last=cur
if dlist is None :dlist=cur
current=current.right
return dlist

lintcode 刷题记录··的更多相关文章

  1. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

  2. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  3. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  4. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  5. 刷题记录:[HarekazeCTF2019]encode_and_encode

    目录 刷题记录:[HarekazeCTF2019]encode_and_encode 一.知识点 JSON转义字符绕过 php伪协议 刷题记录:[HarekazeCTF2019]encode_and_ ...

  6. 刷题记录:[De1CTF 2019]Giftbox && Comment

    目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 ...

  7. 刷题记录:[强网杯 2019]Upload

    目录 刷题记录:[强网杯 2019]Upload 一.知识点 1.源码泄露 2.php反序列化 刷题记录:[强网杯 2019]Upload 题目复现链接:https://buuoj.cn/challe ...

  8. 刷题记录:[XNUCA2019Qualifier]EasyPHP

    目录 刷题记录:[XNUCA2019Qualifier]EasyPHP 解法一 1.error_log结合log_errors自定义错误日志 2.include_path设置包含路径 3.php_va ...

  9. 刷题记录:[DDCTF 2019]homebrew event loop

    目录 刷题记录:[DDCTF 2019]homebrew event loop 知识点 1.逻辑漏洞 2.flask session解密 总结 刷题记录:[DDCTF 2019]homebrew ev ...

随机推荐

  1. redis 3.0 集群__配置文件详解(常用配置)

    参考文档 http://www.cnblogs.com/huangjacky/p/3700473.html http://www.cnblogs.com/cxd4321/archive/2012/12 ...

  2. 使用 webpack 搭建 React 项目

    简评:相信很多开发者在入门 react 的时候都是使用 create-react-app 或 react-slingshot 这些脚手架来快速创建应用,当有特殊需求,需要修改 eject 出来的 we ...

  3. POJ-2251-Dungeon Master(3D迷宫,BFS)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 48111   Accepted: 18149 ...

  4. 报错The sandbox is not in sync with the Podfile.lock

    clone下来的项目,运行的时候报错 diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such fi ...

  5. 2016级算法第一次练习赛-B.朴素的中位数

    朴素的中位数 题目链接:https://buaacoding.cn/problem/846/index 分析 题意很简单,就是给定了两个从小到大排好序的数组,找出这两个数组合起来的数据中的中位数. 方 ...

  6. 用API爬取天气预报数据

    1.注册免费API和阅读技术文档: 注册地址:https://console.heweather.com 文档地址:https://www.heweather.com/documents/api-ur ...

  7. mysql 2006 go away 错误

    https://blog.csdn.net/yypsober/article/details/71330673 原文地址.

  8. AS添加依赖库提示Manifest merger failed解决办法

    今天在学习<Android权威编程指南>时 在project structure中添加recyclerview时提示错误 按照提示添加tools:replace标签还是报错 然后切换至bu ...

  9. MyEclipse配置,每次打开server中都没有weblogic

    最近在myeclipse新配了个weblogic,结果每次打开myeclipse在server中都看不到weblogic,得重新去配置页面走一遭才能出现.很麻烦. 后来在网上找了找,找到一个办法: 在 ...

  10. 怎么在vue中引入layui

    新项目想用layui框架,学习了把前辈是怎么引入layui的,这里记录下 1.index.html要引入layui.js文件 <script src="/static/layui/la ...